static void AddCSV(FasterListLessGarbage <string> newValues, string[] newLanguages, Dictionary <string, int> languageIndices) { if (newValues.size < 2) { return; } string key = newValues[0]; if (string.IsNullOrEmpty(key)) { return; } string[] copy = ExtractStrings(newValues, newLanguages, languageIndices); if (mDictionary.ContainsKey(key)) { mDictionary[key] = copy; if (newLanguages == null) { Debug.LogWarning("Localization key '" + key + "' is already present"); } } else { try { mDictionary.Add(key, copy); } catch (System.Exception ex) { Debug.LogError("Unable to add '" + key + "' to the Localization dictionary.\n" + ex.Message); } } }
static string[] ExtractStrings(FasterListLessGarbage <string> added, string[] newLanguages, Dictionary <string, int> languageIndices) { if (newLanguages == null) { string[] values = new string[mLanguages.Length]; for (int i = 1, max = Mathf.Min(added.size, values.Length + 1); i < max; ++i) { values[i - 1] = added[i]; } return(values); } else { string[] values; string s = added[0]; if (!mDictionary.TryGetValue(s, out values)) { values = new string[mLanguages.Length]; } for (int i = 0, imax = newLanguages.Length; i < imax; ++i) { string language = newLanguages[i]; int index = languageIndices[language]; values[index] = added[i + 1]; } return(values); } }
static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false) { if (bytes == null) { return(false); } ByteReader reader = new ByteReader(bytes); // The first line should contain "KEY", followed by languages. FasterListLessGarbage <string> header = reader.ReadCSV(); // There must be at least two columns in a valid CSV file if (header.size < 2) { return(false); } header.RemoveAt(0); string[] languagesToAdd = null; if (string.IsNullOrEmpty(mLanguage)) { localizationHasBeenSet = false; } // Clear the dictionary if (!localizationHasBeenSet || (!merge && !mMerging) || mLanguages == null || mLanguages.Length == 0) { mDictionary.Clear(); mLanguages = new string[header.size]; if (!localizationHasBeenSet) { mLanguage = PlayerPrefs.GetString("Language", header[0]); localizationHasBeenSet = true; } for (int i = 0; i < header.size; ++i) { mLanguages[i] = header[i]; if (mLanguages[i] == mLanguage) { mLanguageIndex = i; } } } else { languagesToAdd = new string[header.size]; for (int i = 0; i < header.size; ++i) { languagesToAdd[i] = header[i]; } // Automatically resize the existing languages and add the new language to the mix for (int i = 0; i < header.size; ++i) { if (!HasLanguage(header[i])) { int newSize = mLanguages.Length + 1; System.Array.Resize(ref mLanguages, newSize); mLanguages[newSize - 1] = header[i]; Dictionary <string, string[]> newDict = new Dictionary <string, string[]>(); foreach (KeyValuePair <string, string[]> pair in mDictionary) { string[] arr = pair.Value; System.Array.Resize(ref arr, newSize); arr[newSize - 1] = arr[0]; newDict.Add(pair.Key, arr); } mDictionary = newDict; } } } Dictionary <string, int> languageIndices = new Dictionary <string, int>(); for (int i = 0; i < mLanguages.Length; ++i) { languageIndices.Add(mLanguages[i], i); } // Read the entire CSV file into memory for (;;) { FasterListLessGarbage <string> temp = reader.ReadCSV(); if (temp == null || temp.size == 0) { break; } if (string.IsNullOrEmpty(temp[0])) { continue; } AddCSV(temp, languagesToAdd, languageIndices); } if (!mMerging && onLocalize != null) { mMerging = true; OnLocalizeNotification note = onLocalize; onLocalize = null; note(); onLocalize = note; mMerging = false; } return(true); }