Dynamic Strings on runtime

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# :slight_smile:

  1. I cannot open Asset Tables because I get following error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)
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)
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)
UnityEditor.Localization.UI.GenericAssetTableListView`2[T1,T2].BuildRoot () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/GenericAssetTableListView.cs:201)
UnityEditor.IMGUI.Controls.TreeView+TreeViewControlDataSource.FetchData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
UnityEditor.IMGUI.Controls.TreeViewDataSource.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
UnityEditor.IMGUI.Controls.TreeView+TreeViewControlDataSource.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
UnityEditor.IMGUI.Controls.TreeViewController.ReloadData () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
UnityEditor.IMGUI.Controls.TreeView.Reload () (at <b43e6d4802d64ea8bbdaa0bf64614d3b>:0)
UnityEditor.Localization.UI.GenericAssetTableListView`2[T1,T2].Initialize () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/GenericAssetTableListView.cs:123)
UnityEditor.Localization.UI.StringTableEditor.OnIMGUI () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Editor/UI/Tables/StringTableEditor.cs:33)
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)
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

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.

Hi Karl,
I will prepare something for you and let you know! Maybe I can also put the bug in the project :wink:

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 :slight_smile:

Best,
Markus

1 Like

Thanks. If you can do that example then it will help a lot, Hopefully a future version can do what you need.

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 :slight_smile: 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

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();,

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.

Can you give me a more code sample using below code so I can understand a little more.thanks

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

Like this

using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public LocalizedString localizedString = new LocalizedString { TableReference = "My Table", TableEntryReference = "SCORE" };
    public Text text;

    void Start()
    {
        localizedString.StringChanged += LocalizedString_StringChanged;
    }

    void LocalizedString_StringChanged(string value)
    {
        Debug.Log(value);
    }

    public void SetScore(int score)
    {
        var operation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My Table", "SCORE", score);
        if (operation.IsDone)
            text.text = operation.Result;
        else
            operation.Completed += t => text.text = t.Result;
    }
}

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?

Ah sorry, we dont have a params object[ ] version. You need to wrap the argument in an IList.

Something like:

GetLocalizedStringAsync("My Table", "SCORE", new object[]{ score });
1 Like