Before I buy - will I be able to localize lists?

More
7 years 8 months ago #1684 by Sarr
Yup, it is simple and that's ok. So mystery solved for me, now just gotta get used to it :).

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago - 7 years 8 months ago #1707 by Sarr
Hmm, this is strange. I got my text field translated via this modfiied function:
public string ParseAndLocalize( string text ) {
	string[] separators = {",", ".", "!", "?", ";", ":", " "};
	var arrayItems = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);    // divides the text into each element
	var translatedTerms = arrayItems.Select(x=>ScriptLocalization.Get(x)).ToArray();    // translate each of those elements
	return string.Join(", ", translatedTerms);   // for English, it returns "Sword, Chain mail, Shield"
}
It was called just after contents were added to the Text_ch_sh_char_armor_prof_1.text.

Now I'd like to get that translated again every time user changes language. So I tried this:
public new void OnModifyLocalization() {
var translatedTerms = armorProfList.Select(x=>ScriptLocalization.Get(x)).ToArray();
Localize.MainTranslation = string.Join(", ", translatedTerms);   // for English, it returns "Sword, Chain mail, Shield"
}
But didn't work. I also tried adding this line:
Text_ch_sh_char_armor_prof_1.text = ParseAndLocalize( Text_ch_sh_char_armor_prof_1.text );
to maybe run this previous function on each language change. Still doesn't work.

Also, do I need to put this function in separate script and attach it to this text object? Or can I just put it in my main "big" script and reference that object to get acces to that function from it?
Of course I do it via Localize on Text_ch_sh_char_armor_prof_1.text object in Unity Editor. I set Callback to the object containing the function OnModifyLocalization, then choose that function from dropdown. Darn, everything else works now.
Last edit: 7 years 8 months ago by Sarr.

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago #1708 by Frank
The problem you are having is that after you apply the ParseAndLocalize function, you can't apply it again because what's in the text is not longer the terms, but its translations.

You will need to add it to a cache variable and use that instead.

Something like this (if you dont have access here to armorProfList)
(Add a Localize cmp to the text and set a callback that calls this script)
public class CustomCode : MonoBehaviour
{
   private string mCurrentTerms, mCurrentTranslation;

   public string ParseAndLocalize( string text ) 
   {
	string[] separators = {",", ".", "!", "?", ";", ":", " "};
	var arrayItems = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);    // divides the text into each element
	var translatedTerms = arrayItems.Select(x=>ScriptLocalization.Get(x)).ToArray();    // translate each of those elements
	return string.Join(", ", translatedTerms);   // for English, it returns "Sword, Chain mail, Shield"
   } 

public new void OnModifyLocalization() 
{
    // if we just changed languages, then use the previous terms, 
    // otherwise, we changed the value of  Text_ch_sh_char_armor_prof_1.text, so use that as terms

    string terms = (Localize.MainTranslation==mCurrentTranslation) ? mCurrentTerms : Text_ch_sh_char_armor_prof_1.text;

    Localize.MainTranslation = ParseAndLocalize( terms );

    // Save in the cache
    mCurrentTerms = terms;
    mCurrentTranslation = Localize.MainTranslation;
}
}

then, in your code do:
Text_ch_sh_char_armor_prof_1.text = string.Join(", ", armorProfList.Select(x=>ScriptLocalization.Get(x)).ToArray());
Text_ch_sh_char_armor_prof_1.GetComponent<Localize>().OnLocalize(true);  // this will force calling the callback


An even better way If you have access to armorProfList:
public class CustomCode : MonoBehaviour
{
   public new void OnModifyLocalization() 
   {
       Localize.MainTranslation = string.Join(", ", armorProfList.Select(x=>ScriptLocalization.Get(x)).ToArray());
   }
}


Then, in your code, every time you change the armorProfList list, just call
Text_ch_sh_char_armor_prof_1.GetComponent<Localize>().OnLocalize(true);  // this will force calling the callback

Everytime the language is changed, or the object is re-enabled, the callback will also be called automatically.

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 following user(s) said Thank You: Sarr

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago - 7 years 8 months ago #1709 by Sarr
This is strange. I have access to the list, because I inherit from main script (like below). But the field doesn't change when I change language.
Everything else changes fine.
public class CallbackNotifier : CharacterGenerator {

	public void OnModifyLocalization() 
	{
		Localize.MainTranslation = string.Join(", ", armorProfList.Select(x=>ScriptLocalization.Get(x)).ToArray());
	}
}

Also I'd prefer not to put that in different script. Should it work if I reference other object in Localize (callback) and use the callback function from different object for this text object? I have main script attached to canvas, would be better for me there.
Just thought this CallbackNotifier class must be attached as a component to text object, like above, so I did that to check... Still not working
(last 2 attachments are before applying any strings to that obect, default from Unity)
Attachments:
Last edit: 7 years 8 months ago by Sarr.

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago #1710 by Frank

But the field doesn't change when I change language.


Its not calling OnModifyLocalization because the Localize component doesn't have a valid term with a "not empty" translation. (see that your terms shows as "not localized or in a different source")
There is an optimization that whenever there was no translation, it skips doing the callback and parameters.

I removed that constraint and will release a new build (2.6.8a7) later today.

In the meantime, you can make it work by creating a term (any one) and adding some translation to it (anything) and assign that to the localize component. That will bypass the optimization and the callback will write the correct value.

ust thoughy this CallbackNotifier class must be attached as a component to text object


Yes, you can place the callback script in any object, just assign a reference to it. Also, you don't need a separated script for this. If you add the OnModifyLocalization function to your CharacterGenerator, it will work as well.

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 following user(s) said Thank You: Sarr

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago #1711 by Sarr
Great, so thank you again, it works now :). Good to hear about an update too.
Last thing for me to do will be probably inserting variables into translated text, but I think example codes for parameters or callback in I2 Manual show how to do it.
// Find the value of Parameters or return null if not found
     public string GetParameterValue( string ParamName )
     {
           if (ParamName == "WINNER")
               return "Javier";      // You should get this value from your Game Manager
           
           if (ParamName == "NUM PLAYERS")
               return 5.ToString();

           return null; // not found is defined null, not ""
     }
Just with variables instead of "Javier" or 5.ToString(); Or like you showed to some user:
string PlayerColor = LocalizationManager.GetTermTranslation( "Color/" + GameManager.WinningColor );
Localize.MainTranslation = Localize.MainTranslation.Replace("{PLAYER_COLOR}", PlayerColor);
Guess I can handle that :D. Great software, after we finish our projects I'll let you know.

Please Log in or Create an account to join the conversation.

Time to create page: 0.350 seconds
Template by JoomlaShine