Explicit Plural string using iOS Stringsdict files
Asked Answered
C

2

5

I am getting started learning iOS Stringsdict files and found some existing code on a project which used the following syntax:

<key>zero</key>
<string>You no message.</string>

As per the CLDR, zero is an invalid plural in English and we expect to use explicit plural rules (=0 when using ICU MessageFormat)

I tried to find how to use explicit plural rules in iOS Stringsdict files and could not find any way to achieve this. Can someone confirm if this is supported or not?


Example of solutions (I cannot test them but maybe someone can?)

<key>0</key>
<string>You no message.</string>

Or

<key>=0</key>
<string>You no message.</string>

Extra reference on explicit plural rules part of the CLDR implementation of ICU MessageFormat:

https://formatjs.io/guides/message-syntax/#plural-format

=value This is used to match a specific value regardless of the plural categories of the current locale.

Crissy answered 23/4, 2020 at 17:55 Comment(8)
Can you explain what you mean by "invalid plural" in this question? Is this a technical term I'm not familiar with? (I agree that zero is certainly plural in English, and this strings file is incorrect. I'm just trying to understand what you mean by "invalid" here.) I'm unclear how this relates to MessageFormat. ICU lumps zero as "other" (and thus plural) in English: unicode-org.github.io/cldr-staging/charts/37/supplemental/… (It's a very interesting question; I'd just like to understand it better. Can you link the part of the CLDR you're referencing?)Melissiamelita
Yes, the rules for English in the CLDR (the same link you provided) only support one and other. From my perspective, Stringsdict file is just Apple's custom implementation is ICU MessageFormat in terms of plural rules. In MessageFormat, there is a way to explicitly overwrite the categories with hard numbers to increase fluency (example in the question). See another example here: formatjs.io/guides/message-syntax/#plural-format. I could not find if it also supports explicit plural rules like the official syntax or if it just supports the rules defined in the CLDR.Crissy
"Stringsdict file is just Apple's custom implementation is ICU MessageFormat in terms of plural rules." Why do you believe that? I'd say Stringsdict is Apple's implementation of a system of translation, which MessageFormat also is. That doesn't mean they have anything like the same features, or even goals. What string translation problem are you facing that you're having trouble solving with Stringsdict?Melissiamelita
From my perspective, ICU MessageFormat is the standard implementation of the CLDR (they are both Unicode standards). If Apple decided to use the CLDR plural rules, I would presume they would also include explicit plural rules to make plural syntax more fluent. But of course, I could not confirm this anywhere which leads me to think this is not supported in their implementation,Crissy
I agree that it's almost certainly unsupported. I can't think of any reason to believe that Apple based this format on a Unicode standard. Apple has had localization for decades that weren't tied to ICU (ICU was designed for C++ and Java). Is there a specific translation problem you're having on iOS? I'm certain we can help you solve that.Melissiamelita
I am actually part of a working group with Unicode (on MessageFormat) and we have someone from Apple on this group as well :) CLDR is also what Apple used to build Stringsdict (it's confirmed in their doc). For me, plurals is a well-defined problem (standard by Unicode) and explicit rules should be part of any implementation. Let me add an example in my question of what I would expect Apple to support.Crissy
In your edit, isn't zero exactly what you're describing (i.e. "The format string to use for the number 0")? How is this different than "This is used to match a specific value regardless of the plural categories of the current locale" when the specific value is zero? When you say you can't test this, can you describe what behavior you would expect if the test were to pass vs fail?Melissiamelita
so yeah, I guess @paiv answer the question which is quite strange to me. This means that with Apple's implementation they cannot support Latvian and Prussian unless there is some other logic I don't understand?Crissy
C
2

Short Answer

.stringsdict files have no way to support explicit plural rules (other than a custom Apple implementation of zero which is detailed below)

Detailed Answer

Normal CLDR implementation:

  • All rules that are not in the CLDR for a given language will be ignored
  • If using the rule zero, it will use the CLDR values (most languages have 0 as value for zero). This also includes languages like Latvian who have 20, 30, etc. values mapped to zero and also contradicts Apple's own documentation (this behavior was verified):

If "zero" is present, the value is used for mapping the argument value zero regardless of what CLDR rule specifies for the numeric value.

Source: Foundation Release Notes for OS X v10.9

Custom (Apple) CLDR implementation:

  • All languages can use the zero category from the CLDR even if the rule is not defined for this language (reference here)
  • Presumably, they implemented this to facilitate negative forms of sentences which is a common use case (this can even be found in their examples). For example instead of writing:

You have 0 emails.

You can write:

You have no emails.

  • This is a very common use case but is typically not covered using CLDR categories, it is used by using explicit values. For example, in ICU MessageFormat you can use =0 and not zero for negative forms.
    • While this seems convenient, it creates a big problem, what if you want to use negative forms for Latvian using the zero category? You simply can't - basically Apple broke linguistic rules by overwriting the CLDR.

Complimentary details:

  • There are only two languages in the CLDR where zero does not equal 0:
  • Neither iOS nor macOS is available in the Latvian languages but they support locale settings (keyboard and date formats)
  • This means that there are probably few applications that will support Latvian, unless they have a manual way to change the language inside the application itself (this is a less common scenario for iOS which typically honor the device's settings)

Conclusion

Tip #1: If you need to use Latvian, you should probably avoid using zero for negative forms, and use code instead, with strings outside of the stringsdict file

Tip #2: Make sure that your translation process supports this behavior correctly!

Crissy answered 25/4, 2020 at 13:55 Comment(1)
Just to clarify Latvian support: users can choose Latvian as their preferred language in iOS and macOS and although the system UI will not be localised to Latvian, any app that includes a Latvian localisation will use it without having to include a manual way to change the language in the app (an approach which is unusual for apps on both platforms). I would assume devices sold in Latvia would have Latvian specified as their default language out of the box. However, I also assume Latvian speakers commonly use the majority of their software in a language other than Latvian.Reposeful
A
3

If you are interested in the zero rule only, it is handled in .stringsdict file for any language.

Source: Foundation Release Notes for OS X v10.9

If "zero" is present, the value is used for mapping the argument value zero regardless of what CLDR rule specifies for the numeric value.

Otherwise, these are the only rules handled (depends on language): zero, one, two, few, many, others

Abuttal answered 23/4, 2020 at 21:6 Comment(5)
Wow why would Apple do this? they built their own logic on top of the CLDR? do we know how they can support plurals in Latvian (unicode-org.github.io/cldr-staging/charts/37/supplemental/…) or Prussian (unicode-org.github.io/cldr-staging/charts/37/supplemental/…) where zero can have multiple values?Crissy
@NicolasBouvrette in those languages zero would match CLDR rules. It's English where zero is an extra.Abuttal
How can you confirm? Neither iOS nor macOS is available in the Latvian language so I suspect this probably why they implemented this zero == 0 rule you found in the documentation. It does say "regardless of what CLDR rule specifies"Crissy
That documentation line is not precise. One can start an app with AppleLanguages set to lv_LV to check actual behavior.Abuttal
So you are saying you tested it and it works for Latvian?Crissy
C
2

Short Answer

.stringsdict files have no way to support explicit plural rules (other than a custom Apple implementation of zero which is detailed below)

Detailed Answer

Normal CLDR implementation:

  • All rules that are not in the CLDR for a given language will be ignored
  • If using the rule zero, it will use the CLDR values (most languages have 0 as value for zero). This also includes languages like Latvian who have 20, 30, etc. values mapped to zero and also contradicts Apple's own documentation (this behavior was verified):

If "zero" is present, the value is used for mapping the argument value zero regardless of what CLDR rule specifies for the numeric value.

Source: Foundation Release Notes for OS X v10.9

Custom (Apple) CLDR implementation:

  • All languages can use the zero category from the CLDR even if the rule is not defined for this language (reference here)
  • Presumably, they implemented this to facilitate negative forms of sentences which is a common use case (this can even be found in their examples). For example instead of writing:

You have 0 emails.

You can write:

You have no emails.

  • This is a very common use case but is typically not covered using CLDR categories, it is used by using explicit values. For example, in ICU MessageFormat you can use =0 and not zero for negative forms.
    • While this seems convenient, it creates a big problem, what if you want to use negative forms for Latvian using the zero category? You simply can't - basically Apple broke linguistic rules by overwriting the CLDR.

Complimentary details:

  • There are only two languages in the CLDR where zero does not equal 0:
  • Neither iOS nor macOS is available in the Latvian languages but they support locale settings (keyboard and date formats)
  • This means that there are probably few applications that will support Latvian, unless they have a manual way to change the language inside the application itself (this is a less common scenario for iOS which typically honor the device's settings)

Conclusion

Tip #1: If you need to use Latvian, you should probably avoid using zero for negative forms, and use code instead, with strings outside of the stringsdict file

Tip #2: Make sure that your translation process supports this behavior correctly!

Crissy answered 25/4, 2020 at 13:55 Comment(1)
Just to clarify Latvian support: users can choose Latvian as their preferred language in iOS and macOS and although the system UI will not be localised to Latvian, any app that includes a Latvian localisation will use it without having to include a manual way to change the language in the app (an approach which is unusual for apps on both platforms). I would assume devices sold in Latvia would have Latvian specified as their default language out of the box. However, I also assume Latvian speakers commonly use the majority of their software in a language other than Latvian.Reposeful

© 2022 - 2024 — McMap. All rights reserved.