/// <summary> /// Initializes a new instance of the <see cref="UnityTranslationInternal.Translator.SectionTokens"/> class. /// </summary> public SectionTokens() { defaultLanguage = null; selectedLanguage = null; }
/// <summary> /// Parse xml file and return SectionLocaleTokens instance with all tokens. /// </summary> /// <returns>SectionLocaleTokens instance.</returns> /// <param name="filename">Name of file.</param> /// <param name="locale">Language code.</param> /// <param name="tokenIds">Token identifiers.</param> /// <param name="stringCount">String count.</param> /// <param name="stringArrayCount">String array count.</param> /// <param name="pluralsCount">Plurals count.</param> private static SectionLocaleTokens ParseXmlTokens(string filename, string locale, Dictionary <string, int>[] tokenIds, int stringCount, int stringArrayCount, int pluralsCount) { SectionLocaleTokens res = null; filename = filename.Remove(filename.Length - 4); do { string xmlResPath = "res/values" + ((locale != "") ? ("-" + locale) : "") + "/" + filename; TextAsset xmlFile = Resources.Load(xmlResPath, typeof(TextAsset)) as TextAsset; if (xmlFile != null) { if (res == null) { res = new SectionLocaleTokens(stringCount, stringArrayCount, pluralsCount); } XmlTextReader reader = null; try { reader = new XmlTextReader(new MemoryStream(xmlFile.bytes, false)); reader.WhitespaceHandling = WhitespaceHandling.None; bool resourcesFound = false; while (reader.Read()) { if (reader.Name == "resources") { resourcesFound = true; List <string> stringNames = new List <string>(); List <string> stringArrayNames = new List <string>(); List <string> pluralsNames = new List <string>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "string") { string tokenName = reader.GetAttribute("name"); if (Utils.CheckTokenName(tokenName, reader.Name, stringNames)) { stringNames.Add(tokenName); int index; if (tokenIds[0].TryGetValue(tokenName, out index)) { if (res.stringValues[index] == null) { res.stringValues[index] = Utils.ProcessTokenValue(reader.ReadString()); } } else { Debug.LogError("Tag <string> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else if (reader.Name == "string-array") { string tokenName = reader.GetAttribute("name"); if (Utils.CheckTokenName(tokenName, reader.Name, stringArrayNames)) { List <string> values = new List <string>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "item") { values.Add(Utils.ProcessTokenValue(reader.ReadString())); } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <string-array> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "string-array") { break; } } } stringArrayNames.Add(tokenName); int index; if (tokenIds[1].TryGetValue(tokenName, out index)) { if (res.stringArrayValues[index] == null) { res.stringArrayValues[index] = values.ToArray(); } } else { Debug.LogError("Tag <string-array> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else if (reader.Name == "plurals") { string tokenName = reader.GetAttribute("name"); if (Utils.CheckTokenName(tokenName, reader.Name, pluralsNames)) { string[] values = new string[(int)PluralsQuantity.Count]; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "item") { PluralsQuantity quantity = PluralsQuantity.Count; // Nothing string quantityValue = reader.GetAttribute("quantity"); if (quantityValue == null) { Debug.LogError("Attribute \"quantity\" not found for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } else if (quantityValue == "") { Debug.LogError("Attribute \"quantity\" empty for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } else if (quantityValue == "zero") { quantity = PluralsQuantity.Zero; } else if (quantityValue == "one") { quantity = PluralsQuantity.One; } else if (quantityValue == "two") { quantity = PluralsQuantity.Two; } else if (quantityValue == "few") { quantity = PluralsQuantity.Few; } else if (quantityValue == "many") { quantity = PluralsQuantity.Many; } else if (quantityValue == "other") { quantity = PluralsQuantity.Other; } else { Debug.LogError("Unknown attribute \"quantity\" value \"" + quantityValue + "\" for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } if (quantity != PluralsQuantity.Count) { if (values[(int)quantity] == null) { values[(int)quantity] = Utils.ProcessTokenValue(reader.ReadString()); } else { Debug.LogError("Duplicate <item> tag with attribute \"quantity\" value \"" + quantityValue + "\" in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <plurals> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "plurals") { break; } } } pluralsNames.Add(tokenName); int index; if (tokenIds[2].TryGetValue(tokenName, out index)) { if (res.pluralsValues[index] == null) { res.pluralsValues[index] = values; } } else { Debug.LogError("Tag <plurals> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <resources> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } break; } } if (!resourcesFound) { Debug.LogError("Tag <resources> not found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } catch (Exception e) { Debug.LogError("Exception occured while parsing \"Assets/Resources/" + xmlResPath + ".xml\""); Debug.LogException(e); } finally { if (reader != null) { reader.Close(); } } } if (locale != "") { int index = locale.LastIndexOf('-'); if (index < 0) { break; } locale = locale.Remove(index); } else { break; } } while (true); return(res); }
/// <summary> /// Parse xml file and return SectionLocaleTokens instance with all tokens. /// </summary> /// <returns>SectionLocaleTokens instance.</returns> /// <param name="filename">Name of file.</param> /// <param name="locale">Language code.</param> /// <param name="tokenIds">Token identifiers.</param> /// <param name="stringCount">String count.</param> /// <param name="stringArrayCount">String array count.</param> /// <param name="pluralsCount">Plurals count.</param> private static SectionLocaleTokens parseXmlTokens(string filename, string locale, Dictionary<string, int>[] tokenIds, int stringCount, int stringArrayCount, int pluralsCount) { SectionLocaleTokens res = null; filename = filename.Remove(filename.Length - 4); do { string xmlResPath = "res/values" + ((locale != "") ? ("-" + locale) : "") + "/" + filename; TextAsset xmlFile = Resources.Load(xmlResPath, typeof(TextAsset)) as TextAsset; if (xmlFile != null) { if (res == null) { res = new SectionLocaleTokens(stringCount, stringArrayCount, pluralsCount); } XmlTextReader reader = null; try { reader = new XmlTextReader(new MemoryStream(xmlFile.bytes, false)); reader.WhitespaceHandling = WhitespaceHandling.None; bool resourcesFound = false; while (reader.Read()) { if (reader.Name == "resources") { resourcesFound = true; List<string> stringNames = new List<string>(); List<string> stringArrayNames = new List<string>(); List<string> pluralsNames = new List<string>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "string") { string tokenName = reader.GetAttribute("name"); if (Utils.checkTokenName(tokenName, reader.Name, stringNames)) { stringNames.Add(tokenName); int index; if (tokenIds[0].TryGetValue(tokenName, out index)) { if (res.stringValues[index] == null) { res.stringValues[index] = Utils.processTokenValue(reader.ReadString()); } } else { Debug.LogError("Tag <string> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else if (reader.Name == "string-array") { string tokenName = reader.GetAttribute("name"); if (Utils.checkTokenName(tokenName, reader.Name, stringArrayNames)) { List<string> values = new List<string>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "item") { values.Add(Utils.processTokenValue(reader.ReadString())); } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <string-array> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "string-array") { break; } } } stringArrayNames.Add(tokenName); int index; if (tokenIds[1].TryGetValue(tokenName, out index)) { if (res.stringArrayValues[index] == null) { res.stringArrayValues[index] = values.ToArray(); } } else { Debug.LogError("Tag <string-array> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else if (reader.Name == "plurals") { string tokenName = reader.GetAttribute("name"); if (Utils.checkTokenName(tokenName, reader.Name, pluralsNames)) { string[] values = new string[(int)PluralsQuantity.Count]; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "item") { PluralsQuantity quantity = PluralsQuantity.Count; // Nothing string quantityValue = reader.GetAttribute("quantity"); if (quantityValue == null) { Debug.LogError("Attribute \"quantity\" not found for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } else if (quantityValue == "") { Debug.LogError("Attribute \"quantity\" empty for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } else if (quantityValue == "zero") { quantity = PluralsQuantity.Zero; } else if (quantityValue == "one") { quantity = PluralsQuantity.One; } else if (quantityValue == "two") { quantity = PluralsQuantity.Two; } else if (quantityValue == "few") { quantity = PluralsQuantity.Few; } else if (quantityValue == "many") { quantity = PluralsQuantity.Many; } else if (quantityValue == "other") { quantity = PluralsQuantity.Other; } else { Debug.LogError("Unknown attribute \"quantity\" value \"" + quantityValue + "\" for tag <item> in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } if (quantity != PluralsQuantity.Count) { if (values[(int)quantity] == null) { values[(int)quantity] = Utils.processTokenValue(reader.ReadString()); } else { Debug.LogError("Duplicate <item> tag with attribute \"quantity\" value \"" + quantityValue + "\" in tag <plurals> with name \"" + tokenName + "\" in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <plurals> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "plurals") { break; } } } pluralsNames.Add(tokenName); int index; if (tokenIds[2].TryGetValue(tokenName, out index)) { if (res.pluralsValues[index] == null) { res.pluralsValues[index] = values; } } else { Debug.LogError("Tag <plurals> with unknown token name \"" + tokenName + "\" found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } else { Debug.LogError("Unexpected tag <" + reader.Name + "> found in tag <resources> in \"Assets/Resources/" + xmlResPath + ".xml\""); } } } break; } } if (!resourcesFound) { Debug.LogError("Tag <resources> not found in \"Assets/Resources/" + xmlResPath + ".xml\""); } } catch (Exception e) { Debug.LogError("Exception occured while parsing \"Assets/Resources/" + xmlResPath + ".xml\""); Debug.LogException(e); } finally { if (reader != null) { reader.Close(); } } } if (locale != "") { int index = locale.LastIndexOf('-'); if (index < 0) { break; } locale = locale.Remove(index); } else { break; } } while (true); return res; }