/// ------------------------------------------------------------------------------------ /// <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(XLiffTransUnit 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 cause of a previous bug), because the XLiff of language X should //surely take precedence, as the translation for that language. existingTu.AddOrReplaceVariant(variantToAdd); TranslationsById[tu.Id] = variantToAdd.Value; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Updates the value for the specified translation unit with the specified new value. /// </summary> /// ------------------------------------------------------------------------------------ private XLiffTransUnit UpdateValue(XLiffDocument xliffTarget, XLiffTransUnit tuSource, string newValue, LocalizingInfo locInfo, string tuId) { // One would think there would be a source XLiffTransUnit, but that isn't necessarily true // with users editing interactively and adding tooltips or shortcuts. Debug.Assert(tuSource == null || tuId == tuSource.Id); Debug.Assert(tuId.StartsWith(locInfo.Id)); var tuTarget = xliffTarget.GetTransUnitForId(tuId); // If the XLiffTransUnit exists in the target language, check whether we're removing the translation // instead of adding or changing it. if (tuTarget != null) { var tuvTarg = tuTarget.GetVariantForLang(locInfo.LangId); if (tuvTarg != null) { // don't need to update if the value hasn't changed if (tuvTarg.Value == newValue) { return(tuTarget); } if (string.IsNullOrEmpty(newValue)) { _updated = true; tuTarget.RemoveVariant(tuvTarg); if ((tuTarget.Source == null || string.IsNullOrEmpty(tuTarget.Source.Value)) && (tuTarget.Target == null || string.IsNullOrEmpty(tuTarget.Target.Value))) { xliffTarget.RemoveTransUnit(tuTarget); tuTarget = null; } } } } // If we're removing an existing translation, we can quit now. if (string.IsNullOrEmpty(newValue)) { xliffTarget.File.Body.TranslationsById.TryRemove(tuId, out _); return(tuTarget); } // If the XLiffTransUnit does not exist in the target language yet, create it and fill in the // source language value (if any). if (tuTarget == null) { tuTarget = new XLiffTransUnit(); tuTarget.Id = tuId; tuTarget.Dynamic = locInfo.DiscoveredDynamically; xliffTarget.AddTransUnit(tuTarget); if (tuSource != null && locInfo.LangId != _defaultLang) { var tuvSrc = tuSource.GetVariantForLang(_defaultLang); if (tuvSrc != null && !string.IsNullOrEmpty(tuvSrc.Value)) { tuTarget.AddOrReplaceVariant(_defaultLang, tuvSrc.Value); } } tuTarget.AddNote("ID: " + tuId); } tuTarget.AddOrReplaceVariant(locInfo.LangId, newValue); xliffTarget.File.Body.TranslationsById[tuId] = newValue; _updated = true; return(tuTarget); }
string GetDefaultVariantValue(XLiffTransUnit tu) { var variant = tu.GetVariantForLang(LocalizationManager.kDefaultLang); return(variant?.Value); }