diff --git a/backend/Origam.Common/OrigamSettings.cs b/backend/Origam.Common/OrigamSettings.cs index e3facf3bc5..1369276313 100644 --- a/backend/Origam.Common/OrigamSettings.cs +++ b/backend/Origam.Common/OrigamSettings.cs @@ -63,6 +63,7 @@ public OrigamSettings(string name) int roundRobinBatchSize, string slogan, string localizationFolder, + string localizationIncludedDocumentationElements, bool executeUpgradeScriptsOnStart, int exportRecordsLimit, string helpUrl, @@ -97,6 +98,7 @@ public OrigamSettings(string name) this.WorkQueueProcessingMode = workQueueProcessingMode; this.Slogan = slogan; this.LocalizationFolder = localizationFolder; + this.LocalizationIncludedDocumentationElements = localizationIncludedDocumentationElements; this.ExecuteUpgradeScriptsOnStart = executeUpgradeScriptsOnStart; this.ExportRecordsLimit = exportRecordsLimit; this.HelpUrl = helpUrl; @@ -187,7 +189,11 @@ public override string ToString() [Category("Localization")] public string LocalizationFolder { get; set; } = ""; - [Category("Localization")] + [Category("Localization")] + [Description("Comma separated names of documentation categories to be include in the generated localization files e.g. USER_SHORT_HELP,USER_LONG_HELP")] + public string LocalizationIncludedDocumentationElements { get; set; } = ""; + + [Category("Localization")] [Description("List of languages that will be used when generating translation files in Architect. Comma separated e.g. en-US,de-DE.")] public string TranslationBuilderLanguages { get; set; } = ""; @@ -331,6 +337,7 @@ public object Clone() this.RoundRobinBatchSize, this.Slogan, this.LocalizationFolder, + this.LocalizationIncludedDocumentationElements, this.ExecuteUpgradeScriptsOnStart, this.ExportRecordsLimit, this.HelpUrl, diff --git a/backend/Origam.DA.Service/LocalizationCache.cs b/backend/Origam.DA.Service/LocalizationCache.cs index 3e8c6711fe..598e7a60cf 100644 --- a/backend/Origam.DA.Service/LocalizationCache.cs +++ b/backend/Origam.DA.Service/LocalizationCache.cs @@ -156,7 +156,9 @@ private void LoadFile(string path) } string memberValue = it.Current.Value; - + if (element.ContainsKey(memberName)) { + throw new Exception($"Error when loading file {path}. Element id {id} contains category {memberName} multiple times. Please remove the duplicates from the file and try again."); + } element.Add(memberName, memberValue); } while (it.Current.MoveToNext()); } diff --git a/backend/Origam.OrigamEngine/TranslationBuilder.cs b/backend/Origam.OrigamEngine/TranslationBuilder.cs index cdd2793e49..3894427ee4 100644 --- a/backend/Origam.OrigamEngine/TranslationBuilder.cs +++ b/backend/Origam.OrigamEngine/TranslationBuilder.cs @@ -28,6 +28,7 @@ using Origam.DA.ObjectPersistence; using Origam.Schema; using Origam.Workbench.Services; +using System.Linq; namespace Origam.OrigamEngine { @@ -37,95 +38,110 @@ namespace Origam.OrigamEngine public static class TranslationBuilder { public static void Build(Stream stream, LocalizationCache currentTranslations, string locale, Guid packageId) - { - XmlTextWriter xtw = new XmlTextWriter(stream, System.Text.Encoding.UTF8); - xtw.Formatting = Formatting.Indented; - xtw.WriteStartDocument(true); - xtw.WriteStartElement("OrigamLocalization"); - - IPersistenceService persistence = ServiceManager.Services.GetService(typeof(IPersistenceService)) as IPersistenceService; - IDocumentationService docSvc = ServiceManager.Services.GetService(typeof(IDocumentationService)) as IDocumentationService; - - List list = persistence - .SchemaProvider - .RetrieveListByPackage(packageId); - - foreach(AbstractSchemaItem item in list) - { - IList memberList = Reflector.FindMembers(item.GetType(), typeof(LocalizableAttribute), new Type[]{}); - Hashtable values = new Hashtable(); - IQueryLocalizable ql = item as IQueryLocalizable; - - foreach(MemberAttributeInfo mai in memberList) - { - bool isLocalizable = true; - - if(ql != null) - { - isLocalizable = ql.IsLocalizable(mai.MemberInfo.Name); - } - - if(isLocalizable) - { - object translatableValue = Reflector.GetValue(mai.MemberInfo, item); - - if(translatableValue != null && translatableValue.ToString() != "") - { - values.Add(mai.MemberInfo.Name, translatableValue); - } - } - } - DocumentationComplete docData = docSvc.LoadDocumentation(item.Id); - - if(values.Count > 0 || docData.Documentation.Count > 0) - { - xtw.WriteStartElement("Element"); - xtw.WriteAttributeString("Id", item.Id.ToString()); - xtw.WriteAttributeString("Path", item.Path); - - foreach(DictionaryEntry entry in values) - { - xtw.WriteStartElement((string)entry.Key); - xtw.WriteAttributeString("OriginalText", entry.Value.ToString()); - string translation; - if(currentTranslations != null) - { - translation = currentTranslations.GetLocalizedString(item.Id, (string)entry.Key, entry.Value.ToString(), locale); - } - else - { - translation = entry.Value.ToString(); - } - xtw.WriteString(translation); - xtw.WriteEndElement(); - } - - - foreach(DocumentationComplete.DocumentationRow docRow in docData.Documentation) - { - xtw.WriteStartElement("Documentation"); - xtw.WriteAttributeString("Category", docRow.Category); - xtw.WriteAttributeString("OriginalText", docRow.Data.ToString()); - string translation; - if(currentTranslations != null) - { - translation = currentTranslations.GetLocalizedString(item.Id, "Documentation " + docRow.Category, docRow.Data.ToString(), locale); - } - else - { - translation = docRow.Data.ToString(); - } - xtw.WriteString(translation); - xtw.WriteEndElement(); - } - - xtw.WriteEndElement(); - } - } - - xtw.WriteEndElement(); - xtw.WriteEndDocument(); - xtw.Flush(); - } - } + { + XmlTextWriter xtw = new XmlTextWriter(stream, System.Text.Encoding.UTF8); + xtw.Formatting = Formatting.Indented; + xtw.WriteStartDocument(true); + xtw.WriteStartElement("OrigamLocalization"); + + IPersistenceService persistence = ServiceManager.Services.GetService(typeof(IPersistenceService)) as IPersistenceService; + IDocumentationService docSvc = ServiceManager.Services.GetService(typeof(IDocumentationService)) as IDocumentationService; + + List list = persistence + .SchemaProvider + .RetrieveListByPackage(packageId); + + foreach (AbstractSchemaItem item in list) + { + IList memberList = Reflector.FindMembers(item.GetType(), typeof(LocalizableAttribute), new Type[] { }); + Hashtable values = new Hashtable(); + IQueryLocalizable ql = item as IQueryLocalizable; + + foreach (MemberAttributeInfo mai in memberList) + { + bool isLocalizable = true; + + if (ql != null) + { + isLocalizable = ql.IsLocalizable(mai.MemberInfo.Name); + } + + if (isLocalizable) + { + object translatableValue = Reflector.GetValue(mai.MemberInfo, item); + + if (translatableValue != null && translatableValue.ToString() != "") + { + values.Add(mai.MemberInfo.Name, translatableValue); + } + } + } + DocumentationComplete docData = docSvc.LoadDocumentation(item.Id); + + if (values.Count > 0 || docData.Documentation.Count > 0) + { + xtw.WriteStartElement("Element"); + xtw.WriteAttributeString("Id", item.Id.ToString()); + xtw.WriteAttributeString("Path", item.Path); + + foreach (DictionaryEntry entry in values) + { + xtw.WriteStartElement((string)entry.Key); + xtw.WriteAttributeString("OriginalText", entry.Value.ToString()); + string translation; + if (currentTranslations != null) + { + translation = currentTranslations.GetLocalizedString(item.Id, (string)entry.Key, entry.Value.ToString(), locale); + } + else + { + translation = entry.Value.ToString(); + } + xtw.WriteString(translation); + xtw.WriteEndElement(); + } + string[] categoriesToInclude = GetDocumentationCategoriesToInclude(); + foreach (DocumentationComplete.DocumentationRow docRow in docData.Documentation) + { + if (!categoriesToInclude.Contains(docRow.Category)) { + continue; + } + xtw.WriteStartElement("Documentation"); + xtw.WriteAttributeString("Category", docRow.Category); + xtw.WriteAttributeString("OriginalText", docRow.Data.ToString()); + string translation; + if (currentTranslations != null) + { + translation = currentTranslations.GetLocalizedString(item.Id, "Documentation " + docRow.Category, docRow.Data.ToString(), locale); + } + else + { + translation = docRow.Data.ToString(); + } + xtw.WriteString(translation); + xtw.WriteEndElement(); + } + + xtw.WriteEndElement(); + } + } + + xtw.WriteEndElement(); + xtw.WriteEndDocument(); + xtw.Flush(); + } + + private static string[] GetDocumentationCategoriesToInclude() + { + OrigamSettings settings = ConfigurationManager.GetActiveConfiguration(); + if (settings.LocalizationIncludedDocumentationElements == null) { + return new string[0]; + } + return settings.LocalizationIncludedDocumentationElements + .Split(',') + .Select(x => x.Trim()) + .Where(x => x != "") + .ToArray(); + } + } }