Unity localization (multilingual) plugin Localization

Game localization mainly means that text and resources (pictures, etc.) change with the language. For example, some artistic words in the game use pictures, and different pictures must be loaded according to the language.

install, configure

insert image description here
Localization is a localization plug-in officially launched by unity. It is installed through PackageManager.
insert image description here
Before using it, you need to create a localization configuration. Edit > Project Settings > Localization to generate a general configuration file.
insert image description here
Locale Generator is used to add or remove languages. Every time a language is added, it will also Generate the corresponding configuration file, and then select the default language configuration file, as shown above

create form

insert image description here
Window > Asset Management > Localization Tables Create a table to establish the correspondence between different resources, that is, a key corresponds to resources in multiple languages. You can choose to create a text table or a resource table. After writing the table name, you can create it

Text table (String Table)

insert image description here

insert image description here
Here we first demonstrate with a text table, create a table named MyStringTable and add a TextMeshPro to the String Table UI, right-click and select Localize
insert image description here
to automatically add a script, specify the table name and Entry Name (that is, the key in the table), and then hang it in the scene script to test

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;

public class TestLocalization : MonoBehaviour
{
    
    
    AsyncOperationHandle m_InitializeOperation;
    private Locale _chineseLocale;
    private Locale _englishLocale;

    void Start()
    {
    
    
        // SelectedLocaleAsync will ensure that the locales have been initialized and a locale has been selected.
        m_InitializeOperation = LocalizationSettings.SelectedLocaleAsync;
        if (m_InitializeOperation.IsDone)
        {
    
    
            InitializeCompleted(m_InitializeOperation);
        }
        else
        {
    
    
            m_InitializeOperation.Completed += InitializeCompleted;
        }
    }

    void InitializeCompleted(AsyncOperationHandle obj)
    {
    
    
        var locales = LocalizationSettings.AvailableLocales.Locales;
        for (int i = 0; i < locales.Count; ++i)
        {
    
    
            var locale = locales[i];
            if (locale.LocaleName == "Chinese (Simplified) (zh-Hans)")
            {
    
    
                _chineseLocale = locale;
            }
            else if (locale.LocaleName == "English (en)")
            {
    
    
                _englishLocale = locale;
            }
        }
    }

    private void OnGUI()
    {
    
    
        if (GUI.Button(new Rect(0, 0, 100, 50),"中文"))
        {
    
    
            LocalizationSettings.Instance.SetSelectedLocale(_chineseLocale);
        }
        
        if (GUI.Button(new Rect(0, 60, 100, 50),"英文"))
        {
    
    
            LocalizationSettings.Instance.SetSelectedLocale(_englishLocale);
        }
    }
}

insert image description here
You can change the language by modifying the Locale at runtime. This method is suitable for static text on the interface, and you can also obtain the corresponding text in the code. You need to specify the table name and key, as follows

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;

public class TestLocalization2 : MonoBehaviour
{
    
    
    public TextMeshProUGUI text;
    
    void Start()
    {
    
    
        StartCoroutine(LoadStrings());
    }
    
    IEnumerator LoadStrings()
    {
    
    
        // A string table may not be immediately available such as during initialization of the localization system or when a table has not been loaded yet.
        var loadingOperation = LocalizationSettings.StringDatabase.GetTableAsync("MyStringTable");
        yield return loadingOperation;

        if (loadingOperation.Status == AsyncOperationStatus.Succeeded)
        {
    
    
            var stringTable = loadingOperation.Result;
            text.text = stringTable.GetEntry("id_hello").GetLocalizedString();
        }
        else
        {
    
    
            Debug.LogError("Could not load String Table\n" + loadingOperation.OperationException.ToString());
        }
    }
}

Or get the text table through the table name during initialization, and then get the string from the text table

using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;

public class TestLocalization3 : MonoBehaviour
{
    
    
    public LocalizedStringTable stringTable = new LocalizedStringTable {
    
     TableReference = "MyStringTable" };
    string m_TranslatedStringHello;

    void OnEnable()
    {
    
    
        stringTable.TableChanged += LoadStrings;
    }

    void OnDisable()
    {
    
    
        stringTable.TableChanged -= LoadStrings;
    }

    void LoadStrings(StringTable stringTable)
    {
    
    
        m_TranslatedStringHello = GetLocalizedString(stringTable, "id_hello");
    }

    static string GetLocalizedString(StringTable table, string entryName)
    {
    
    
        var entry = table.GetEntry(entryName);
        return entry.GetLocalizedString();
    }

    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_TranslatedStringHello);
    }
}

insert image description here
Under Window > Asset Management > Localization Scene Controls
editor, we can modify the current Locale through Localization Scene Controls, which is convenient for debugging.
If the amount of text in the game is not very large, it can be managed through the text table of Localization. If the amount of text is large, it is recommended Manage through Excel, write a manager to load text

Resource Table (Asset Table)

insert image description here
The logic of the resource table is similar, first create a MyAssetTable, add test data, because the resource item is Object type, so any resource is supported, add two sprites here, add an
insert image description here
Image on the UI, right-click and select Localize, automatically add the script, specify the table name and key, you can test it.
insert image description here
This applies to static images on the interface. Raw Image and Audio Source components also support right-clicking to select Localize

expand

I want the font of TextMeshPro to change with the language. Localization also provides interface support for expansion. First, create three scripts

using System;

namespace UnityEngine.Localization
{
    
    
    [Serializable]
    public class LocalizedTmpFont : LocalizedAsset<TMPro.TMP_FontAsset>
    {
    
    
        
    }
}
using System;
using UnityEngine.Events;

namespace UnityEngine.Localization.Events
{
    
    
    [Serializable]
    public class UnityEventTmpFont : UnityEvent<TMPro.TMP_FontAsset>
    {
    
    
    }
}
using TMPro;
using UnityEngine.Localization.Events;

namespace UnityEngine.Localization.Components
{
    
    
    [AddComponentMenu("Localization/Asset/Localize TmpFont Event")]
    public class LocalizeTmpFontEvent : LocalizedAssetEvent<TMPro.TMP_FontAsset, LocalizedTmpFont, UnityEventTmpFont>
    {
    
    
        private TextMeshProUGUI text;
        private void Awake()
        {
    
    
            text = GetComponent<TextMeshProUGUI>();
        }

        protected override void UpdateAsset(TMP_FontAsset localizedAsset)
        {
    
    
            base.UpdateAsset(localizedAsset);
            text.font = localizedAsset;
        }
    }
}

insert image description here
Also add the desired font to the resource table
insert image description here
Finally, you can hang the LocalizeTmpFontEvent script on TextMeshPro, specify the table name and id
insert image description here

Guess you like

Origin blog.csdn.net/sinat_34014668/article/details/127471573