Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Dynamic Strings on runtime

Discussion in 'Localization Tools' started by SomehowLuke, Nov 7, 2019.

  1. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hi, I was trying out your new localize package and ran into some issues I do not know how to solve.

    1. I have some problems with dynamic strings. What I want to achieve:
    • Lets say I have several zodiacs on my screen.
    • On the bottom of my screen I have a text field that shows the name of the current zodiac.
    • A zodiac of course has also to be translated.
    • Each Zodiac item has an specific id.
    • What I already have is the id of the current selected zodiac and currently I write the id into the text field.
    • Now I want to use that Id to automatically translate it to the correct name in my selected language and of course I want to have functionality that your localization brings in (auto update on switching languages etc)
    • What I have tried so far is using a LocalizedString that has a standard table/entry name set in the inspector and I wanted to change the entry name dynamically using the zodiac id (string that matches the keys in my table) => did not work or I do not know how
    • I added a TableEntryReference to each zodiac and once the selection changes it catches the current selected zodiac and its TableEntryReference, then I changed the TableEntryReference of my LocalizedString and tried a RefreshString() but it still had its old standard reference I have set in the inspector, so I have tried to call GetLocalizedString() which got the correct string and I was able to set the text field but after I switch the language and updateString got called it changed back to the old string reference.
    How is the best way to solve that in C# :)

    2. I cannot open Asset Tables because I get following error:

    Code (CSharp):
    1. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    2. Parameter name: index
    3. System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0)
    4. System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
    5. System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)
    6. UnityEditor.Localization.UI.StringTableTreeViewItem.Initialize (System.Collections.Generic.List`1[T] tables, System.Int32 startIdx) (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/StringTableTreeViewItem.cs:19)
    7. UnityEditor.Localization.UI.GenericAssetTableListView`2[T1,T2].CreateTreeViewItem (System.Int32 index, UnityEngine.Localization.Tables.KeyDatabase+KeyDatabaseEntry keyEntry) (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/GenericAssetTableListView.cs:171)
    8. UnityEditor.Localization.UI.GenericAssetTableListView`2[T1,T2].BuildRoot () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/GenericAssetTableListView.cs:201)
    9. UnityEditor.IMGUI.Controls.TreeView+TreeViewControlDataSource.FetchData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
    10. UnityEditor.IMGUI.Controls.TreeViewDataSource.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
    11. UnityEditor.IMGUI.Controls.TreeView+TreeViewControlDataSource.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
    12. UnityEditor.IMGUI.Controls.TreeViewController.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
    13. UnityEditor.IMGUI.Controls.TreeView.Reload () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
    14. UnityEditor.Localization.UI.GenericAssetTableListView`2[T1,T2].Initialize () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/GenericAssetTableListView.cs:123)
    15. UnityEditor.Localization.UI.StringTableEditor.OnIMGUI () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/StringTableEditor.cs:33)
    16. UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Boolean canAffectFocus) (at <b256487a5e4140809d026a943f161bc2>:0)
    17. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    But if I use for example TextMesh => Localize I am able to add new entries to my created table and also able to select these created entries.


    Hope this explains what I want to achieve.
    Thanks in advance and best regards!

    Luke
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Hi Luke,

    Ok I think I understand but are you able to provide a simple example of how you have your zodiacs setup?
    Is the zodiac Id using the Localization Id or your own system?

    Thanks for the bug report, ill take a look. Can you tell me how I could create this error locally? It would help make sure the issue is fixed.
     
  3. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hi Karl,
    I will prepare something for you and let you know! Maybe I can also put the bug in the project ;-)

    I had to remove the package of my current zodiac project because I was not able to work with it.I like the concept but of course its a preview :)

    Best,
    Markus
     
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Thanks. If you can do that example then it will help a lot, Hopefully a future version can do what you need.
     
  5. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hi, I did not forget about my promise but I was not able to create a sample project so far. I will be able to create one mid of december because till then time does not allow it :) I will post it here once I have it and I will make sure to use the current update you have just shared. Thanks and best,
    Markus
     
  6. ravianand

    ravianand

    Joined:
    Jan 21, 2020
    Posts:
    8
    hi i am using the localization package to translate my app, but i have not yet understand how to get score text and score working. can someone help me?





    scoreText.text = "Score: " + score.ToString();,
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    You would want to put the value into the translated string.

    "Score: {0}" and then pass the score in as an argument so it can be formatted correctly.
     
  8. ravianand

    ravianand

    Joined:
    Jan 21, 2020
    Posts:
    8
     
  9. ravianand

    ravianand

    Joined:
    Jan 21, 2020
    Posts:
    8
    Can you give me a more code sample using below code so I can understand a little more.thanks


    scoreText.text = "Score: " + score.ToString();,
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Like this

    upload_2021-1-8_23-9-16.png

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4. using UnityEngine.Localization.Settings;
    5. using UnityEngine.UI;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour
    8. {
    9.     public LocalizedString localizedString = new LocalizedString { TableReference = "My Table", TableEntryReference = "SCORE" };
    10.     public Text text;
    11.  
    12.     void Start()
    13.     {
    14.         localizedString.StringChanged += LocalizedString_StringChanged;
    15.     }
    16.  
    17.     void LocalizedString_StringChanged(string value)
    18.     {
    19.         Debug.Log(value);
    20.     }
    21.  
    22.     public void SetScore(int score)
    23.     {
    24.         var operation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My Table", "SCORE", score);
    25.         if (operation.IsDone)
    26.             text.text = operation.Result;
    27.         else
    28.             operation.Completed += t => text.text = t.Result;
    29.     }
    30. }
    31.  
     
  11. Robert_Luminous

    Robert_Luminous

    Joined:
    May 21, 2019
    Posts:
    5
    This line of code is throwing errors for me as StringDatabase does not appear to have any overload of GetLocalizedStringAsync with these parameters being acceptable. Am I missing something or is it because I am on Localization package version 1.3.2?
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Ah sorry, we dont have a
    params object[]
    version. You need to wrap the argument in an IList.

    Something like:

    Code (csharp):
    1. GetLocalizedStringAsync("My Table", "SCORE", new object[]{ score });
     
    Robert_Luminous likes this.