/// ------------------------------------------------------------------------------------ private void SaveFileForLangId(string langId, bool forceCreation, XLiffDocument xliffOriginal) { if (!forceCreation && !OwningManager.DoesCustomizedTranslationExistForLanguage(langId)) { return; } var xliffOutput = CreateEmptyStringFile(); if (langId != LocalizationManager.kDefaultLang) { xliffOutput.File.TargetLang = langId; } xliffOutput.File.ProductVersion = OwningManager.AppVersion; xliffOutput.File.HardLineBreakReplacement = s_literalNewline; xliffOutput.File.AmpersandReplacement = _ampersandReplacement; if (OwningManager != null && OwningManager.Name != null) { xliffOutput.File.Original = OwningManager.Name + ".dll"; } foreach (var tu in DefaultXliffDocument.File.Body.TransUnits) { var tuTarget = xliffOriginal.File.Body.GetTransUnitForId(tu.Id); XLiffTransUnitVariant tuv = null; if (tuTarget != null) { tuv = tuTarget.GetVariantForLang(langId); } // REVIEW: should we write units with no translation (target)? var newTu = new XLiffTransUnit { Id = tu.Id, Dynamic = tu.Dynamic }; newTu.AddOrReplaceVariant(tu.GetVariantForLang(LocalizationManager.kDefaultLang)); if (tuv != null) { newTu.AddOrReplaceVariant(tuv); } newTu.Notes = tu.CopyNotes(); xliffOutput.AddTransUnit(newTu); } xliffOutput.File.Body.TransUnits.Sort(TuComparer); xliffOutput.Save(OwningManager.GetPathForLanguage(langId, true)); }
/// ------------------------------------------------------------------------------------ /// <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); }