Skip to content

Commit

Permalink
very small start of a refactoring for handling links of texts transla…
Browse files Browse the repository at this point in the history
…tions, related to #1818, fixes #5260
  • Loading branch information
stephanegigandet committed May 27, 2021
1 parent 5bc0db9 commit 6c5100d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ BEGIN
&count_products
&add_params_to_query
&url_for_text
&process_template
@search_series
Expand Down Expand Up @@ -297,8 +298,44 @@ $world_subdomain = format_subdomain('world');

my $user_preferences; # enables using user preferences to show a product summary and to rank and filter results


=head1 FUNCTIONS
=head2 url_for_text ( $textid )
Return the localized URL for a text. (e.g. "data" points to /data in English and /donnees in French)
=cut

# Note: the following urls are currently hardcoded, but the idea is to build the mapping table
# at startup from the available translated texts in the repository. (TODO)
my %urls_for_texts = (
"ecoscore" => {
en => "eco-score-the-environmental-impact-of-food-products",
fr => "eco-score-l-impact-environnemental-des-produits-alimentaires",
},
);

sub url_for_text($) {

my $textid = shift;

if (not defined $urls_for_texts{$textid}) {
return "/" . $textid;
}
elsif (defined $urls_for_texts{$textid}{$lc}) {
return "/" . $urls_for_texts{$textid}{$lc};
}
elsif ($urls_for_texts{$textid}{en}) {
return "/" . $urls_for_texts{$textid}{en};
}
else {
return "/" . $textid;
}
}


=head2 process_template ( $template_filename , $template_data_ref , $result_content_ref )
Add some functions and variables needed by many templates and process the template with template toolkit.
Expand All @@ -324,6 +361,7 @@ sub process_template($$$) {
$template_data_ref->{display_date_without_time} = \&display_date_without_time;
$template_data_ref->{display_date_ymd} = \&display_date_ymd;
$template_data_ref->{display_date_tag} = \&display_date_tag;
$template_data_ref->{url_for_text} = \&url_for_text;
$template_data_ref->{display_taxonomy_tag} = sub ($$) {
return display_taxonomy_tag($lc, $_[0], $_[1]);
};
Expand Down
4 changes: 2 additions & 2 deletions templates/display_product.tt.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ <h2 id="environmental_impact">[% lang('environmental_impact') %]</h2>

[% IF ecoscore_grade %]
<h4>Eco-score
<a href="/ecoscore" title="[% lang('ecoscore_information') %]">[% display_icon('info') %]</a>
<a href="[% url_for_text("ecoscore") %]" title="[% lang('ecoscore_information') %]">[% display_icon('info') %]</a>
</h4>

<p>[% lang("ecoscore_description") %]</p>
Expand All @@ -238,7 +238,7 @@ <h4>Eco-score
<p class="note">→ [% lang("ecoscore_warning_international") %]</p>
[% END %]

<a href="/ecoscore" title="[% lang('ecoscore_information') %]">
<a href="[% url_for_text("ecoscore") %]" title="[% lang('ecoscore_information') %]">
<img src="/images/icons/ecoscore-[% ecoscore_grade_lc %].svg" alt="Eco-score [% ecoscore_grade %]" style="margin-bottom:1rem;max-width:100%">
</a>
<br>
Expand Down

0 comments on commit 6c5100d

Please sign in to comment.