//writes data into file public static bool WriteLocalizationFile(string filepath, LocalizationData localizationData) { bool success = false; //if filepath is valid... if (!String.IsNullOrEmpty(filepath)) { //serialize data based on extension string fileData = null; string fileExtension = Path.GetExtension(filepath); switch (fileExtension) { case ".json": fileData = ToJson(localizationData); break; case ".yml": fileData = ToYaml(localizationData); break; } //write data File.WriteAllText(filepath, fileData); success = true; } //result feedback return(success); }
//reads data from file public static bool ReadLocalizationFile(string filename, out LocalizationData localizationData) { bool success = false; localizationData = null; // searches Localization File string filepath = Path.Combine(Application.streamingAssetsPath, "Localization Files", filename); // if file is valid... if (File.Exists(filepath)) { // read data based on extension string fileData = File.ReadAllText(filepath); string fileExtension = Path.GetExtension(filepath); switch (fileExtension) { case ".json": localizationData = FromJson(fileData); break; case ".yml": localizationData = FromYaml(fileData); break; } success = true; } // result feedback return(success); }
// loads data from Localization File public static void LoadLocalizationFile(string filename) { // loads data from file LocalizationData localizationData = new LocalizationData(); bool success = LocalizationParser.ReadLocalizationFile(filename, out localizationData); if (success) { // creates and populates dictionary localDictionary = new Dictionary <string, string>(); foreach (LocalizationItem item in localizationData.items) { localDictionary.Add(item.key, item.value); } // sucessful debug Debug.Log("Data loaded! Dictionary contains " + localDictionary.Count + " entries!"); // mass localization MassLocalize(); } else { // error debug Debug.LogError("Cannot find Localization File!!"); } }
//yaml utility methods private static string ToYaml(LocalizationData data) { ISerializer serializer = new SerializerBuilder().Build(); return(serializer.Serialize(data)); }
//json utility methods private static string ToJson(LocalizationData data) { return(JsonUtility.ToJson(data)); }