/// <summary> /// When all but the last part of the id changed, this can help reunite things /// </summary> internal TMXTransUnit GetTransUnitForOrphan(TMXTransUnit orphan) { var terminalIdToMatch = TMXLocalizedStringCache.GetTerminalIdPart(orphan.Id); var defaultTextToMatch = GetDefaultVariantValue(orphan); return(_transUnits.FirstOrDefault(tu => TMXLocalizedStringCache.GetTerminalIdPart(tu.Id) == terminalIdToMatch && GetDefaultVariantValue(tu) == defaultTextToMatch)); }
/// ------------------------------------------------------------------------------------ private void SaveFileForLangId(string langId, bool forceCreation) { var tmxDoc = CreateEmptyStringFile(); tmxDoc.Header.SourceLang = langId; tmxDoc.Header.SetPropValue(LocalizationManager.kAppVersionPropTag, OwningManager.AppVersion); foreach (var tu in TmxDocument.Body.TransUnits) { var tuv = tu.GetVariantForLang(langId); if (tuv == null) { continue; } var newTu = new TMXTransUnit { Id = tu.Id }; tmxDoc.AddTransUnit(newTu); newTu.AddOrReplaceVariant(tu.GetVariantForLang(LocalizationManager.kDefaultLang)); newTu.AddOrReplaceVariant(tuv); newTu.Notes = tu.CopyNotes(); newTu.Props = tu.CopyProps(); } tmxDoc.Body.TransUnits.Sort(TuComparer); if (forceCreation || OwningManager.DoesCustomizedTranslationExistForLanguage(langId)) { tmxDoc.Save(OwningManager.GetTmxPathForLanguage(langId, true)); } }
private void UpdateTransUnitComment(TMXTransUnit tu, LocalizingInfo locInfo) { if (locInfo.DiscoveredDynamically && (tu.GetPropValue(TMXLocalizedStringCache.kDiscoveredDynamically) != "true")) { tu.AddProp(TMXLocalizedStringCache.kDiscoveredDynamically, "true"); _updated = true; } if ((locInfo.UpdateFields & UpdateFields.Comment) != UpdateFields.Comment) { return; } if ((tu.Notes.Count > 0) && (tu.Notes[0].Text == locInfo.Comment)) { return; } tu.Notes.Clear(); _updated = true; if (!string.IsNullOrEmpty(locInfo.Comment)) { tu.AddNote(locInfo.Comment); } }
/// ------------------------------------------------------------------------------------ /// <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> /// Updates the value for the specified translation unit with the specified new value. /// </summary> /// ------------------------------------------------------------------------------------ private TMXTransUnit UpdateValue(TMXTransUnit tu, string newValue, LocalizingInfo locInfo, string tuId) { newValue = newValue ?? string.Empty; // Get rid of the variant we are about to set if it is present. // If no variants remain get rid of the whole thing. // Later we will create whatever we need. if (tu != null) { var tuv = tu.GetVariantForLang(locInfo.LangId); if (tuv != null) { // don't need to update if the value hasn't changed if (tuv.Value == newValue) { return(tu); } _updated = true; tu.RemoveVariant(tuv); if (tu.Variants.Count == 0) { _tmxDoc.RemoveTransUnit(tu); tu = null; // so we will make a new one if needed. } } } if (newValue == string.Empty) { return(tu); } // Create a new entry if needed. if (tu == null) { tu = new TMXTransUnit(); tu.Id = tuId; _tmxDoc.AddTransUnit(tu); } tu.AddOrReplaceVariant(locInfo.LangId, newValue); _updated = true; return(tu); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Removes the specified translation unit. /// </summary> /// ------------------------------------------------------------------------------------ internal void RemoveTransUnit(TMXTransUnit tu) { if (tu == null) { return; } if (_transUnits.Contains(tu)) { _transUnits.Remove(tu); } else if (tu.Id != null) { var tmptu = GetTransUnitForId(tu.Id); if (tmptu != null) { _transUnits.Remove(tmptu); } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// If a translation unit does not already exist for the id in the specified /// translation unit, then the translation unit is added. Otherwise, if the variant /// for the specified language does not exist in the translation unit, it is added. /// </summary> /// ------------------------------------------------------------------------------------ internal void AddTransUnitOrVariantFromExisting(TMXTransUnit tu, string langId) { var variantToAdd = tu.GetVariantForLang(langId); if (variantToAdd == null || AddTransUnit(tu)) { return; } var existingTu = GetTransUnitForId(tu.Id); //notice, we don't care if there is already a string in there for this language //(that was the source of a previous bug), because the tmx of language X should //surely take precedence, as source of the translation, over other language's //tms files which, by virtue of their alphabetical order (e.g. arabic), came //first. This probably only effects English, as it has variants in all the other //languages. Previously, Arabic would be processed first, so when English came //along, it was too late. existingTu.AddOrReplaceVariant(variantToAdd); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Adds the specified translation unit. /// </summary> /// <param name="tu">The translation unit.</param> /// <returns>true if the translation unit was successfully added. Otherwise, false.</returns> /// ------------------------------------------------------------------------------------ internal bool AddTransUnit(TMXTransUnit tu) { if (tu == null || tu.IsEmpty) { return(false); } if (tu.Id == null) { tu.Id = (++_transUnitId).ToString(); } // If a translation unit with the specified id already exists, then quit here. if (GetTransUnitForId(tu.Id) != null) { return(false); } _transUnits.Add(tu); return(true); }
///// ------------------------------------------------------------------------------------ ///// <summary> ///// Saves the cache to the specified file, if the cache is dirty. If the cache is ///// dirty and saved, then true is returned. Otherwise, false is returned. ///// </summary> ///// ------------------------------------------------------------------------------------ //private bool SaveIfDirty(string tmxFile) //{ // if (!IsDirty || string.IsNullOrEmpty(tmxFile)) // return false; // //_tmxFile = tmxFile; // IsDirty = false; // TmxDocument.Body.TransUnits.Sort(TuComparer); // TmxDocument.Save(tmxFile); // return true; //} #endregion /// ------------------------------------------------------------------------------------ /// <summary> /// Compares two translation units for equality. /// </summary> /// ------------------------------------------------------------------------------------ private static int TuComparer(TMXTransUnit tu1, TMXTransUnit tu2) { if (tu1 == null && tu2 == null) { return(0); } if (tu1 == null) { return(-1); } if (tu2 == null) { return(1); } string x = tu1.GetPropValue(kGroupPropTag); string y = tu2.GetPropValue(kGroupPropTag); if (x == y) { return(String.CompareOrdinal(tu1.Id, tu2.Id)); } if (x == null) { return(-1); } if (y == null) { return(1); } return(String.CompareOrdinal(x, y)); }
private bool IsTranslationUnitNoLongerUsed(TMXTransUnit tu) { return(_englishTuIdsNoLongerUsed.Contains(tu.Id)); }
/// ------------------------------------------------------------------------------------ /// <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); }
private static string GetDefaultVariantValue(TMXTransUnit tu) { var variant = tu.GetVariantForLang(LocalizationManager.kDefaultLang); return(variant?.Value); }
/// <summary> /// When we change ids after people have already been localizing, we have a BIG PROBLEM. /// This helps with the common case were we just changed the hierarchical organizaiton of the id, /// that is, the parts of the id before th final '.'. /// </summary> public TMXTransUnit GetTransUnitForOrphan(TMXTransUnit orphan) { return(Body.GetTransUnitForOrphan(orphan)); }