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 f_lang function to emulate python f-strings for translations #5962

Merged
merged 2 commits into from
Oct 12, 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
7 changes: 4 additions & 3 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,10 @@ ($$$$$)
# this needs to be below the "add (language name) in all field labels" above, so that it does not change this label.
if (($User{moderator}) and ($tabsid eq "front_image")) {

my $msg = sprintf(lang("move_data_and_photos_to_main_language"),
'<span class="tab_language">' . $language . '</span>',
'<span class="main_language">' . lang("lang_" . $product_ref->{lc}) . '</span>');
my $msg = f_lang("f_move_data_and_photos_to_main_language", {
language => '<span class="tab_language">' . $language . '</span>',
main_language => '<span class="main_language">' . lang("lang_" . $product_ref->{lc}) . '</span>'
});

my $moveid = "move_" . $tabid . "_data_and_images_to_main_language";

Expand Down
1 change: 1 addition & 0 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} = \&lang;
$template_data_ref->{f_lang} = \&f_lang;
$template_data_ref->{lang_sprintf} = \&lang_sprintf;
$template_data_ref->{lc} = $lc;
$template_data_ref->{cc} = $cc;
Expand Down
55 changes: 52 additions & 3 deletions lib/ProductOpener/Lang.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ BEGIN

&lang
&lang_sprintf
&f_lang
&lang_in_other_lc
%lang_lc

Expand Down Expand Up @@ -113,7 +114,7 @@ sub separator_before_colon($) {

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.
If a translation is not available, the function returns English.

=head3 Arguments

Expand Down Expand Up @@ -157,14 +158,21 @@ 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.
Warning: if multiple variables need to be interpolated,
they will be in the same order for all languages.

If a translation is not available, the function returns English.

=head3 Arguments

=head4 string id $stringid

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

=head4 other arguments

Arguments to be interpolated.

=cut

sub lang_sprintf() {
Expand All @@ -181,11 +189,52 @@ sub lang_sprintf() {
}


=head2 f_lang( $stringid, $variables_ref )

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 Python's f-string format with
named parameters between { }.

e.g. "This is a string with {a_variable} and {another_variable}."

Variables between { } are interpolated with the corresponding entry
in the $variables_ref hash reference.

If a translation is not available, the function returns English.

=head3 Arguments

=head4 string id $stringid

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

=cut

sub f_lang($$) {

my $stringid = shift;
my $variables_ref = shift;

my $translation = lang($stringid);
if (defined $translation) {
# look for string keys between { } and replace them with the corresponding
# value in $variables_ref hash reference
$translation =~ s/\{([^\{\}]+)\}/$variables_ref->{$1}/eg;
return $translation;
}
else {
return '';
}
}


=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.
If a translation is not available, the function returns English.

=head3 Arguments

Expand Down
24 changes: 12 additions & 12 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -4305,10 +4305,10 @@ msgctxt "some_unknown_ingredients"
msgid "Some ingredients could not be recognized."
msgstr "Some ingredients could not be recognized."

# keep the 2 %s : they will be replaced by language names.
msgctxt "move_data_and_photos_to_main_language"
msgid "Move all data and selected photos in %s to the main language of the product: %s"
msgstr "Move all data and selected photos in %s to the main language of the product: %s"
# variable names between { } must not be translated
msgctxt "f_move_data_and_photos_to_main_language"
msgid "Move all data and selected photos in {language} to the main language of the product: {main_language}"
msgstr "Move all data and selected photos in {language} to the main language of the product: {main_language}"

msgctxt "move_data_and_photos_to_main_language_replace"
msgid "Replace existing values and selected photos"
Expand Down Expand Up @@ -5790,15 +5790,15 @@ 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"
# variable names between { } must not be translated
msgctxt "f_carbon_footprint_per_100g_of_product"
msgid "{grams} g CO² per 100g of product"
msgstr "{grams} 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"
# variable names between { } must not be translated
msgctxt "f_equal_to_driving_km_in_a_petrol_car"
msgid "Equal to driving {kilometers} km in a petrol car"
msgstr "Equal to driving {kilometers} km in a petrol car"

msgctxt "source_ademe_agribalyse"
msgid "Source: ADEME Agribalyse Database"
Expand Down
26 changes: 13 additions & 13 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -4317,10 +4317,10 @@ msgctxt "some_unknown_ingredients"
msgid "Some ingredients could not be recognized."
msgstr "Some ingredients could not be recognized."

# keep the 2 %s : they will be replaced by language names.
msgctxt "move_data_and_photos_to_main_language"
msgid "Move all data and selected photos in %s to the main language of the product: %s"
msgstr "Move all data and selected photos in %s to the main language of the product: %s"
# variable names between { } must not be translated
msgctxt "f_move_data_and_photos_to_main_language"
msgid "Move all data and selected photos in {language} to the main language of the product: {main_language}"
msgstr "Move all data and selected photos in {language} to the main language of the product: {main_language}"

msgctxt "move_data_and_photos_to_main_language_replace"
msgid "Replace existing values and selected photos"
Expand Down Expand Up @@ -5807,15 +5807,15 @@ 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"
# variable names between { } must not be translated
msgctxt "f_carbon_footprint_per_100g_of_product"
msgid "{grams} g CO² per 100g of product"
msgstr "{grams} 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"
# variable names between { } must not be translated
msgctxt "f_equal_to_driving_km_in_a_petrol_car"
msgid "Equal to driving {kilometers} km in a petrol car"
msgstr "Equal to driving {kilometers} km in a petrol car"

msgctxt "source_ademe_agribalyse"
msgid "Source: ADEME Agribalyse Database"
Expand All @@ -5827,4 +5827,4 @@ msgstr "Environment"

msgctxt "health_card_title"
msgid "Nutrition and health"
msgstr "Nutrition and health"
msgstr "Nutrition and health"
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
[% END %]
"title_element": {
[% SET driving_100g_rounded = driving_100g FILTER format('%.1f'); %]
"title": "[% lang_sprintf('equal_to_driving_s_km_in_a_petrol_car', driving_100g_rounded) %]",
"title": "[% f_lang('f_equal_to_driving_km_in_a_petrol_car', { 'kilometers' => driving_100g_rounded } ) %]",
[% SET co2_100g_rounded = co2_100g * 1000 FILTER format('%.0f'); %]
"subtitle": "[% lang_sprintf('s_carbon_footprint_per_100g_of_product', co2_100g_rounded) %]",
"subtitle": "[% f_lang('f_carbon_footprint_per_100g_of_product', { 'grams' => co2_100g_rounded }) %]",
"icon_url": "[% static_subdomain %]/images/icons/dist/car.svg",
"icon_color_from_evaluation": true,
},
Expand Down