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

Bug LocalizationTableCollection could not be found on build

Discussion in 'Localization Tools' started by flippi273, Jun 29, 2021.

  1. flippi273

    flippi273

    Joined:
    Nov 17, 2018
    Posts:
    23
    In one of our scripts we have the following variable defined

    Code (CSharp):
    1. [SerializeField] LocalizationTableCollection localizationTable;
    When playing in the Unity Editor, everything works as intended, but when trying to build and run the game, we get the following error:


    I've seen some things posted about setting up an Assembly Definition file (asmdef), but we are not using one.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    That's an editor only class. What are you trying to do with the collection?
     
    flippi273 likes this.
  3. darthmorf

    darthmorf

    Joined:
    Jun 21, 2017
    Posts:
    11
    Hi, I'm a dev with flippi273 on the same project. Below is what we're using the class for. It's essentially a custom script that we place on misc gui text elements in order to have them make use of localisation, using a custom system to detect localised words.

    Code (CSharp):
    1.  public class LocalizationTextReplacer : MonoBehaviour
    2.     {
    3.         // Config Params
    4.         [SerializeField] LocalizationTableCollection localizationTable;
    5.  
    6.         // Cached Components
    7.         TextMeshProUGUI textMesh;
    8.  
    9.         // State
    10.         string originalText;
    11.  
    12.         private void Awake()
    13.         {
    14.             try
    15.             {
    16.                 textMesh = gameObject.GetComponent<TextMeshProUGUI>();
    17.             }
    18.             catch
    19.             {
    20.                 Debug.LogWarning("Localization Text Replacer assigned to element with no TextMeshProUGUI!");
    21.             }
    22.  
    23.             originalText = textMesh.text;
    24.  
    25.             UpdateLocalization();
    26.         }
    27.  
    28.         private void Start()
    29.         {
    30.             GameObject vOptionsMenu = GameObject.FindGameObjectWithTag(TagManager.OPTIONS_MENU_TAG);
    31.  
    32.             // Add listener to update text when localization changes
    33.             vOptionsMenu.GetComponent<LocalizationController>().GetOnLocaleChangedEvent().AddListener(UpdateLocalization);
    34.         }
    35.  
    36.         private void UpdateLocalization()
    37.         {
    38.             textMesh.SetText(LocalizationManager.ApplyLocalization(localizationTable.name, originalText));
    39.         }
    40.     }
    41.  
    42. public static class LocalizationManager
    43.     {
    44.         public static string ApplyLocalization(string pLocalizationTableName, string pStringToLocalize)
    45.         {
    46.             // Regex matches pairs of { } ignoring nested {} pairs
    47.             foreach (Match vMatch in Regex.Matches(pStringToLocalize, "\\{((?>[^{}]+|\\{(?<n>)|\\}(?<-n>))+(?(n)(?!)))\\}"))
    48.             {
    49.                 string vStrippedMatch = vMatch.Value.Replace("{", "").Replace("}", "");
    50.                 string vLocalizedString = LocalizationSettings.StringDatabase.GetLocalizedString(pLocalizationTableName, vStrippedMatch);
    51.  
    52.                 pStringToLocalize = pStringToLocalize.Replace(vMatch.Value, vLocalizedString);
    53.             }
    54.  
    55.             return pStringToLocalize;
    56.         }
    57.     }
    58.  
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Ok I see. The LocalizationTableCollection is an editor only class, its there to provide an Editor API so you can configure a project but does not work in the player. In the Player we provide multiple ways to get a localized string. We have some sample code in the package, you can get to it via the Package Manager window.
    For your code it looks like you just want to change LocalizationTableCollection to be a LocalizedStringTable.

    Your code would probably look something like this:
    Code (csharp):
    1. public class LocalizationTextReplacer : MonoBehaviour
    2.     {
    3.         // Config Params
    4.         [SerializeField] LocalizedStringTable localizationTable;
    5.  
    6.         // Cached Components
    7.         TextMeshProUGUI textMesh;
    8.  
    9.         // State
    10.         string originalText;
    11.  
    12.         private void Awake()
    13.         {
    14.             try
    15.             {
    16.                 textMesh = gameObject.GetComponent<TextMeshProUGUI>();
    17.             }
    18.             catch
    19.             {
    20.                 Debug.LogWarning("Localization Text Replacer assigned to element with no TextMeshProUGUI!");
    21.             }
    22.  
    23.             originalText = textMesh.text;
    24.  
    25.             UpdateLocalization();
    26.         }
    27.  
    28.         private void Start()
    29.         {
    30.             GameObject vOptionsMenu = GameObject.FindGameObjectWithTag(TagManager.OPTIONS_MENU_TAG);
    31.  
    32.             // Add listener to update text when localization changes
    33.             vOptionsMenu.GetComponent<LocalizationController>().GetOnLocaleChangedEvent().AddListener(UpdateLocalization);
    34.         }
    35.  
    36.         private void UpdateLocalization()
    37.         {
    38.             textMesh.SetText(LocalizationManager.ApplyLocalization(localizationTable.TableReference, originalText));
    39.         }
    40.     }
    41.  
    42. public static class LocalizationManager
    43.     {
    44.         public static string ApplyLocalization(TableReference  pLocalizationTableName, string pStringToLocalize)
    45.         {
    46.             // Regex matches pairs of { } ignoring nested {} pairs
    47.             foreach (Match vMatch in Regex.Matches(pStringToLocalize, "\\{((?>[^{}]+|\\{(?<n>)|\\}(?<-n>))+(?(n)(?!)))\\}"))
    48.             {
    49.                 string vStrippedMatch = vMatch.Value.Replace("{", "").Replace("}", "");
    50.                 string vLocalizedString = LocalizationSettings.StringDatabase.GetLocalizedString(pLocalizationTableName, vStrippedMatch);
    51.  
    52.                 pStringToLocalize = pStringToLocalize.Replace(vMatch.Value, vLocalizedString);
    53.             }
    54.  
    55.             return pStringToLocalize;
    56.         }
    57.     }
     
    flippi273 likes this.
  5. darthmorf

    darthmorf

    Joined:
    Jun 21, 2017
    Posts:
    11
    Thank you! That has gotten rid of that one. Not sure how I managed to get those two classes mixed up.
    We're also getting this:
    Assets\Scripts\Localization\LocalizationTextReplacer.cs(6,19): error CS0234: The type or namespace name 'Localization' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)


    I think it's the same as this person here but unfortunatley they do not mention how they add the 'Localization asmdef'.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Do you have
    Code (csharp):
    1. using UnityEditor.Localization;
    at the top? You should remove this line.
     
    flippi273 likes this.
  7. darthmorf

    darthmorf

    Joined:
    Jun 21, 2017
    Posts:
    11
    Oh wow, I feel a little dumb there haha - coffee hasn't quite kicked in. Thanks so much for your help, and the speed at which you gave it!
     
    karl_jones likes this.