Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fallback to english for knowledge panels #6804

Merged
merged 3 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ sub process_template($$$) {
return display_taxonomy_tag($lc, $_[0], $_[1]);
};

# Display one taxonomy entry in the target language, without language prefix
$template_data_ref->{display_taxonomy_tag_name} = sub ($$) {
return display_taxonomy_tag_name($lc, $_[0], $_[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok I guess, I don't think we have templates where we do want the tag with the leading language code. We might in the future though, e.g. if we want to use panels to link to a list of products with a specific tag.

Another option is to change display_taxonomy_tag to display_taxonomy_tag_name in all templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change to have a specific display_taxonomy_tag_name available in templates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've change that, to instead have two function and use appropriate one in knowledge panels.

};

# Display a list of taxonomy entries in the target language
$template_data_ref->{display_taxonomy_tags_list} = sub ($$) {
my $tagtype = shift;
Expand Down
4 changes: 4 additions & 0 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ sub create_ecoscore_panel($$$) {
# Add properties of interest
foreach my $property (qw(environmental_benefits description)) {
my $property_value = get_inherited_property("labels", $labelid, $property . ":" . $target_lc);
if (not defined $property_value && $target_lc != "en") {
# fallback to english
$property_value = get_inherited_property("labels", $labelid, $property . ":" . "en");
}
if (defined $property_value) {
$label_panel_data_ref->{$property} = $property_value;
}
Expand Down
46 changes: 46 additions & 0 deletions lib/ProductOpener/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ BEGIN
&canonicalize_taxonomy_tag_link
&exists_taxonomy_tag
&display_taxonomy_tag
&display_taxonomy_tag_name
&display_taxonomy_tag_link
&get_taxonomy_tag_and_link_for_lang

Expand Down Expand Up @@ -3443,6 +3444,25 @@ sub exists_taxonomy_tag($$) {
}


=head2 display_taxonomy_tag ( $target_lc, $tagtype, $canon_tagid )

Return the name of a tag for displaying it to the user

=head3 Arguments

=head4 $target_lc - target language code

=head4 $tagtype

=head4 $canon_tagid

=head3 Return values

The tag translation if it exists in target language,
otherwise, the tag id.

=cut

sub display_taxonomy_tag($$$)
{
my $target_lc = shift; $target_lc =~ s/_.*//;
Expand Down Expand Up @@ -3548,6 +3568,32 @@ sub display_taxonomy_tag($$$)
}


=head2 display_taxonomy_tag_name ( $target_lc, $tagtype, $canon_tagid )

A version of display_taxonomy_tag that removes eventual language prefix

=head3 Arguments

=head4 $target_lc - target language code

=head4 $tagtype

=head4 $canon_tagid

=head3 Return values

The tag translation if it exists in target language,
otherwise, the tag in its primary language

=cut

sub display_taxonomy_tag_name($$$)
{
my $display_value = display_taxonomy_tag($_[0], $_[1], $_[2]);
# remove eventual leading language code
$display_value =~ s/^\w\w://;
return $display_value;
}

sub canonicalize_tag_link($$)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<br>

[% IF panel.ecoscore_extended_data_for_category.defined %]
<p>Estimated impact for the category ([% display_taxonomy_tag("categories",panel.ecoscore_extended_data_for_category.category_id) %]):</p>
<p>Estimated impact for the category ([% display_taxonomy_tag_name("categories",panel.ecoscore_extended_data_for_category.category_id) %]):</p>

<ul>
<li>Climate change score average: [% panel.ecoscore_extended_data_for_category.climate_change %]</li>
Expand Down Expand Up @@ -79,7 +79,7 @@
"id": "[% step %]",
"values": [
{
"text": "[% display_taxonomy_tag('ingredients', ingredient.id) %]"
"text": "[% display_taxonomy_tag_name('ingredients', ingredient.id) %]"
},
{
"text": "[% ingredient.quantity %]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
{
"values": [
{
"text": "[% display_taxonomy_tag("origins",origin.origin) %]",
"text": "[% display_taxonomy_tag_name("origins",origin.origin) %]",
},
{
"text": "[% round(origin.percent) %] %",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@
{
"values": [
{
"text": "[% IF packaging.number %][% packaging.number %] [% END %][% display_taxonomy_tag('packaging_shapes',packaging.shape) %][% IF packaging.quantity %] ([% packaging.quantity %])[% END %]"
"text": "[% IF packaging.number %][% packaging.number %] [% END %][% display_taxonomy_tag_name('packaging_shapes',packaging.shape) %][% IF packaging.quantity %] ([% packaging.quantity %])[% END %]"
},
{
"text": "[% display_taxonomy_tag('packaging_materials',packaging.material) %]"
"text": "[% display_taxonomy_tag_name('packaging_materials',packaging.material) %]"
},
{
"text": "[% display_taxonomy_tag('packaging_recycling',packaging.recycling) %]",
"text": "[% display_taxonomy_tag_name('packaging_recycling',packaging.recycling) %]",
[% IF packaging.recycling == "en:recycle" %]
"evaluation": "good",
[% ELSIF packaging.recycling == "en:discard" %]
Expand Down
2 changes: 1 addition & 1 deletion templates/api/knowledge-panels/environment/label.tt.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"evaluation": "good",
"title_element": {
"title": "[% display_taxonomy_tag("labels",panel.label) %]",
"title": "[% display_taxonomy_tag_name("labels", panel.label) %]",
[% IF panel.icon_url.defined %]
"icon_url": "[% panel.icon_url %]",
[% END %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"title_element": {
"title": "[% lang('manufacturing_places_s') FILTER ucfirst %]",
[% IF panel.packager_code_data.cc == 'fr' %]
"subtitle": "[% panel.packager_code_data.commune %] - [% display_taxonomy_tag('countries', 'en:france') %]",
"subtitle": "[% panel.packager_code_data.commune %] - [% display_taxonomy_tag_name('countries', 'en:france') %]",
[% ELSIF panel.packager_code_data.cc == 'ch' %]
"subtitle": "[% panel.packager_code_data.full_address %]",
[% ELSIF panel.packager_code_data.cc == 'es' %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{
"values": [
{
"text": "[% display_taxonomy_tag("origins",origin.origin) %]",
"text": "[% display_taxonomy_tag_name("origins",origin.origin) %]",
},
{
"text": "[% round(origin.percent) %] %",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
[% IF recycling_type == "en:recycle" %]
"evaluation": "good",
"icon_url": "[% static_subdomain %]/images/icons/dist/recycle-variant.svg",
"icon_alt": "[% display_taxonomy_tag("packaging_recycling",recycling_type) %]",
"icon_alt": "[% display_taxonomy_tag_name("packaging_recycling",recycling_type) %]",
[% ELSIF recycling_type == "en:discard" %]
"evaluation": "bad",
"icon_url": "[% static_subdomain %]/images/icons/dist/delete.svg",
"icon_alt": "[% display_taxonomy_tag("packaging_recycling",recycling_type) %]",
"icon_alt": "[% display_taxonomy_tag_name("packaging_recycling",recycling_type) %]",
[% ELSE %]
"evaluation": "neutral",
"icon_url": "[% static_subdomain %]/images/icons/dist/help.svg",
Expand All @@ -77,9 +77,9 @@
"html": `
[% FOREACH packaging IN product.packagings %]
[% IF packaging.recycling == recycling_type OR (recycling_type == "en:unknown" AND NOT packaging.recycling.defined) %]
<strong>[% display_taxonomy_tag('packaging_shapes',packaging.shape) %]</strong>
<strong>[% display_taxonomy_tag_name('packaging_shapes',packaging.shape) %]</strong>
[% IF packaging.material %]
([% display_taxonomy_tag('packaging_materials',packaging.material) %])
([% display_taxonomy_tag_name('packaging_materials',packaging.material) %])
[% END %]
<br>
[% END %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"size": "small",
"title_element": {
"title": `[% display_taxonomy_tag("additives", panel.additive) %]`,
"title": `[% display_taxonomy_tag_name("additives", panel.additive) %]`,
},
"elements": [
[% IF panel.wikipedia_abstract %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<p>[% f_lang('f_nova_markers_for_nova_group', { 'nova_group' => panel.nova_group_name }) %][% sep %]:</p>
<ul>
[% FOREACH marker IN product.nova_groups_markers.$nova_group %]
<li>[% lang(marker.0 _ '_s') FILTER ucfirst %][% sep %]: [% display_taxonomy_tag(marker.0, marker.1) %]</li>
<li>[% lang(marker.0 _ '_s') FILTER ucfirst %][% sep %]: [% display_taxonomy_tag_name(marker.0, marker.1) %]</li>
[% END %]
</ul>
[% END %]
Expand Down