Numeric fields can have a prefix or suffix. These are defined in both singular and plural form. When the field is shown on a page the suffix gets translated using the format_plural() function (this is done in the number_field_formatter_view() function). However, on the edit form, this prefix or suffix is not translated, it is just filtered (using function field_filter_xss())and displayed (this happens in function number_field_widget_form()).

Aside from the fact that other field properties are translated via contrib, this is not consistent and considered an error. To solve this in D7, I guess that we have to use t() for prefix/suffix on the edit form as format_plural does so on showing the field.

In D8, the situation will be completely different.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fietserwin’s picture

Status: Active » Needs review
FileSize
802 bytes

Attached a patch that uses t() (as outlined in the problem summary).

Status: Needs review » Needs work

The last submitted patch, 1220964-1.patch, failed testing.

yched’s picture

Title: Field prefix/suffix are not translated on entity edit form » Number field prefix/suffix get t()'ed through format_plural()
Version: 7.x-dev » 8.x-dev
Status: Needs work » Needs review
Issue tags: +Needs backport to D7
FileSize
1.35 KB

t() is not meant to be called on user-entered data, that is the realm of http://drupal.org/project/i18n.
Translation of field properties is handled by the i18n_field module.

It's rather a bug in number.module that number_field_formatter_view() runs the prefixes and suffixes through format_plural(). We need to use our own non-t() logic in there.

I guess something like the attached patch. This does not support languages with more than one plural form, but :
- I don't think the current code (straight from the CCK era) does either - at least not officially. The help text for the 'prefix' and 'suffix settings reads : "Define a string that should be suffixed to the value. Separate singular and plural values with a pipe ('pound|pounds')".
- At this time in the night, I can't seem to make any sense of the logic behind multi-plural languages inside format_plural() anyway.

In other words, opinions from i18n / locale people welcome :-)

sun’s picture

Status: Needs review » Needs work
+++ b/modules/field/modules/number/number.module
@@ -284,8 +284,11 @@ function number_field_formatter_view($entity_type, $entity, $field, $instance, $
+          // We cannot use format_plural() to determine the right strings, ¶
+          // because those should not go through t().
+          $index = ($item['value'] == 1) ? 0 : 1;
+          $prefix = (count($prefixes) > 1) ? $prefixes[$index] : $prefixes[0];
+          $suffix = (count($suffixes) > 1) ? $suffixes[$index] : $suffixes[0];
           $output = $prefix . $output . $suffix;

mmm, I don't want to sound bitchy, but the added inline comment documents a fact or bug that shouldn't have existed in the first place (common knowledge)... -- whereas I have an extremely hard time to figure out what that effin' $index logic is actually doing, but there's no comment for that... ;)

10 days to next Drupal core point release.

fietserwin’s picture

Status: Needs work » Needs review
FileSize
2 KB

Having a 2nd look at the code of format_plural and it turns out it is quite easy to circumvent the use of format_plural. Because what we want to know is what plural form to use. That is not as easy as looking at the value (1 = singular , other = plural) because there are languages where it is much more complicated than that (see e.g. http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html). But within format_plural this logic is outsourced to locale_get_plural(). Thus the number_field module should use locale_get_plural() itself to get the "plural form index".

Note as there are languages that have more than 2 plural forms, the code should just check if the index is within range.

More notes:
- i18n_field must translate before explode()'ing the prefix/suffix string as a user entered string with 2 plural forms can lead to a prefix/suffix string with 3 or 4 entries in another language. I'm not sue if i18n_field is already doing anything with prefix/suffix. If not I will create an issue in the i18n queue.
- The "plural form index" should perhaps be better explained in the documentation of function locale_get_plural. I will create a separate issue for that as I think it should be renamed to locale_get_plural_form_index and that code contains superfluous return statements.

fietserwin’s picture

fietserwin’s picture

sun’s picture

Issue tags: +D8MI

This remix, of directly accessing and using locale information in the global language object to handle and output plural forms of user input, requires architectural discussion.

fietserwin’s picture

FileSize
1.95 KB

Patch contained 2 lines to initialize $index, reposting a new one.

@todo: if locale_get_plural() won't return -1 anymore (see issue #1221276: locale_get_plural() only works for a single language within a request and does not work for English) this code can be further cleaned-up. Not sure if I should put this todo in the patch?

@sun: how is the global language object involved? (Other than not using the $langcode passed in, which seems to be the wrong way to go assuming the patch in #1216772: Field allowed values are shown untranslated is correct.)

sun’s picture

  1. locale_get_plural() only exists if Locale module is installed. Whereas number fields also exist on sites without Locale module.
  2. locale_get_plural() takes the plural formulas from the configured list of languages.
  3. locale_get_plural() does not work for English.
  4. This is the first time I see code attempting to use locale meta information from configured languages to process configured plural form strings (user input). The idea definitely makes sense, but Drupal core is not prepared at all for this.
fietserwin’s picture

I get it and your arguments explain why the errors only pop up now and were not discovered earlier. But, looking forward, what does this mean for the process to follow? Accept a kind of dirty fix for D7 and subsequently address it properly within the D8MI initiative (which is an accepted backport workflow)? Or do you want it properly solved in D7 as well?

Gábor Hojtsy’s picture

Yes, I theoretically agree that given that we assume the site setting is in the default site language, we should use the plural rules for the default site language. Looks like the easiest way out would be to fix locale_get_plural() to work with English as well.

sun’s picture

Status: Needs review » Postponed

Postponing on #1221276: locale_get_plural() only works for a single language within a request and does not work for English -- there's a working patch over there, just needs tests to get RTBC.

Gábor Hojtsy’s picture

Issue tags: +language-ui

Adding UI language translation tag.

#1221276: locale_get_plural() only works for a single language within a request and does not work for English is nearing completion, please review there! Thanks!

Gábor Hojtsy’s picture

Issue tags: +language-content

Tagging for the content handling leg of D8MI (too).

Gábor Hojtsy’s picture

Status: Postponed » Needs work
jair’s picture

Issue tags: +Needs reroll

Needs reroll

amateescu’s picture

Component: field system » number.module
danylevskyi’s picture

Assigned: Unassigned » danylevskyi
danylevskyi’s picture

Assigned: danylevskyi » Unassigned
neetu morwani’s picture

Assigned: Unassigned » neetu morwani
Status: Needs work » Needs review
FileSize
15.03 KB

Rerolled the patch #9.

The last submitted patch, 1220964-21.patch, failed testing.

biro.botond’s picture

Status: Needs work » Needs review

21: 1220964-21.patch queued for re-testing.

biro.botond’s picture

Issue summary: View changes
Alumei’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

This was resolved for 8.x as part of #1785748: Field formatters as plugins although it propably still needs a backport to 7.x.

mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev
Assigned: neetu morwani » Unassigned

This still a concern in D8? Unassigned issue too.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (cannot reproduce)

I tested this on 9.4.x, standard install, and was not able to reproduce this error. On the edit form, the prefix and suffix are now translated.

Therefore, closing as cannot reproduce. If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!