Search Unity

Text Changed Updated event

Discussion in 'Localization Tools' started by OsamaDeep, Oct 6, 2020.

  1. OsamaDeep

    OsamaDeep

    Joined:
    Jun 22, 2013
    Posts:
    20
    hello, is there any event or place that i can use to add extra line of code when Text get the localized new string?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Hi,
    You want to append text to the Localized string or you want to do something when the value changes?
    How are you localizing the strings, are you using a script or using the LocalizedStringEvent component we provide?
     
  3. OsamaDeep

    OsamaDeep

    Joined:
    Jun 22, 2013
    Posts:
    20
    we have problem to localize RTL languages like Arabic, so after i grab the Text from table i need to fix it since unity dosent suppoer RTL until now, so i need to know when the string ready after i get it from table, then i will change it and back it to the Text field.
    edit: and yes i used the Event provided in 0.8+.
     

    Attached Files:

  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Ok. You could change the target of the Update String event to a script or you could write a script to do everything in 1.
    Take a look at the Samples we include with the package(In the package manager window).

    Try the sample `LocalizedStringWithChangeHandlerExample` under the LoadingStrings category.
    In UpdateString modify the translated value and then set it to the text component.

    Here is an example

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4. using UnityEngine.UI;
    5.  
    6. namespace UnityEditor.Localization.Samples
    7. {
    8.     public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
    9.     {
    10.         public Text text;
    11.  
    12.         public LocalizedString stringRef = new LocalizedString() { TableReference = "My String Table", TableEntryReference = "Hello World" };
    13.         string m_TranslatedString;
    14.  
    15.         void OnEnable()
    16.         {
    17.             stringRef.StringChanged += UpdateString;
    18.         }
    19.  
    20.         void OnDisable()
    21.         {
    22.             stringRef.StringChanged -= UpdateString;
    23.         }
    24.  
    25.         void UpdateString(string translatedValue)
    26.         {
    27.             m_TranslatedString = translatedValue;
    28.  
    29.             // Do something here
    30.  
    31.             text.text = m_TranslatedString;
    32.         }
    33.     }
    34. }
    35.  
    I would also be interested in what your RTL fix is, maybe its something we could incorporate.
     
    wardetta likes this.
  5. OsamaDeep

    OsamaDeep

    Joined:
    Jun 22, 2013
    Posts:
    20
    Thank you for the example,
    Arabic Localization is a pain for Us, since Unity does not support RTL as i mention before, so our community start doing open source support, the problem is:
    - Arabic language start from Right to left
    - Arabic characters has different form when came between 2 char like this : ب if it come between 2 letters it will be like ـبـ and if it came at the end its form like ـب .

    there fore right now there is Core support for the issue above, here the source code:
    https://github.com/Konash/arabic-su...Assets/ArabicSupport/Scripts/ArabicSupport.cs

    but this has another problem, because its flip all characters, so index of first character will equal to index of last character, and if you have statement in only 1 line will be fine, but when it come to mutlilines it show first line below the 2nd line, here come the 2nd tool thats fix the multiline issue:
    https://github.com/AbdullahAlimam/A...l-Imam/Arabic Support UI/Script/ArabicText.cs

    the previous 2 tools, does not support TextMesh Pro, thats enforce me to do that, just updated some lines and make it support normal text and TMP text , inputfield , and TMP inputfield.

    Code (CSharp):
    1.                 case whichType.TMPText:
    2.                     tmpTextComponent.text = "";
    3.  
    4.                     for (int lineIndex = 0; lineIndex < rtlParagraph.Length; lineIndex++)
    5.                     {
    6.                         string[] words = rtlParagraph[lineIndex].Split(' ');
    7.                         System.Array.Reverse(words);
    8.                         tmpTextComponent.text = string.Join(" ", words);
    9.                         Canvas.ForceUpdateCanvases();
    10.                         for (int i = 0; i < tmpTextComponent.textInfo.lineCount; i++)
    11.                         {
    12.                             int startIndex = tmpTextComponent.textInfo.lineInfo[i].firstCharacterIndex;
    13.                             int endIndex = (i == tmpTextComponent.textInfo.lineCount - 1) ? tmpTextComponent.text.Length
    14.                                 : tmpTextComponent.textInfo.lineInfo[i+1].firstCharacterIndex;
    15.                             int length = endIndex - startIndex;
    16.                             string[] lineWords = tmpTextComponent.text.Substring(startIndex, length).Split(' ');
    17.                             System.Array.Reverse(lineWords);
    18.                             finalText = finalText + string.Join(" ", lineWords).Trim() + "\n";
    19.                         }
    20.                     }
    all files attached.
    if you can help us with this it will be great. and if you need more information, i am happy to help.
     

    Attached Files:

    jGate99 likes this.
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Thanks!
    Arabic is an issue we are aware of and its certainly something we plan to address in the future.
    Ill record what you said so we can look further into it when we begin the work adding suport to the Localization package.
     
    OsamaDeep likes this.