예제 #1
0
        private void DownloadFileToResources()
        {
            TextAsset data = Resources.Load(mCurrentFilename) as TextAsset;

            if (data == null)
            {
                Debug.LogError("Not found the file " + mCurrentFilename + " in Resources folder");
                return;
            }
            LocalizationData deserializedData = null;

            switch (TypeOfFile)
            {
            case eTypeOfFile.JSON:
                deserializedData = FromJsonTextAsset <LocalizationData>(data);
                break;

            case eTypeOfFile.XML:
                deserializedData = FromXmlTextAsset <LocalizationData>(data);
                break;

            default:
                break;
            }
            if (deserializedData != null)
            {
                InitDictionnary(deserializedData);
            }
        }
예제 #2
0
        public void LoadLocalizedText(string languageCode)
        {
            IsReady         = false;
            localizedText   = new Dictionary <string, string>();
            currentLanguage = languageCode;
            string filePath = Path.Combine(localizationDir, $"{currentLanguage}.json");

            if (File.Exists(filePath))
            {
                string json = File.ReadAllText(filePath);
                // TODO: load GNU gettext .mo files
                LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>(json);

                for (int i = 0; i < loadedData.items.Length; i++)
                {
                    localizedText.Add(loadedData.items[i].textId, loadedData.items[i].text);
                }
                Debug.Log($"Localization data loaded for {languageCode}, dictionary contains: {localizedText.Count} entries.");
            }
            else
            {
                Debug.LogWarning($"Cannot find localization file for '{currentLanguage}' at {filePath}!");
            }

            IsReady = true;
            OnLanguageChanged();
        }
예제 #3
0
        /// <summary>
        /// Loads localized text from JSON file into the localizedText dictionary
        /// </summary>
        /// <param name="fileName"> Name of the file to be loaded.false Will vary depending on language </param>
        /// <remark> Only english for now </remark>
        public void LoadLocalizedText(string fileName)
        {
            localizedText = new Dictionary <string, string>();
            // StreamingAssetsPath will always be known to unity, regardless of hardware
            string filePath = Path.Combine(Application.streamingAssetsPath, fileName);

            if (File.Exists(filePath))
            {
                string dataAsJson = File.ReadAllText(filePath);

                // deserialize text from text to a LocalizationData object
                LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>(dataAsJson);

                for (int i = 0; i < loadedData.texts.Length; i++)
                {
                    localizedText.Add(loadedData.texts[i].key, loadedData.texts[i].value);
                }
            }
            else
            {
                // ideally would handle this more gracefully, than just throwing an error (e.g. a pop up)
                Debug.LogError("Cannot find file");
            }

            isReady = true;
        }
예제 #4
0
        public void LoadLocalizedText(string fileName)
        {
            localizedText = new Dictionary <string, string> ();
            string filePath = Path.Combine(Application.streamingAssetsPath, fileName);

            string dataAsJson;

            if (Application.platform == RuntimePlatform.Android)            //Need to extract file from apk first
            {
                WWW reader = new WWW(filePath);
                while (!reader.isDone)
                {
                }

                dataAsJson = reader.text;
            }
            else
            {
                dataAsJson = File.ReadAllText(filePath);
            }



            LocalizationData loadedData = JsonUtility.FromJson <LocalizationData> (dataAsJson);

            for (int i = 0; i < loadedData.items.Length; i++)
            {
                localizedText.Add(loadedData.items [i].key, loadedData.items [i].value);
            }

            //Debug.Log ("Data loaded, dictionary contains: " + localizedText.Count + " entries");


            isReady = true;
        }
예제 #5
0
        private void LoadLocalizationData()
        {
            string filePath = EditorUtility.OpenFilePanel("Select Localization Data File", Application.streamingAssetsPath, "json");


            if (!string.IsNullOrEmpty(filePath))
            {
                string dataAsJson = File.ReadAllText(filePath);
                localizationData = JsonUtility.FromJson <LocalizationData>(dataAsJson);
            }
        }
예제 #6
0
 private void InitDictionnary(LocalizationData data)
 {
     mDictionaryText = new Dictionary <string, string>();
     foreach (var item in data.Items)
     {
         mDictionaryText.Add(item.Key, item.Value);
     }
     IsReady = true;
     UpdateCurrentText();
     Debug.LogFormat("{0}: Sucess file loaded {1}", LOG_HEADER, mCurrentFilename);
 }
예제 #7
0
        /// <summary>
        /// Loads localized text from JSON file into the localizedText dictionary
        /// </summary>
        /// <param name="fileName"> Name of the file to be loaded.false Will vary depending on language </param>
        /// <remark> Only english for now </remark>
        public void LoadLocalizedText(string fileName)
        {
            localizedText = new Dictionary <string, string>();

            // if (Application.platform == RuntimePlatform.WebGLPlayer) {
            //     //UnityWebRequest webRequest = new UnityWebRequest();
            //     var webRequest = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, fileName));
            //     yield return webRequest.SendWebRequest();
            //     // while (!webRequest.isDone) {
            //     //     if (webRequest.isNetworkError || webRequest.isHttpError) {
            //     //         break;
            //     //     }
            //     // }
            //     // if (webRequest.isNetworkError || webRequest.isHttpError) {
            //     //      Debug.LogError("WebRequest broke yo");
            //     // } else {
            //     string dataAsJson = webRequest.downloadHandler.text;//ReadAllText(filePath);

            //     // deserialize text from text to a LocalizationData object
            //     LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);

            //     for(int i = 0; i < loadedData.texts.Length; i++) {
            //         localizedText.Add(loadedData.texts[i].key, loadedData.texts[i].value);
            //     }
            //     //}
            // }
            // else {
            // StreamingAssetsPath will always be known to unity if standalone
            string filePath = Path.Combine(Application.streamingAssetsPath, fileName);

            if (File.Exists(filePath))
            {
                string dataAsJson = File.ReadAllText(filePath);

                // deserialize text from text to a LocalizationData object
                LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>(dataAsJson);

                for (int i = 0; i < loadedData.texts.Length; i++)
                {
                    localizedText.Add(loadedData.texts[i].key, loadedData.texts[i].value);
                }
            }
            else
            {
                // ideally would handle this more gracefully, than just throwing an error (e.g. a pop up)
                Debug.LogError("Cannot find file");
            }
            // }

            isReady = true;
            // yield return null;
        }
        private void LoadLocalization()
        {
            string filePath = Path.Combine(Application.streamingAssetsPath, language);

            if (File.Exists(filePath))
            {
                LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>
                                                  (File.ReadAllText(filePath));
                foreach (LocalizationItem item in loadedData)
                {
                    localizedText.Add(item.key, item.value);
                }
            }
        }
        private IEnumerator LoadLocalization()
        {
            string filePath = Path.Combine(Application.streamingAssetsPath, language);

            using (UnityWebRequest www = UnityWebRequest.Get(filePath))
            {
                yield return(www.SendWebRequest());

                LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>
                                                  (www.downloadHandler.text);
                foreach (LocalizationItem item in loadedData)
                {
                    localizedText.Add(item.key, item.value);
                }
            }
        }
예제 #10
0
        public void LoadLocalizedText(string language, string loadKeys)
        {
            PlayerPrefs.SetString("language", $"{language}.json");
            PlayerPrefs.Save();

            m_LocalizedText = new Dictionary <string, string>();

            LocalizationData loadedData = JsonUtility.FromJson <LocalizationData>(loadKeys);

            for (int i = 0; i < loadedData.items.Length; i++)
            {
                m_LocalizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
            }

            m_IsReady = true;
        }
예제 #11
0
        /// <summary>
        /// Выполняет парсинг файла с локализацией
        /// </summary>
        private void ParseLocalizationData(string jsonData, bool sendEvent)
        {
            if (string.IsNullOrEmpty(jsonData))
            {
                throw new RapWayException("Не найден файл локализации!");
            }

            _data = JsonUtility.FromJson <LocalizationData>(jsonData);

            if (sendEvent)
            {
                EventManager.RaiseEvent(EventType.LangChanged);
            }

            IsReady = true;
        }
        public string GetLocalizedValue(string key)
        {
            string _result = key;

            if (!_isReady)
            {
                return(_result);
            }

            LocalizationData _resultData = Dictionary.Find(t => t.Key == key);

            if (_resultData == null)
            {
                Debug.LogError(_missingTextString + " for Key: " + key);
                return(_result);
            }

            if (UseKeyAsValue)
            {
                return(_result);
            }

            switch (_currentLocalization)
            {
            case SystemLanguage.English:
                _result = _resultData.English;
                break;

            case SystemLanguage.Russian:
                _result = _resultData.Russian;
                break;

            case SystemLanguage.German:
                _result = _resultData.German;
                break;

            case SystemLanguage.French:
                _result = _resultData.French;
                break;

            case SystemLanguage.Spanish:
                _result = _resultData.Spanish;
                break;
            }

            return(_result);
        }
예제 #13
0
        public LocalizationData GenerateList()
        {
            if (mDictionaryText == null || mDictionaryText.Count == 0)
            {
                return(null);
            }
            var ret = new LocalizationData
            {
                Items = new List <LocalizationItem>()
            };

            foreach (var data in mDictionaryText)
            {
                var item = new LocalizationItem(data);
                ret.Items.Add(item);
            }
            return(ret);
        }
예제 #14
0
        private void CallbackDownloadXmlFile(string data, bool error)
        {
            if (error)
            {
                Debug.LogError("Download xml file failed: " + data);
                return;
            }
            if (string.IsNullOrEmpty(data))
            {
                Debug.LogError("Not found the file or is empty" + mCurrentFilename);
                return;
            }
            LocalizationData deserializedData = FromXmlString <LocalizationData>(data);

            if (deserializedData != null && deserializedData.Items != null)
            {
                InitDictionnary(deserializedData);
            }
        }
예제 #15
0
        private void DownloadFileToStreamingAssset()
        {
            string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, mCurrentFilenameWithExtension);

            LocalizationData deserializedData = null;

            switch (TypeOfFile)
            {
            case eTypeOfFile.JSON:
                deserializedData = FromJsonFile <LocalizationData>(filePath);
                break;

            case eTypeOfFile.XML:
                deserializedData = FromXmlFile <LocalizationData>(filePath);
                break;

            default:
                break;
            }
            if (deserializedData != null && deserializedData.Items != null)
            {
                InitDictionnary(deserializedData);
            }
        }
예제 #16
0
 private void CreateNewLocalizationData()
 {
     localizationData = new LocalizationData();
 }
예제 #17
0
        private static void CheckForValidFiles()
        {
            string registryPath = Path.Combine(Application.streamingAssetsPath, LOCALIZATION_REGISTRY_FILE_NAME + ".json");

            if (File.Exists(registryPath))
            {
                string dataAsJson = File.ReadAllText(registryPath);
                try
                {
                    m_FileRegistry = JsonUtility.FromJson <LocalizationFileRegistry>(dataAsJson);
                    Debug.Log("Registry File exists and is readable");

                    LocalizationFileRegistry tempRegist = new LocalizationFileRegistry(new List <LocalizationFileEntry>());
                    for (int i = 0; i < m_FileRegistry.fileEntries.Count; i++)
                    {
                        string filePath = Path.Combine(Application.streamingAssetsPath, m_FileRegistry.fileEntries[i].fileName + ".json");
                        if (File.Exists(filePath))
                        {
                            try
                            {
                                string           langDataAsJson = File.ReadAllText(filePath);
                                LocalizationData loadedData     = JsonUtility.FromJson <LocalizationData>(langDataAsJson);

                                localizedTextList.Add(new Dictionary <string, string>());

                                if (loadedData.font != null)
                                {
                                    fontList.Add(loadedData.font);
                                }
                                else
                                {
                                    fontList.Add(instance.defaultFont);
                                }


                                for (int j = 0; j < loadedData.items.Length; j++)
                                {
                                    localizedTextList[localizedTextList.Count - 1].Add(loadedData.items[j].key, loadedData.items[j].value);
                                }
                                //Check to make sure that all other entries after the initial English file have the correct Count as well as the exact same keys.
                                if (i > 0)
                                {
                                    if (!FilesAreEqual(localizedTextList[0], localizedTextList[localizedTextList.Count - 1]))
                                    {
                                        Debug.Log("Error: Localization File " + filePath + " can not be used due to inconsistencies with the base Localization File.");
                                    }
                                    else
                                    {
                                        tempRegist.fileEntries.Add(m_FileRegistry.fileEntries[i]);
                                    }
                                }
                                else if (i == 0)
                                {
                                    tempRegist.fileEntries.Add(m_FileRegistry.fileEntries[i]);
                                }

                                Debug.Log(filePath + " successfully loaded for localization. -- " + localizedTextList[localizedTextList.Count - 1].Count + " entries.");
                            }
                            catch
                            {
                                Debug.Log("Error: Localization File " + filePath + " found, but can not be read.");
                                m_Status = LocalizationStatus.fail;
                            }
                        }
                        else
                        {
                            Debug.Log("Error: Localization File " + filePath + " Not found");
                        }
                    }
                    if (localizedTextListCount == 0)
                    {
                        Debug.Log("Error: No localization text files could be found. Localization has failed");
                        m_Status = LocalizationStatus.fail;
                        return;
                    }
                    //modify the registy with the correct list of valid localization options.
                    m_FileRegistry = tempRegist;

                    m_Status = LocalizationStatus.success;
                }
                catch
                {
                    Debug.Log("Error: Registry File exists, but can not be read properly Localization has failed.");
                    m_Status = LocalizationStatus.fail;
                }
            }
            else
            {
                Debug.Log("Error: No Registry File exists. Localization has failed");
                m_Status = LocalizationStatus.fail;
            }
        }
예제 #18
0
 /// <summary>
 /// Выполняет парсинг данных локализации в json
 /// </summary>
 public static string LocalizationDataToJson(LocalizationData data)
 => JsonUtility.ToJson(data);