示例#1
0
        private static void Init()
        {
            if (!_inited)
            {
                _settings = DB.Get <LocalizationSettingsDBEntry>();

                _defaultLanguage = DB.Get <LanguageItem>(_defaultSystemLanguage.ToString());

                var localizationSettingsData = PlayerPrefsLocalData.GetDataNode(LOCALIZATION_SETTINGS_KEY);
                var savedLanguageString      = localizationSettingsData.ContainsKey(LOCALIZATION_SAVED_LANGUAGE_KEY)
                    ? localizationSettingsData.GetString(LOCALIZATION_SAVED_LANGUAGE_KEY)
                    : null;

                if (string.IsNullOrEmpty(savedLanguageString))
                {
                    ChangeLanguage(_currentSystemLanguage.ToString());
                }
                else
                {
                    var savedLanguage = DB.Get <LanguageItem>(savedLanguageString);
                    if (savedLanguage != null)
                    {
                        ChangeLanguage(savedLanguage);
                    }
                    else
                    {
                        Debug.LogWarning(string.Format("Language not found: {0}", savedLanguageString));
                    }
                }

                _inited = true;
            }
        }
示例#2
0
        public static string Translate(string key)
        {
            var translation = key;

            if (!_inited)
            {
                _defaultLanguage = DB.Get <LanguageItem>(_defaultSystemLanguage.ToString());

                var localizationSettingsData = PlayerPrefsLocalData.GetDataNode(LOCALIZATION_SETTINGS_KEY);
                var savedLanguageString      = localizationSettingsData.ContainsKey(LOCALIZATION_SAVED_LANGUAGE_KEY)
                    ? localizationSettingsData.GetString(LOCALIZATION_SAVED_LANGUAGE_KEY)
                    : null;

                if (string.IsNullOrEmpty(savedLanguageString))
                {
                    ChangeLanguage(_currentSystemLanguage.ToString());
                }
                else
                {
                    var savedLanguage = DB.Get <LanguageItem>(savedLanguageString);
                    if (savedLanguage != null)
                    {
                        ChangeLanguage(savedLanguage);
                    }
                    else
                    {
                        Debug.LogWarning(string.Format("Language not found: {0}", savedLanguageString));
                    }
                }

                _inited = true;
            }

            if (_defaultLanguage != null && _defaultLanguage.translations != null &&
                _defaultLanguage.translations.ContainsKey(key) && !string.IsNullOrEmpty(_defaultLanguage.translations[key]))
            {
                translation = _defaultLanguage.translations[key];
            }

            if (currentLanguage != null && currentLanguage.translations != null &&
                currentLanguage.translations.ContainsKey(key) && !string.IsNullOrEmpty(currentLanguage.translations[key]))
            {
                translation = currentLanguage.translations[key];
            }

            return(translation);
        }
示例#3
0
        public static void ChangeLanguage(LanguageItem newLanguage)
        {
            if (currentLanguage == newLanguage)
            {
                return;
            }
            var localizationSettingsData = PlayerPrefsLocalData.GetDataNode(LOCALIZATION_SETTINGS_KEY);

            localizationSettingsData.AddOrSet(LOCALIZATION_SAVED_LANGUAGE_KEY, newLanguage.name);
            PlayerPrefsLocalData.SetDataNode(LOCALIZATION_SETTINGS_KEY, localizationSettingsData);
            currentLanguage = newLanguage;

            if (_inited)
            {
                changeLanguage(newLanguage);
            }
        }
        private static void Init()
        {
            if (!_inited)
            {
                _settingsDBEntry = DB.Get <LocalizationSettingsDBEntry>();

                _defaultLanguage = DB.Get <LanguageItem>(_defaultSystemLanguage.ToString());

                var localizationSettingsData = PlayerPrefsLocalData.GetDataNode(LOCALIZATION_SETTINGS_KEY);

                _settings = JSONUtuls.Deserialize <Settings>(localizationSettingsData);

                if (_settings == null)
                {
                    _settings = new Settings();
                }

                if (string.IsNullOrEmpty(_settings.savedLanguage))
                {
                    ChangeLanguage(_currentSystemLanguage.ToString());
                }
                else
                {
                    var savedLanguage = DB.Get <LanguageItem>(_settings.savedLanguage);
                    if (savedLanguage != null)
                    {
                        ChangeLanguage(savedLanguage);
                    }
                    else
                    {
                        Debug.LogWarning(string.Format("Language not found: {0}", _settings.savedLanguage));
                    }
                }

                _inited = true;
            }
        }
        public static void ChangeLanguage(LanguageItem newLanguage)
        {
            if (currentLanguage == newLanguage)
            {
                return;
            }

            if (_settings == null)
            {
                _settings = new Settings();
            }

            _settings.savedLanguage = newLanguage.name;

            var localizationSettingsData = _settings.ToJson();

            PlayerPrefsLocalData.SetDataNode(LOCALIZATION_SETTINGS_KEY, localizationSettingsData);
            currentLanguage = newLanguage;

            if (_inited)
            {
                changeLanguage?.Invoke(newLanguage);
            }
        }