Welcome, Guest
Username: Password: Remember me

TOPIC: How to handle plurals in script

How to handle plurals in script 4 months 3 weeks ago #3054

I'm aware of setting up translations with plurals through the Localize component, however, I need the translations to happen through scripting. I know how to use baked terms but I'm unsure how to do it with plurals. For example, I need to be able to translate the phrase "FIGHT IN 5 CAREER OR ONLINE MATCHES" where '5' is the variable.

Example Code:
int amount = 5;
example.text = "FIGHT IN " + amount + " CAREER OR ONLINE MATCHES";

How would I go about localizing this string of text into other languages where the amount can be any number? I've looked through the documentation but still not understanding it fully.
Last Edit: 4 months 3 weeks ago by koalitygame.
The administrator has disabled public write access.

How to handle plurals in script 4 months 3 weeks ago #3055

  • Frank
  • Frank's Avatar
  • Offline
  • Administrator
  • Posts: 1129
  • Thank you received: 241
  • Karma: 70
Hi,
For that, you need to use the same setup to define the plurals in a term, just add the term to the I2Languages.prefab and set its plural forms
inter-illusion.com/assets/I2LocalizationManual/Plurals.html

Then, you need to change the number in the translation by a Parameter.
inter-illusion.com/assets/I2LocalizationManual/Parameters.html
For example:
Plural: "FIGHT IN {[MATCHES_LEFT]} CAREERS OR ONLINE MATCHES"
One: "FIGHT IN {[MATCHES_LEFT]} CAREER OR ONLINE MATCH"
Zero: "NO CAREERS OR MATCHES LEFT"

Then, in your script, you ask for the translation of that term with the apply parameters enable:
string translation = LocalizationManager.GetTranslation( "TERM_FIGHTS", applyParameters:true )

That will get the translation of the term TERM_FIGHTS into the current language, will get the value of MATCHES_LEFT and findout what plural form should be used, and then replace MATCHES_LEFT with its value.

That code will use the ParameterManager if you have any of those components in your scene.
If you want to pass the values directly, you can do it as well:
var dict = new Dictionary<string, int>();
dict["MATCHES_LEFT"] = 5;
string translation = LocalizationManager.GetTranslation( "TERM_FIGHTS" )
LocalizationManager.ApplyParameters(ref translation, dict);

or even:
string translation = LocalizationManager.GetTranslation( "TERM_FIGHTS" )
LocalizationManager.ApplyParameters(ref translation, (p)=>{ return p=="MATCHES_LEFT" ? 5 : null; });

Hope that helps,
Frank
Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
The administrator has disabled public write access.

How to handle plurals in script 4 months 2 weeks ago #3073

Thanks for the help, Frank. I got it working although I couldn't find a method named ApplyParameters, instead it was ApplyLocalizationParams. I also couldn't get the method to work with a dictionary value, so I just passed in the translation and used a GlobalParameters script to change the variables.

I did run into a major issue, however, when building the game using IL2CPP. The game crashes right after the startup screen and looking through the logs it gives me a fatal error with this line:

LocalizationParamsManager_t2237076064::get__AutoRegister_3() const

The game works perfectly when building through Mono. I think it may have something to do with upgrading Unity from 2017 to 2018 but I'm not really sure. Any help is appreciated!
The administrator has disabled public write access.

How to handle plurals in script 4 months 2 weeks ago #3074

  • Frank
  • Frank's Avatar
  • Offline
  • Administrator
  • Posts: 1129
  • Thank you received: 241
  • Karma: 70
Are you in the latest version of the plugin?
The AutoRegister was removed recently.
If you are in the latest version and still getting that error, please let me know.

Hope that helps,
Frank
Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
The administrator has disabled public write access.

How to handle plurals in script 4 months 2 weeks ago #3075

I was pretty sure I updated it to the latest version a few days ago, but I just checked and I was still on an older version. It's now updated and I'm exporting a new build. I'll let you know if that fixes the issue, thanks!
The administrator has disabled public write access.

How to handle plurals in script 4 months 2 weeks ago #3076

Updating fixed the issue. Thanks for the help, Frank!
The administrator has disabled public write access.
Time to create page: 0.164 seconds
Colors