Search Unity

Resolved Plural Localization Formatter

Discussion in 'Localization Tools' started by JulianMC, Apr 26, 2021.

  1. JulianMC

    JulianMC

    Joined:
    Dec 30, 2019
    Posts:
    26
    Hi, I've been trying to learn how to use Unity's localization but when I came to learn the Plural Formatter, I can't seem to understand how to use it. I've read the documentation and all and I can't find exemples.

    I have this text: you have "x" seconds to finish.
    In the localization table I have it so it can translate to French and Spanish.
    Exemple: In Spanish it would it says: tienes "x" segundos para terminar.

    I would like to use the plural formatter so when it gets to 1, seconds change to second in all 3 languages, but I don't understand how to.

    How do I use this: upload_2021-4-26_11-38-22.png
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    We are working on improving the Smart String docs at the moment.
    This is the page you need https://docs.unity3d.com/Packages/com.unity.localization@0.11/manual/Pluralization.html

    We also have a simple example of plurals in the quick start guide https://docs.unity3d.com/Packages/c...localization-create-a-string-table-collection

    Those 3 values in Names are the choices you have for the formatter, it means you can specify a plural with plural or p or leave it empty and it will implicitly use the plural formatter.

    So for example your English would look like:

    Code (csharp):
    1. "You have {0:plural:1 second|{} seconds} to finish."
    You need to pass the value into the LocalizedString. You could do it the same way as this although you would need to change the placeholder from 0 to a property or variable name in your script so it can find the value. You could pass the value in directly into the Arguments property. See the Loading Strings examples on how to use LocalizedString https://docs.unity3d.com/Packages/c...localization-create-a-string-table-collection

    E.G

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4.  
    5. namespace UnityEditor.Localization.Samples
    6. {
    7.     public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
    8.     {
    9.         public LocalizedString stringRef = new LocalizedString() { TableReference = "My String Table", TableEntryReference = "Hello World" };
    10.         string m_TranslatedString;
    11.         int m_Score = 0;
    12.  
    13.         void OnEnable()
    14.         {
    15.             stringRef.Arguments = new object[1] { m_Score };
    16.             stringRef.StringChanged += UpdateString;
    17.         }
    18.  
    19.         void OnDisable()
    20.         {
    21.             stringRef.StringChanged -= UpdateString;
    22.         }
    23.  
    24.         public void UpdateScore(int score)
    25.         {
    26.             m_Score = score;
    27.  
    28.             stringRef.Arguments[0] = score;
    29.             stringRef.RefreshString();
    30.         }
    31.  
    32.         void UpdateString(string translatedValue)
    33.         {
    34.             m_TranslatedString = translatedValue;
    35.             Debug.Log("Translated Value Updated: " + translatedValue);
    36.         }
    37.  
    38.         void OnGUI()
    39.         {
    40.             GUILayout.Label(m_TranslatedString);
    41.         }
    42.     }
    43. }
    44.  
    So for English the first value is the singular and the other is the many, Spanish is also a One or Other so would follow a similar pattern.
    Further plural rules can be found here https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html
     
    Last edited: Apr 26, 2021
    JulianMC likes this.
  3. JulianMC

    JulianMC

    Joined:
    Dec 30, 2019
    Posts:
    26
    If I understand well, this code is to use on the Localization Table Editor?

    Code (CSharp):
    1. "You have {0:plural:1 second|{} seconds} to finish."
    EDIT:
    This is the part that I didn't understand, I thought this was only to use inside a script, but now I understand that I have to putt this in the Localization Table Editor and on a script put the variable with the value, in this case it's 0 but I can change that so it can be:

    Code (CSharp):
    1. "You have {timeLeft:plural:1 second|{} seconds} to finish."
    and in my script
    int timeLeft = 3;

    and for other Languages, (Spanish)
    Code (CSharp):
    1. "Tienes {timeLeft:plural:1 segundo|{} segundos} para terminar."

    Thank you so much for your help.
     
    Last edited: Apr 26, 2021
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    Yes that's correct, glad you solved it. The text will be different for each language so has to come from the table however each table can use the placeholders you provide. So essentially it fetches the correct string with placeholders and then applies the formatting.
     
    JulianMC likes this.
  5. RGV

    RGV

    Joined:
    Jan 25, 2016
    Posts:
    48
    How do you use pluralized keys in localize string event without boilerplate code?

    I mean, having this example:
    upload_2023-8-22_11-59-40.png

    plural as variable name is not recognize by localize string event to properly pluralize it.
    Neither does it when marking as smart:
    upload_2023-8-22_12-0-37.png
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,279
    Hi,
    Looks like you are using the 0 index as an argument however, there is no 0 item. The smart format arguments can not be referenced by indexes, they need the variable name. So it would be:

    {plural:p:Competicion|Competiciones}

    You also need to make sure that Smart is toggled on.
     
  7. RGV

    RGV

    Joined:
    Jan 25, 2016
    Posts:
    48
    Ow I understand now why that "0" was there!
    I was dwelling on why "0" and not a name. So silly I didn't recognize it was like an anonym-numered argument. Sorry!

    Thanks on your blink-of-an-eye help! :D
     
    karl_jones likes this.