Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Localization - How to get a localized string in a script? (prefabs)

Discussion in 'Scripting' started by ZoidbergForPresident, Jan 24, 2021.

  1. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    As in the title.

    I checked the doc:https://docs.unity3d.com/Packages/com.unity.localization@0.4/manual/

    But LocalizedAssetReference isn't recognized in code.

    I want to use localized Strings in a prefab, I originally used ScriptableObjects to get the adequate text but Localization seems way nicer to have, but I have that interrogation concerning prefabs.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Looks like that API is at 0.4, which is preview (anything below 1.0) and means it is subject to change.

    For something as trivial as injecting localized text, I would just make my own, probably by importing a CSV file and injecting it wherever Text appears in game. The actual hard work isn't so much in the injection as it is in getting it all set up to happen seamlessly throughout your project, and the API isn't really much help there.
     
  3. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Yeah, I found the most recent one.

    I've got another issue, I updated my scriptable objects with the adequate localized strings and put the code (from localized string example in doc) to update the string when language is changed.

    But now when I launch the game I get an error:

    ArgumentNullException: Value cannot be null.
    Parameter name: locale
    UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].GetTableAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Locale locale) (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Settings/Database/LocalizedDatabase.cs:128)
    UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].GetTableEntryAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale) (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Settings/Database/LocalizedDatabase.cs:165)
    UnityEngine.Localization.Settings.LocalizedDatabase`2[TTable,TEntry].GetTableEntryAsync (UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference) (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Settings/Database/LocalizedDatabase.cs:184)
    UnityEngine.Localization.LocalizedString.HandleLocaleChange (UnityEngine.Localization.Locale _) (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Localized Reference/LocalizedString.cs:218)
    UnityEngine.Localization.LocalizedString.ForceUpdate () (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Localized Reference/LocalizedString.cs:203)
    UnityEngine.Localization.LocalizedString.add_StringChanged (UnityEngine.Localization.LocalizedString+ChangeHandler value) (at Library/PackageCache/com.unity.localization@0.8.1-preview/Runtime/Localized Reference/LocalizedString.cs:98)
    BoxData.OnEnable () (at Assets/Scripts/ScriptableObjects/BoxData.cs:23)

    Apparently the locale is set to null. I've put code waiting for the localization to be ready in said ScriptableObjects (
    yield return LocalizationSettings.InitializationOperation;)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.