Skip to content

develop‐Language

Seong Hyeok OH edited this page Jul 11, 2023 · 2 revisions

준비

  • 스프트라아트에 언어 번역 적용
  • Singleton을 Laoding씬에 적용
  • 번역이 필요한 텍스트에 LocalizeScript 적용

스크립트

  • Singleton.cs: 영어, 한국어 가져오고 적용하기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

[System.Serializable]
public class Lang
{
    public string lang, langLocalize;
    public List value = new List();
}

public class Singleton : MonoBehaviour
{
    const string langURL = "https://docs.google.com/spreadsheets/d/1JaMgqfIbYieORwphzKA2BnmEZZ0z52oottzmhBzueBE/export?format=tsv";
    public event System.Action LocalizeChanged = (index) => { };
    public event System.Action LocalizeSettingChanged = () => { };
    public int curLangIndex;
    public List Langs;

    #region 싱글톤
    public static Singleton S;

    private void Awake()
    {
        if (null == S)
        {
            S = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(this);
        }
        InitLang();
    }
    #endregion
    private void InitLang()
    {
        int langIndex = PlayerPrefs.GetInt("LangIndex", -1);
        int systemIndex = Langs.FindIndex(x => x.lang.ToLower() == Application.systemLanguage.ToString().ToLower());
        if (systemIndex == -1)
        {
            systemIndex = 0;
        }
        int index = langIndex == -1 ? systemIndex : langIndex;

        SetLangIndex(index);
    }
    [ContextMenu("언어 가져오기")]
    private void GetLang()
    {
        StartCoroutine(GetLangCo());
    }
    private IEnumerator GetLangCo()
    {
        UnityWebRequest www = UnityWebRequest.Get(langURL);
        yield return www.SendWebRequest();
        SetLangList(www.downloadHandler.text);
    }
    private void SetLangList(string tsv)
    {
        // 이차원 배열(세로, 가로)
        string[] row = tsv.Split('\n');
        int rowSize = row.Length;
        int columnSize = row[0].Split('\t').Length;
        string[,] Sentence = new string[rowSize, columnSize];

        for (int i = 0; i < rowSize; i++)
        {
            string[] column = row[i].Split('\t');
            for (int j = 0; j < columnSize; j++)
            {
                Sentence[i, j] = column[j];
            }
        }

        // 클래스 리스트
        Langs = new List();
        for (int i = 0; i < columnSize; i++)
        {
            Lang lang = new Lang();
            lang.lang = Sentence[0, i];
            lang.langLocalize = Sentence[1, i];

            for (int j = 2; j < rowSize; j++)
            {
                lang.value.Add(Sentence[j, i]);
            }
            Langs.Add(lang);
        }
    }
    public void SetLangIndex(int index)
    {
        curLangIndex = index;
        PlayerPrefs.SetInt("LangIndex", curLangIndex);
        LocalizeChanged(curLangIndex);
        LocalizeSettingChanged();
    }
}

  • LocalizeScript: 텍스트 변환

using UnityEngine;
using static Singleton;
using TMPro;

public class LocalizeScript : MonoBehaviour
{
    public string TextKey;

    private void Start()
    {
        LocalizeChanged(S.curLangIndex);
        S.LocalizeChanged += LocalizeChanged;
    }
    private void OnDestroy()
    {
        S.LocalizeChanged -= LocalizeChanged;
    }
    private string Localize(string key, int langIndex)
    {
        int keyIndex = S.Langs[0].value.FindIndex(x => x.ToLower() == key.ToLower());
        return S.Langs[langIndex].value[keyIndex];
    }
    private void LocalizeChanged(int langIndex)
    {
        TMP_Text tmpText = GetComponent();
        if (tmpText != null)
        {
            tmpText.text = Localize(TextKey, langIndex);
        }
    }
}

  • LocalizeSetting: 드롭다운 UI 적용

using System.Collections.Generic;
using UnityEngine;
using static Singleton;
using TMPro;

public class LocalizeSetting : MonoBehaviour
{
    private TMP_Dropdown mdropdown;

    private void Start()
    {
        mdropdown = GetComponent();
        if (mdropdown.options.Count != S.Langs.Count)
        {
            SetLangOption();
        }
        mdropdown.onValueChanged.AddListener((d) => S.SetLangIndex(mdropdown.value));
        LocalizeSettingChanged();
        S.LocalizeSettingChanged += LocalizeSettingChanged;
    }
    private void OnDestroy()
    {
        S.LocalizeSettingChanged -= LocalizeSettingChanged;
    }
    private void SetLangOption()
    {
        List optionDatas = new List();
        for (int i = 0; i < S.Langs.Count; i++)
        {
            optionDatas.Add(new TMP_Dropdown.OptionData() { text = S.Langs[i].langLocalize });
        }
        mdropdown.options = optionDatas;
    }
    private void LocalizeSettingChanged()
    {
        mdropdown.value = S.curLangIndex;
    }
}

참고

Clone this wiki locally