Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: It was not possible to choose which documenattion items will be included in localization files #2455

Merged
merged 2 commits into from Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/Origam.Common/OrigamSettings.cs
Expand Up @@ -63,6 +63,7 @@ public OrigamSettings(string name)
int roundRobinBatchSize,
string slogan,
string localizationFolder,
string localizationIncludedDocumentationElements,
bool executeUpgradeScriptsOnStart,
int exportRecordsLimit,
string helpUrl,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; } = "";

Expand Down Expand Up @@ -331,6 +337,7 @@ public object Clone()
this.RoundRobinBatchSize,
this.Slogan,
this.LocalizationFolder,
this.LocalizationIncludedDocumentationElements,
this.ExecuteUpgradeScriptsOnStart,
this.ExportRecordsLimit,
this.HelpUrl,
Expand Down
4 changes: 3 additions & 1 deletion backend/Origam.DA.Service/LocalizationCache.cs
Expand Up @@ -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());
}
Expand Down
198 changes: 107 additions & 91 deletions backend/Origam.OrigamEngine/TranslationBuilder.cs
Expand Up @@ -28,6 +28,7 @@
using Origam.DA.ObjectPersistence;
using Origam.Schema;
using Origam.Workbench.Services;
using System.Linq;

namespace Origam.OrigamEngine
{
Expand All @@ -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<AbstractSchemaItem> list = persistence
.SchemaProvider
.RetrieveListByPackage<AbstractSchemaItem>(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<AbstractSchemaItem> list = persistence
.SchemaProvider
.RetrieveListByPackage<AbstractSchemaItem>(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();
}
}
}