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

Feat: add panel_group element and environment_card panel #5958

Merged
merged 3 commits into from
Oct 11, 2021
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
4 changes: 2 additions & 2 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ sub process_template($$$) {
$template_data_ref->{pro_moderator} = $User{pro_moderator};
$template_data_ref->{sep} = separator_before_colon($lc);
$template_data_ref->{lang} = \⟨
$template_data_ref->{lang_sprintf} = \&lang_sprintf;
$template_data_ref->{lc} = $lc;
$template_data_ref->{cc} = $cc;
$template_data_ref->{display_icon} = \&display_icon;
Expand Down Expand Up @@ -8266,8 +8267,7 @@ HTML
# for debugging and demonstration purposes
if (param('panels')) {
create_knowledge_panels($product_ref, $lc, $cc, $knowledge_panels_options_ref);
$template_data_ref->{ecoscore_panel} = display_knowledge_panel($product_ref->{"knowledge_panels_" . $lc}, "ecoscore");
$template_data_ref->{ecoscore_carbon_impact_panel} = display_knowledge_panel($product_ref->{"knowledge_panels_" . $lc}, "ecoscore_carbon_impact");
alexgarel marked this conversation as resolved.
Show resolved Hide resolved
$template_data_ref->{environment_card_panel} = display_knowledge_panel($product_ref->{"knowledge_panels_" . $lc}, "environment_card");
}
}

Expand Down
57 changes: 50 additions & 7 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ BEGIN

&create_knowledge_panels
&create_ecoscore_panels
&create_environment_card_panel

); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
Expand Down Expand Up @@ -162,6 +163,8 @@ sub create_knowledge_panels($$$$) {
# Add knowledge panels

create_ecoscore_panel($product_ref, $target_lc, $target_cc);

create_environment_card_panel($product_ref, $target_lc, $target_cc);
}


Expand Down Expand Up @@ -208,7 +211,7 @@ Some special features that are not included in the JSON format are supported:
=head4 panel template $panel_template

Relative path to the the template panel file, from the "/templates" directory.
e.g. "api/knowledge-panels/ecoscore/agribalyse.tt.json"
e.g. "api/knowledge-panels/environment/ecoscore/agribalyse.tt.json"

=head4 panel data reference $panel_data_ref (optional, can be an empty hash)

Expand Down Expand Up @@ -399,15 +402,15 @@ sub create_ecoscore_panel($$$) {
"title" => $title,
};

create_panel_from_json_template("ecoscore", "api/knowledge-panels/ecoscore/ecoscore.tt.json",
create_panel_from_json_template("ecoscore", "api/knowledge-panels/environment/ecoscore/ecoscore.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

# Add an Agribalyse panel to show the impact of the different steps for the category on average

create_panel_from_json_template("ecoscore_agribalyse", "api/knowledge-panels/ecoscore/agribalyse.tt.json",
create_panel_from_json_template("ecoscore_agribalyse", "api/knowledge-panels/environment/ecoscore/agribalyse.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

create_panel_from_json_template("ecoscore_carbon_impact", "api/knowledge-panels/ecoscore/carbon_impact.tt.json",
create_panel_from_json_template("carbon_footprint", "api/knowledge-panels/environment/carbon_footprint.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

# Add panels for the different bonuses and maluses
Expand All @@ -417,7 +420,7 @@ sub create_ecoscore_panel($$$) {
my $adjustment_panel_data_ref = {
};

create_panel_from_json_template("ecoscore_" . $adjustment, "api/knowledge-panels/ecoscore/" . $adjustment . ".tt.json",
create_panel_from_json_template("ecoscore_" . $adjustment, "api/knowledge-panels/environment/ecoscore/" . $adjustment . ".tt.json",
$adjustment_panel_data_ref, $product_ref, $target_lc, $target_cc);
}

Expand Down Expand Up @@ -446,16 +449,56 @@ sub create_ecoscore_panel($$$) {
}
}

create_panel_from_json_template("environment_label_" . $labelid, "api/knowledge-panels/ecoscore/label.tt.json",
create_panel_from_json_template("environment_label_" . $labelid, "api/knowledge-panels/environment/label.tt.json",
$label_panel_data_ref, $product_ref, $target_lc, $target_cc);
}
}
}
else {
my $panel_data_ref = {};
create_panel_from_json_template("ecoscore", "api/knowledge-panels/ecoscore/ecoscore_unknown.tt.json",
create_panel_from_json_template("ecoscore", "api/knowledge-panels/environment/ecoscore/ecoscore_unknown.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}
}


=head2 create_environment_card_panel ( $product_ref, $target_lc, $target_cc )

Creates a knowledge panel card that contains all knowledge panels related to the environment.

=head3 Arguments

=head4 product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

=head4 language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users.
This parameter sets the desired language for the user facing strings.

=head4 country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

=head3 Return value

The return value is a reference to the resulting knowledge panel data structure.

=cut

sub create_environment_card_panel($$$) {

my $product_ref = shift;
my $target_lc = shift;
my $target_cc = shift;

$log->debug("create environment card panel", { code => $product_ref->{code} }) if $log->is_debug();


my $panel_data_ref = {};
create_panel_from_json_template("environment_card", "api/knowledge-panels/environment/environment_card.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}

1;
124 changes: 109 additions & 15 deletions lib/ProductOpener/Lang.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

=head1 NAME

ProductOpener::Lang - load and return translations

=head1 SYNOPSIS

C<ProductOpener::Lang> loads translations from .po files and return translated strings
through the lang() and lang_sprintf() functions.

=head1 DESCRIPTION



=cut

package ProductOpener::Lang;

use utf8;
Expand All @@ -42,6 +57,7 @@ BEGIN
@Langs

&lang
&lang_sprintf
&lang_in_other_lc
%lang_lc

Expand All @@ -65,6 +81,21 @@ use JSON::PP;

use Log::Any qw($log);

=head1 FUNCTIONS

=head2 separator_before_colon( $l )

=head3 Arguments

=head4 language code $l

In some languages like French, colons have a space before them.
e.g. "Valeur : 500" in French, "Value: 500" in English

This function returns a non-breaking space character for those languages.

=cut

sub separator_before_colon($) {

my $l = shift;
Expand All @@ -78,56 +109,119 @@ sub separator_before_colon($) {
}


# English values have been copied to languages that do not have defined values
# Use the global $lang variable
=head2 lang( $stringid )

Returns a translation for a specific string id in the language defined in the $lang global variable.

If a translation is not available, returns English.

=head3 Arguments

=head4 string id $stringid

In the .po translation files, we use the msgctxt field for the string id.

=cut

sub lang($) {

my $s = shift;
my $stringid = shift;

my $short_l = undef;
if ($lang =~ /_/) {
$short_l = $`; # pt_pt
}

if (not defined $Lang{$s}) {
# English values have been copied to languages that do not have defined values

if (not defined $Lang{$stringid}) {
return '';
}
elsif (defined $Lang{$s}{$lang}) {
return $Lang{$s}{$lang};
elsif (defined $Lang{$stringid}{$lang}) {
return $Lang{$stringid}{$lang};
}
elsif ((defined $short_l) and (defined $Lang{$stringid}{$short_l}) and ($Lang{$stringid}{$short_l} ne '')) {
return $Lang{$stringid}{$short_l};
}
else {
return '';
}
elsif ((defined $short_l) and (defined $Lang{$s}{$short_l}) and ($Lang{$s}{$short_l} ne '')) {
return $Lang{$s}{$short_l};
}


=head2 lang_sprintf( $stringid, [other arguments] )

Returns a translation for a specific string id with specific arguments
in the language defined in the $lang global variable.

The translation is stored using the sprintf format (e.g. with %s) and
lang_sprintf() calls sprintf() to process it.

If a translation is not available, returns English.

=head3 Arguments

=head4 string id $stringid

In the .po translation files, we use the msgctxt field for the string id.

=cut

sub lang_sprintf() {

my $stringid = shift;

my $translation = lang($stringid);
if (defined $translation) {
return sprintf($translation, @_);
}
else {
return '';
}
}

# Lang in a target language that may not be the same as the global $lang

=head2 lang_in_other_lc( $target_lc, $stringid )

Returns a translation for a specific string id in a specific language.

If a translation is not available, returns English.

=head3 Arguments

=head4 target language code $target_lc

=head4 string id $stringid

In the .po translation files, we use the msgctxt field for the string id.

=cut

sub lang_in_other_lc($$) {

my $target_lc = shift;
my $s = shift;
my $stringid = shift;

my $short_l = undef;
if ($target_lc =~ /_/) {
$short_l = $`; # pt_pt
}

if (not defined $Lang{$s}) {
if (not defined $Lang{$stringid}) {
return '';
}
elsif (defined $Lang{$s}{$target_lc}) {
return $Lang{$s}{$target_lc};
elsif (defined $Lang{$stringid}{$target_lc}) {
return $Lang{$stringid}{$target_lc};
}
elsif ((defined $short_l) and (defined $Lang{$s}{$short_l}) and ($Lang{$s}{$short_l} ne '')) {
return $Lang{$s}{$short_l};
elsif ((defined $short_l) and (defined $Lang{$stringid}{$short_l}) and ($Lang{$stringid}{$short_l} ne '')) {
return $Lang{$stringid}{$short_l};
}
else {
return '';
}
}


$log->info("initialize", { data_root => $data_root }) if $log->is_info();

# generate po files from %Lang or %Site_lang
Expand Down
26 changes: 26 additions & 0 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5785,3 +5785,29 @@ msgstr "Ingredients statistics for all products"
msgctxt "recipes_ingredients_for_each_product"
msgid "Ingredients for each product"
msgstr "Ingredients for each product"

msgctxt "carbon_footprint"
msgid "Carbon footprint"
msgstr "Carbon footprint"

# %s will be replaced by a number
msgctxt "s_carbon_footprint_per_100g_of_product"
msgid "%s g CO² per 100g of product"
msgstr "%s g CO² per 100g of product"

# %s will be replaced by a number
msgctxt "equal_to_driving_s_km_in_a_petrol_car"
msgid "Equal to driving %s km in a petrol car"
msgstr "Equal to driving %s km in a petrol car"

msgctxt "source_ademe_agribalyse"
msgid "Source: ADEME Agribalyse Database"
msgstr "Source: ADEME Agribalyse Database"

msgctxt "environment_card_title"
msgid "Environment"
msgstr "Environment"

msgctxt "health_card_title"
msgid "Nutrition and health"
msgstr "Nutrition and health"
28 changes: 27 additions & 1 deletion po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -5801,4 +5801,30 @@ msgstr "We need your donations to fund the Open Food Facts 2021 budget and to co

msgctxt "donation_cta"
msgid "Donate"
msgstr "Donate"
msgstr "Donate"

msgctxt "carbon_footprint"
msgid "Carbon footprint"
msgstr "Carbon footprint"

# %s will be replaced by a number
msgctxt "s_carbon_footprint_per_100g_of_product"
msgid "%s g CO² per 100g of product"
msgstr "%s g CO² per 100g of product"

# %s will be replaced by a number
msgctxt "equal_to_driving_s_km_in_a_petrol_car"
msgid "Equal to driving %s km in a petrol car"
msgstr "Equal to driving %s km in a petrol car"

msgctxt "source_ademe_agribalyse"
msgid "Source: ADEME Agribalyse Database"
msgstr "Source: ADEME Agribalyse Database"

msgctxt "environment_card_title"
msgid "Environment"
msgstr "Environment"

msgctxt "health_card_title"
msgid "Nutrition and health"
msgstr "Nutrition and health"
Loading