/// <summary> /// Load all translation defined in Json format in the specified file /// </summary> /// <param name="fileName">The file we want to load</param> /// <param name="loader">The loader to load each translation</param> public void LoadFile(string fileName, LocalizationLoader loader) { using (StreamReader reader = File.OpenText(fileName)) { JObject root = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); root.Properties().ToList() .ForEach(property => ParseSubElement(property, new Stack <string>(), loader, fileName)); } }
/// <summary> /// Load all translation defined in Json format in the specified file /// </summary> /// <param name="fileName">The file we want to load</param> /// <param name="loader">The loader to load each translation</param> public void LoadFile(string fileName, LocalizationLoader loader) { var input = new StringReader(File.ReadAllText(fileName)); var yaml = new YamlStream(); yaml.Load(input); var mapping = (YamlMappingNode)yaml.Documents[0].RootNode; mapping.ToList() .ForEach(pair => ParseSubElement(pair, new Stack <string>(), loader, fileName)); }
private void ParseSubElement(JProperty property, Stack <string> textId, LocalizationLoader loader, string fileName) { switch (property.Value.Type) { case JTokenType.Object: textId.Push(property.Name); ((JObject)property.Value).Properties().ToList() .ForEach(subProperty => ParseSubElement(subProperty, textId, loader, fileName)); textId.Pop(); break; case JTokenType.String: loader.AddTranslation(LabelPathRootPrefix + string.Join(LabelPathSeparator, textId.Reverse()) + LabelPathSuffix, property.Name, property.Value.ToString(), fileName); break; default: throw new FormatException($"Invalid format in Json language file for property [{property.Name}]"); } }
private void ParseSubElement(KeyValuePair <YamlNode, YamlNode> nodePair, Stack <string> textId, LocalizationLoader loader, string fileName) { if (nodePair.Value is YamlMappingNode mappingNode) { textId.Push(nodePair.Key.ToString()); mappingNode.ToList() .ForEach(pair => ParseSubElement(pair, textId, loader, fileName)); textId.Pop(); } else { loader.AddTranslation(LabelPathRootPrefix + string.Join(LabelPathSeparator, textId.Reverse()) + LabelPathSuffix, nodePair.Key.ToString(), nodePair.Value.ToString(), fileName); } }