/// <summary> /// Helper function that adds a single line from a CSV file to the localization list. /// </summary> static void AddCSV(GameBetterList <string> values, bool warnOnDuplicate = true) { if (values.size < 2) { return; } string key = values[0]; if (string.IsNullOrEmpty(key)) { return; } string[] temp = new string[values.size - 1]; for (int i = 1; i < values.size; ++i) { temp[i - 1] = values[i]; } if (mDictionary.ContainsKey(key)) { mDictionary[key] = temp; if (warnOnDuplicate) { Debug.LogWarning("Localization key '" + key + "' is already present"); } } else { try { mDictionary.Add(key, temp); } catch (System.Exception ex) { Debug.LogError("Unable to add '" + key + "' to the Localization dictionary.\n" + ex.Message); } } }
/// <summary> /// Load the specified CSV file. /// </summary> static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false) { if (bytes == null) { return(false); } GameByteReader reader = new GameByteReader(bytes); // The first line should contain "KEY", followed by languages. GameBetterList <string> temp = reader.ReadCSV(); // There must be at least two columns in a valid CSV file if (temp.size < 2) { return(false); } // Clear the dictionary if (!merge || temp.size - 1 != mLanguage.Length) { merge = false; mDictionary.Clear(); } temp[0] = "KEY"; mLanguages = new string[temp.size - 1]; for (int i = 0; i < mLanguages.Length; ++i) { mLanguages[i] = temp[i + 1]; } // Read the entire CSV file into memory while (temp != null) { AddCSV(temp, !merge); temp = reader.ReadCSV(); } return(true); }