I have a menu to select a character between 4 types.
When a character is selected, a text pannel is updated to add informations.
I want to update this pannel with localization. I know how to do it withtout the new localization package, but what I want to do is “write this specific localization key here”.
The localization key may varie for each character so I will make a “variable variable” to seperate :
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Localization;
public class SelectCharacterEvent : MonoBehaviour, ISelectHandler
{
public string CharacNameIDUI;
public Text CharacSelectionName;
public void OnSelect(BaseEventData eventData)
{
Debug.Log("I'm selected" + CharacNameIDUI);
CharacSelectionName.text = UnityEngine.Localization.Tables.DetailedLocalizationTable<TTable, Tentry>;
}
}
I know how to change text with localization, but in this case it’s more complicated because the text depend on event.
I’m not entirely sure I understand but it sounds like you want to change the Key for a LocalizedString at runtime.
This is fine. The Samples contain various ways to work with LocalizedStrings, this is a modified version that may do what you need:
public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
{
public LocalizedString stringRef = new LocalizedString() { TableReference = "My String Table", TableEntryReference = "Character_TypeKnight_Name" };
public string CharacNameIDUI;
public Text CharacSelectionName;
void OnEnable()
{
stringRef.StringChanged += UpdateString;
}
void OnDisable()
{
stringRef.StringChanged -= UpdateString;
}
void UpdateString(string translatedValue)
{
CharacSelectionName.text = translatedValue;
}
public void OnSelect(BaseEventData eventData)
{
Debug.Log("I'm selected" + CharacNameIDUI);
// Change the Key here. This will trigger an update which will call UpdateString with the new translated value
stringRef.TableEntryReference = CharacNameIDUI;
}
}
There is 2 character playable : a Magician, a Knight. The player must do a choice.
On my UI there is 2 images that represent a Magician, and a Knight, they can be selected.
I have ONE character_nametype_ui that display the choice before confirmation, and is updated when I select Magician or Knight (i.e : “Will you play a Magicial ?”).
All I want, is that when someone click on the Magician’s Image, the character_nametype_ui update with the magician name, but I want to localize that as well. I can change the character_nametype_ui content, but how do I do it with localization ?
In my “Table Collection Name” : UI_Text I have these entries :
You can get a StringTable and then query the Keys and Key Ids.
See the LocalizedStringTableExample in the Samples.
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
namespace UnityEditor.Localization.Samples
{
/// <summary>
/// This example shows how a <see cref="StringTable"> can be used directly in order to get translated
/// strings for multiple entries using a <see cref="LocalizedStringTable>"/>.
/// </summary>
public class LocalizedStringTableExample : MonoBehaviour
{
public LocalizedStringTable stringTable = new LocalizedStringTable { TableReference = "My Strings" };
// We will cache our translated strings
string m_TranslatedStringHello;
string m_TranslatedStringGoodbye;
string m_TranslatedStringThisIsATest;
void OnEnable()
{
stringTable.TableChanged += LoadStrings;
}
void OnDisable()
{
stringTable.TableChanged -= LoadStrings;
}
void LoadStrings(StringTable stringTable)
{
m_TranslatedStringHello = GetLocalizedString(stringTable, "Hello");
m_TranslatedStringGoodbye = GetLocalizedString(stringTable, "Goodbye");
m_TranslatedStringThisIsATest = GetLocalizedString(stringTable, "This is a test");
}
static string GetLocalizedString(StringTable table, string entryName)
{
// Get the table entry. The entry contains the localized string and Metadata
var entry = table.GetEntry(entryName);
return entry.GetLocalizedString(); // We can pass in optional arguments for Smart Format or String.Format here.
}
void OnGUI()
{
// We can check if the localization system is ready using the InitializationOperation.
// Initialization involves loading locales and optionally preloading localized data for the current locale.
if (!LocalizationSettings.InitializationOperation.IsDone)
{
GUILayout.Label("Initializing Localization");
return;
}
GUILayout.Label(m_TranslatedStringThisIsATest);
GUILayout.Label(m_TranslatedStringHello);
GUILayout.Label(m_TranslatedStringGoodbye);
}
}
}
Once you have the table you can access its SharedData property which contains all the keys and ids.
You can also use LocalizationSettings.StringDatabase.GetTableAsync to get the table instead of LocalizationSettings.StringDatabase.GetTableAsync
Similar but by using an AssetTable.
Instead of calling GetLocalizedString on the entry you would call LocalizationSettings.AssetDatabase
LoadAssetAsync(table name, EntryId)