Unity implements multilingual localization UGUI multilingual

1. First create two txt files under Resources. I named them en.txt and zh.txt here, one in English and one in Chinese

It doesn’t have to be placed under Resources. For the convenience of loading here, it can also be placed in other places that can be loaded.

The content of the txt file is shown in the figure.  Note that the encoding format must be UTF-8, otherwise there may be Chinese display problems in unity.

2. The manager loads the configuration and switches the language

A singleton class is inherited here, and you can directly call LanguageMgr.Instance.OnInit(); to initialize the language. After switching languages, I wrote a notification dispatch event here.

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public enum EnLanguageType { 
    English,
    Chinese,
    None,
}
// 多语言
public class LanguageMgr : TSingleton<LanguageMgr>
{
    private readonly string[] m_languageNames = {"en", "zh"};

    //选择自已需要的本地语言  
    private EnLanguageType m_language = EnLanguageType.None;

    private Dictionary<EnLanguageType, Dictionary<string, string>> m_dic = new  Dictionary<EnLanguageType, Dictionary<string, string>>();
    /// <summary>  
    /// 读取配置文件,将文件信息保存到字典里  
    /// </summary>  
    public LanguageMgr()
    {
        // 加载多语言配置
        for (int i = 0; i < m_languageNames.Length; i++)
        {
            if (!m_dic.ContainsKey((EnLanguageType)i))
            {
                m_dic[(EnLanguageType)i] = new Dictionary<string, string>();
            }
            TextAsset ta = Resources.Load<TextAsset>("LanguageTxt/" + m_languageNames[i]);
            string text = ta.text;

            string[] lines = text.Split('\n');
            foreach (string line in lines)
            {
                if (line == null)
                {
                    continue;
                }
                string[] keyAndValue = line.Split('=');
                m_dic[(EnLanguageType)i].Add(keyAndValue[0], keyAndValue[1]);
            }
        }
    }
    public void OnInit()
    {
        ChangeLanguage(EnLanguageType.English);
    }
    /// <summary>  
    /// 获取value  
    /// </summary>  
    /// <param name="key"></param>  
    /// <returns></returns>  
    public string GetValue(string key)
    {
        if (m_dic.ContainsKey(m_language) == false)
        {
            return null;// 不包含语言库
        }
        if (!m_dic[m_language].ContainsKey(key)) return null;// 不包含key
        string value = null;
        m_dic[m_language].TryGetValue(key, out value);
        return value;
    }
    // 格式化 多参数
    public string LanguageFormat(string key,params string[] par)
    {
        var text = GetValue(key);
        if (text == null) return null;
        return string.Format(text, par);
    }
    // 切换语言
    public void ChangeLanguage(EnLanguageType lang)
    {
        if (m_language == lang) return;
        m_language = lang;
        // 更新所有;Text
        EventManager.Instance.DispatchEvent(EventType.UPDATELANGUAGESHOW);
        
    }
}




3. Build a LanguageText script to hang on the UI object with the Text component, and bind the corresponding multilingual key

Initialize the listening event here, and update the text after receiving the language update

using System;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// text多语言绑定
/// </summary>

public class LanguageText : MonoBehaviour
{
    public string key = " ";
    void Start()
    {
        EventManager.Instance.AddListener((int)EventType.UPDATELANGUAGESHOW, UpdateText);
        this.UpdateText(null);
    }
    // 
    public void UpdateText(Message ms)
    {
        if (!gameObject) return;
        GetComponent<Text>().text = LanguageMgr.Instance.GetValue(key);
    }
    private void OnDestroy()
    {
        EventManager.Instance.RemoveListener((int)EventType.UPDATELANGUAGESHOW, UpdateText);
    }
}

 You can call  LanguageMgr.Instance.LanguageFormat("abc","hahaha","20") to get the format parameter multilingual

Guess you like

Origin blog.csdn.net/github_38633141/article/details/123133363