/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the localized value for the specified id. If formatForDisplay is true, then /// the string will have ampersands and newlines converted so the text is displayed /// nicely at runtime. /// </summary> /// ------------------------------------------------------------------------------------ public string GetValueForExactLangAndId(string langId, string id, bool formatForDisplay) { var tu = TmxDocument.GetTransUnitForId(id); if (tu == null) { return(null); } var tuv = tu.GetVariantForLang(langId); if (tuv == null) { return(null); } var value = tuv.Value; if (value == null) { return(null); } if (formatForDisplay && s_literalNewline != null) { value = value.Replace(s_literalNewline, kOSRealNewline); } if (formatForDisplay && _ampersandReplacement != null) { value = value.Replace(_ampersandReplacement, "&"); } return(value); }
/// <summary> /// Processes the TU element and write the TUV segment data into the output files. /// </summary> /// <param name="tmxtuElement">The TU element.</param> private void ProcessTuElement(TmxDocument tmxtuElement) { // Extract first level nodes of current <tu> element. TmxNodeCollection nodes = tmxtuElement.Nodes.FindByName(Token.TUV, true); List <KeyValuePair <string, TmxNodeCollection> > tuvNodeList = new List <KeyValuePair <string, TmxNodeCollection> >(); // Iterate through the TUV nodes in the TU element and create a list of source and target language TUV nodes. // It is expected that a single TU element will contain TUV elements of only 2 languages. foreach (TmxNode node in nodes) { TmxElement element = (TmxElement)node; TmxAttribute langAttr = element.Attributes["lang"] ?? element.Attributes["xml:lang"]; if (langAttr != null) { tuvNodeList.Add(new KeyValuePair <string, TmxNodeCollection>(LanguageMapper.MapLanguage(langAttr.Value.ToLowerInvariant()), element.Nodes)); } } // Create groups for the TUV nodes based on their language code. var tuvLangGroups = from tuvNode in tuvNodeList group tuvNode by tuvNode.Key into tuvLangGroup select tuvLangGroup; // If the TU element contains multiple TUV elements of the same language then we need to add duplicate // sentences for other language TUV elements in the final output file. // Basically we will create a Cartesian product of the different language TUV elements. // e.g. - Language1 = { a1 } // Language2 = { b1, b2 } // Language3 = { c1, c2 } // The following pairs are generated via the Cartesian product // => { a1, b1, c1 } // => { a1, b1, c2 } // => { a1, b2, c1 } // => { a1, b2, c2 } // This LINQ uses the Aggregate method to create the Cartesian product of the TUV groups per language. // The Aggregate method iterates over the TUV language groups and collects the result set of the lambda // function in the accumulatorCollection. This collection is given an initial seed collection via the first argument. IEnumerable <IEnumerable <KeyValuePair <string, TmxNodeCollection> > > seedCollection = new[] { Enumerable.Empty <KeyValuePair <string, TmxNodeCollection> >() }; var finalCollection = tuvLangGroups.Aggregate( seedCollection, (accumulatorCollection, currentInput) => { return(from previousItem in accumulatorCollection from currentItem in currentInput select previousItem.Concat(new[] { currentItem })); }); // Iterate over the the language product and process the TUV element and dump the segment data into the corresponding output files. foreach (var tuvLanguageProduct in finalCollection) { foreach (var tuvLanguageItem in tuvLanguageProduct) { this.ProcessTuvTag(tuvLanguageItem.Value, tuvLanguageItem.Key); } } }
/// <summary> /// Saves content as sentence-by-sentence files /// </summary> /// <param name="outputFilePath">The base name and extension for the text files</param> /// <returns>Array of text file names.</returns> public string[] WriteToSNTFiles(string outputFilePath) { string folder = Path.GetDirectoryName(outputFilePath); string fileName = Path.GetFileNameWithoutExtension(outputFilePath); string extension = string.Empty; if (Path.HasExtension(outputFilePath)) { extension = Path.GetExtension(outputFilePath); } string[] outputFiles = null; try { this.writers = new SntWriterContainer(Path.Combine(folder, fileName), extension); using (TmxReader rdr = new TmxReader(this.streamReader)) { foreach (TmxTag tmxTag in rdr.TmxTags) { switch (tmxTag.TmxTagType) { case TmxTagType.TU: TmxDocument tmxtuDoc = TmxDocument.Create(tmxTag.Value, true); this.ProcessTuElement(tmxtuDoc); break; ////case TmxTagType.XML: ////case TmxTagType.DOCTYPE: ////case TmxTagType.TMX_OPEN: ////case TmxTagType.HEADER: ////case TmxTagType.BODY_OPEN: default: break; } } } outputFiles = this.writers.Files; } catch { throw; } finally { if (this.writers != null) { this.writers.Dispose(); this.writers = null; } } return(outputFiles); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the category for the specified id. /// </summary> /// ------------------------------------------------------------------------------------ internal LocalizationCategory GetCategory(string id) { TMXTransUnit tu = TmxDocument.GetTransUnitForId(id); if (tu != null) { string category = tu.GetPropValue(kCategoryPropTag); try { return((LocalizationCategory)Enum.Parse(typeof(LocalizationCategory), category)); } catch { } } return(LocalizationCategory.Other); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the priority for the specified id. /// </summary> /// ------------------------------------------------------------------------------------ internal LocalizationPriority GetPriority(string id) { TMXTransUnit tu = TmxDocument.GetTransUnitForId(id); if (tu != null) { string priority = tu.GetPropValue(kPriorityPropTag); try { return((LocalizationPriority)Enum.Parse(typeof(LocalizationPriority), priority)); } catch { } } return(LocalizationPriority.NotLocalizable); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Saves the cache to the file from which the cache was originally loaded, but only /// if the cache is dirty. If the cache is dirty and saved, then true is returned. /// Otherwise, false is returned. /// </summary> /// ------------------------------------------------------------------------------------ internal void SaveIfDirty(ICollection <string> langIdsToForceCreate) { if (!IsDirty) { return; } StringBuilder errorMsg = null; // ToArray() prevents exception "Collection was modified" in rare cases (e.g., Bloom BL-2400). foreach (var langId in TmxDocument.GetAllVariantLanguagesFound().ToArray()) { try { SaveFileForLangId(langId, langIdsToForceCreate != null && langIdsToForceCreate.Contains(langId)); } catch (Exception e) { if (e is SecurityException || e is UnauthorizedAccessException || e is IOException) { if (errorMsg == null) { errorMsg = new StringBuilder(); errorMsg.AppendLine("Failed to save localization changes in the following files:"); } errorMsg.AppendLine(); errorMsg.Append("File: "); errorMsg.AppendLine(OwningManager.GetTmxPathForLanguage(langId, true)); errorMsg.Append("Error Type: "); errorMsg.AppendLine(e.GetType().ToString()); errorMsg.Append("Message: "); errorMsg.AppendLine(e.Message); } } } if (errorMsg != null) { throw new IOException(errorMsg.ToString()); } IsDirty = false; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the group for the specified id. /// </summary> /// ------------------------------------------------------------------------------------ internal string GetGroup(string id) { TMXTransUnit tu = TmxDocument.GetTransUnitForId(id); return(tu == null ? null : tu.GetPropValue(kGroupPropTag)); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the comment for the specified id. /// </summary> /// ------------------------------------------------------------------------------------ public string GetComment(string id) { TMXTransUnit tu = TmxDocument.GetTransUnitForId(id); return(tu == null || tu.Notes.Count == 0 ? null : tu.Notes[0].Text); }