Author Topic: Localization with fonts improvement  (Read 1879 times)

kjenkins_oculus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Localization with fonts improvement
« on: October 14, 2013, 03:53:59 PM »
I saw this post
http://www.tasharen.com/forum/index.php?topic=313.msg1468#msg1468
ArenMook said to use an OnLocalize event to change fonts. However, that is not the best solution since it requires a separate script to implement Localize() with every usage of UILabel. And that script will need to hardcode which font goes with what language. BMFont doesn't have the capability to combine fonts, so every localized game that uses non-English characters is going to run into this problem.

As a suggested alternative, I added a singleton class LocalizationFontMap (attached) where you associate a language resource file with a font prefab.

I then changed UILocalize to use the font specified by LocalizationFontMap in the Localize() function.

Original UILocalize :
  1. if (lbl != null)
  2. {
  3.         // If this is a label used by input, we should localize its default value instead
  4.         UIInput input = NGUITools.FindInParents<UIInput>(lbl.gameObject);
  5.         if (input != null && input.label == lbl) input.defaultText = val;
  6.         else lbl.text = val;
  7. }
  8. else if (sp != null)
  9. {
  10.         sp.spriteName = val;
  11.         sp.MakePixelPerfect();
  12. }
  13.  

Changed UILocalize:
  1. UIFont newFont = null;
  2. if (LocalizationFontMap.instance != null)
  3. {
  4.         newFont = LocalizationFontMap.instance.GetCurrentFont();
  5. }      
  6.  
  7. if (lbl != null)
  8. {
  9.         // If this is a label used by input, we should localize its default value instead
  10.         UIInput input = NGUITools.FindInParents<UIInput>(lbl.gameObject);
  11.         if (input != null && input.label == lbl)
  12.         {
  13.                 input.defaultText = val;
  14.         }
  15.         else
  16.         {
  17.                 if (newFont != null)
  18.                         lbl.font = newFont;
  19.                 lbl.text = val;
  20.         }
  21. }
  22. else if (sp != null)
  23. {
  24.         sp.spriteName = val;
  25.         sp.MakePixelPerfect();
  26. }
  27.  

Let me know if you have suggestions for improvement.