168 lines
6.3 KiB
C#
168 lines
6.3 KiB
C#
using System.Collections.Generic;
|
|
|
|
using Misc;
|
|
|
|
using UnityEngine;
|
|
|
|
// for [DllImport], see below
|
|
#if UNITY_IOS && !UNITY_EDITOR
|
|
using System.Runtime.InteropServices;
|
|
#endif
|
|
|
|
namespace Localization
|
|
{
|
|
public class LocalizationManager : MonoBehaviour
|
|
{
|
|
// Singleton Instance
|
|
public static LocalizationManager Instance { get; private set; }
|
|
|
|
public bool IsDebugMode = false;
|
|
|
|
// language indices in the string DB
|
|
private int systemLanguageIndex;
|
|
private int englishLanguageIndex;
|
|
|
|
/// <summary>
|
|
/// <para>Translates the given locId into the local system language (with an
|
|
/// English fallback, if the system language is unknown).</para>
|
|
/// <para>Dynamic parts of the string can be customized through the customValues
|
|
/// dictionary.</para>
|
|
/// </summary>
|
|
/// <param name="locId">Localization ID of the string to translate</param>
|
|
/// <param name="customValues">Optional overrides for dynamic tags in the string</param>
|
|
/// <returns>The localized and patched string</returns>
|
|
public string GetLocalizedString(int locId, Dictionary<string, string> customValues = null)
|
|
{
|
|
return FetchStringFromDatabase(locId, systemLanguageIndex, customValues);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lighter version of GetLocalizedString, always fetching the English text,
|
|
/// and with no custom values.
|
|
/// </summary>
|
|
/// <param name="locId">Localization ID of the string to translate</param>
|
|
/// <returns>The English version of the string</returns>
|
|
public string GetEnglishLocalizedString(int locId)
|
|
{
|
|
return FetchStringFromDatabase(locId, englishLanguageIndex);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
var systemLanguage = Application.systemLanguage;
|
|
|
|
// fix to support different kinds of chinese
|
|
if (systemLanguage == SystemLanguage.Chinese)
|
|
{
|
|
// ISO code for traditional (and HK traditional) is zh-hant, for simplified it's zh-hans
|
|
systemLanguage = GetSystemLanguage().ToUpper().EndsWith("T") ? SystemLanguage.ChineseTraditional : SystemLanguage.ChineseSimplified;
|
|
}
|
|
|
|
// assume the string database always has at least English
|
|
englishLanguageIndex = System.Array.IndexOf(StringDatabase.Languages, SystemLanguage.English);
|
|
Debug.Assert(englishLanguageIndex != -1, "String database must have at least English translations");
|
|
|
|
// detect system language, or just fallback to English
|
|
systemLanguageIndex = System.Array.IndexOf(StringDatabase.Languages, systemLanguage);
|
|
if (systemLanguageIndex == -1)
|
|
systemLanguageIndex = englishLanguageIndex;
|
|
}
|
|
|
|
private string FetchStringFromDatabase(int locId, int language, Dictionary<string, string> customValues = null)
|
|
{
|
|
if (locId == 0)
|
|
return string.Empty;
|
|
|
|
var translations = StringDatabase.Strings.GetField(locId.ToString());
|
|
if (translations == null)
|
|
return "<MISSING_PHONE:" + locId + ">";
|
|
|
|
var stringJson = translations[language];
|
|
if (stringJson.IsNull)
|
|
return "<MISSING_PHONE:" + StringDatabase.Languages[language] + ":" + locId + ">";
|
|
|
|
var localizedString = stringJson.StringValue;
|
|
|
|
// replace dynamic parts of the string
|
|
if (customValues != null)
|
|
localizedString = StringProcessor.formatCustomizableString(localizedString, customValues);
|
|
|
|
// remove extra markers before
|
|
localizedString = StringProcessor.removeMarkers(localizedString);
|
|
|
|
if (IsDebugMode)
|
|
localizedString = "[" + locId.ToString() + "] " + localizedString;
|
|
|
|
return localizedString;
|
|
}
|
|
|
|
|
|
public string GetTwoLanguageFromSystemLanguage()
|
|
{
|
|
switch(Application.systemLanguage) {
|
|
case SystemLanguage.Chinese: return "zh-CN";
|
|
case SystemLanguage.ChineseSimplified: return "zh-CN";
|
|
case SystemLanguage.ChineseTraditional: return "zh-CN";
|
|
case SystemLanguage.Portuguese: return "pt-BR";
|
|
case SystemLanguage.Danish: return "da-DK";
|
|
case SystemLanguage.Dutch: return "nl-NL";
|
|
case SystemLanguage.English: return "en-US";
|
|
case SystemLanguage.Finnish: return "fi-FI";
|
|
case SystemLanguage.French: return "fr-FR";
|
|
case SystemLanguage.German: return "de-DE";
|
|
case SystemLanguage.Italian: return "it-IT";
|
|
case SystemLanguage.Japanese: return "ja-JP";
|
|
case SystemLanguage.Norwegian: return "nb-NO";
|
|
case SystemLanguage.Polish: return "pl-PL";
|
|
case SystemLanguage.Spanish: return "es-ES";
|
|
case SystemLanguage.Swedish: return "sv-SE";
|
|
case SystemLanguage.Russian: return "ru-RU";
|
|
case SystemLanguage.Korean: return "ko-KR";
|
|
default: return "en-US";
|
|
}
|
|
}
|
|
|
|
#if UNITY_IOS && !UNITY_EDITOR
|
|
[DllImport("__Internal")]
|
|
[return: MarshalAs(UnmanagedType.LPStr)]
|
|
private static extern string LocaleInfo_getLocaleIdentifier();
|
|
#endif
|
|
|
|
public string GetSystemLanguage()
|
|
{
|
|
#if UNITY_EDITOR
|
|
return "en";
|
|
#endif
|
|
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
using (AndroidJavaClass localInfoClass = new AndroidJavaClass("com.ubisoft.jd.locale.LocaleInfo"))
|
|
{
|
|
return localInfoClass.CallStatic<string>("getLanguage");
|
|
}
|
|
#elif UNITY_IOS && !UNITY_EDITOR
|
|
string locale = LocaleInfo_getLocaleIdentifier();
|
|
return locale.Split('_')[0];
|
|
#endif
|
|
}
|
|
|
|
public string GetSystemCountry()
|
|
{
|
|
#if UNITY_EDITOR
|
|
return "US";
|
|
#endif
|
|
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
using (AndroidJavaClass localInfoClass = new AndroidJavaClass("com.ubisoft.jd.locale.LocaleInfo"))
|
|
{
|
|
return localInfoClass.CallStatic<string>("getCountry");
|
|
}
|
|
#elif UNITY_IOS && !UNITY_EDITOR
|
|
string locale = LocaleInfo_getLocaleIdentifier();
|
|
return locale.Split('_')[1];
|
|
#endif
|
|
}
|
|
}
|
|
}
|