Example #1
0
        /// <summary>
        /// Initializes local database extracted from Tables.pak
        /// </summary>
        public void Init()
        {
            // Copy Tables.pak into working directory
            if (Directory.GetFiles(WorkingDirectory, "*.*", SearchOption.AllDirectories).Length == 0)
            {
                ZipFile.ExtractToDirectory(DbFilePath, WorkingDirectory);
            }

            // Remove all *.pak file so KCD loads .xml files only
            foreach (var tblFile in Directory.GetFiles(WorkingDirectory, "*.tbl", SearchOption.AllDirectories))
            {
                File.Delete(tblFile);
            }

            // Add localization
            string localizationsDir = $@"{WorkingDirectory}\Localizations_{LocalizationLanguage.ToString().Substring(0, 3).ToLower()}";

            if (Directory.Exists(localizationsDir))
            {
                Directory.Delete(localizationsDir, true);
            }
            Directory.CreateDirectory(localizationsDir);
            ZipFile.ExtractToDirectory($@"{KCDDirectory}\Localization\{LocalizationLanguage}_xml.pak", localizationsDir);

            Database      = new DatabaseDescriptor(WorkingDirectory, localizationsDir);
            Localizations = new LocalizationsLoader(_database);             // Can't use prop because of custom getter
            IsInitialized = true;
        }
        /// <summary>
        /// Get the translation of a specified text on a certain language
        /// </summary>
        /// <param name="content">Text to translate</param>
        /// <param name="customLanguage">To what language will the text be translated to?</param>
        /// <returns></returns>
        public string CustomTranslate(LocalizationKeyword content, LocalizationLanguage customLanguage)
        {
            if (!alreadyInitialized)
            {
                InitializeSystems();
            }

            LocalizationData data;

            if (languages.TryGetValue(customLanguage, out data))
            {
                string result = data.Translate(content);

                //Check if the consulted language has a translation for such text
                if (result == null)
                {
                    //If the consulted language is the one set as default or fallback, return "ERROR".
                    if (customLanguage == defaultLanguage)
                    {
                        string contentString = content.ToString();
                        Debug.LogError("There's no match in the language \"" + customLanguage.ToString() + "\" for \"" + contentString + "\"");
                        return(contentString);
                    }
                    else
                    {
                        return(CustomTranslate(content, defaultLanguage));
                    }
                }

                return(result);
            }

            if (customLanguage == defaultLanguage)
            {
                Debug.LogError("There's no record of the language \"" + customLanguage.ToString() + "\".");
                return("[ERROR]");
            }
            else
            {
                Debug.LogError("There's no record of the language \"" + customLanguage.ToString() + "\". The default language will be used.");
                return(CustomTranslate(content, defaultLanguage));
            }
        }
        /// <summary>
        /// Creates the conversion table between string and language ID
        /// </summary>
        void InitializeLanguagesIDDictionary()
        {
            languagesIDTable = new Dictionary <string, LocalizationLanguage>();

            System.Array A = System.Enum.GetValues(typeof(LocalizationLanguage));
            if (A.Length == 0)
            {
                throw new System.ArgumentException("Enum " + typeof(LocalizationLanguage).ToString() + " is empty", "array");
            }

            for (int i = 0; i < A.Length; i++)
            {
                LocalizationLanguage locLan = (LocalizationLanguage)A.GetValue(i);
                languagesIDTable.Add(locLan.ToString(), locLan);
            }
        }
Example #4
0
        /// <summary>
        /// Loads the translator with the data holded inside its DataFile.
        /// </summary>
        public void LoadData()
        {
            //languages = new Dictionary<LocalizationLanguage, LocalizationData>();

            TextAsset localizationData = dataFile;

            if ((object)localizationData == null)  //System.IO.File.Exists(levelPath))
            {
                Debug.LogError(@"I/O ERROR: Couldn't  load the data file for """ + language.ToString() + @""". Is the field assigned?");
                //string level_text = System.IO.File.ReadAllText(levelPath);
                return;
            }

            string localizationText = localizationData.text;

            Debug.Log(@"Loading the data file for """ + language.ToString() + @"""");

            var rawLanguageData = JSON.Parse(localizationText);

            if ((object)rawLanguageData == null)
            {
                Debug.LogWarning("JSON is empty");
                return;
            }

            //Not used in the code yet. Commented to avoid warnings.
            string name = rawLanguageData["NAME"].Value;
            string id   = rawLanguageData["ID"].Value;

            //string scriptType = rawLanguageData["SCRIPT"].Value;
            //The writing system is defined on the Inspector, but it could
            //be read from the data file and determined using this code.

            /*if (scriptType == "RTL")
             *  writingSystem = WritingSystem.RTL;
             * else
             *  writingSystem = WritingSystem.LTR;*/


            //TODO: Check if the id key exists
            //LocalizationLanguage newLanguageID = languagesIDTable[id.ToUpper()];
            //LocalizationData newData = new LocalizationData(newLanguageID, null, null); //id

            var data = rawLanguageData["DATA"];

            for (int i = 0; i < data.Count; i++)
            {
                var    dataValue   = data[i];
                string keyword     = dataValue[0].Value;
                string translation = dataValue[1].Value;

                if (writingSystem == WritingSystem.RTL)
                {
                    translation.Reverse();
                }

                //TODO: Check if the type key exists
                AddData(LocalizationManager.Instance.GetDataType(keyword), translation);
            }

            //languages.Add(newLanguageID, newData);
        }