Skip to content

Commit

Permalink
Merge pull request #344 from klee-contrib/gideruette/issue174
Browse files Browse the repository at this point in the history
Gideruette/issue174
  • Loading branch information
gideruette authored Apr 23, 2024
2 parents 52da6df + 82e867d commit ca15fe3
Show file tree
Hide file tree
Showing 43 changed files with 1,108 additions and 63 deletions.
5 changes: 5 additions & 0 deletions TopModel.Core/Model/I18nConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public class I18nConfig
/// Si les libellés des listes de références doivent être traduits.
/// </summary>
public bool TranslateReferences { get; set; } = true;

/// <summary>
/// Si les libellés des propriétés doivent être traduits.
/// </summary>
public bool TranslateProperties { get; set; } = true;
}
5 changes: 5 additions & 0 deletions TopModel.Core/schema.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
"type": "boolean",
"description": "Si les libellés des listes de références doivent être traduits.",
"default": "true"
},
"translateProperties": {
"type": "boolean",
"description": "Si les libellés des propriétés doivent être traduits.",
"default": "true"
}
}
},
Expand Down
10 changes: 10 additions & 0 deletions TopModel.Generator.Core/GeneratorConfigBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public abstract class GeneratorConfigBase
public string Language { get; set; }
#nullable enable

/// <summary>
/// Si les libellés des listes de références doivent être traduits.
/// </summary>
public bool? TranslateReferences { get; set; }

/// <summary>
/// Si les libellés des propriétés doivent être traduits.
/// </summary>
public bool? TranslateProperties { get; set; }

/// <summary>
/// Générateurs désactivés.
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion TopModel.Generator.Core/TranslationGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public TranslationGeneratorBase(ILogger<TranslationGeneratorBase<T>> logger, Tra
.Where(c => c.Tags.Contains(tag))
.SelectMany(c => c.Properties.OfType<IFieldProperty>());

return properties.SelectMany(p => GetResourceFileNames(p, tag))
return properties
.SelectMany(p => GetResourceFileNames(p, tag))
.Concat(properties.SelectMany(p => GetCommentResourceFileNames(p, tag)))
.Concat(GetMainResourceFileNames(tag))
.Select(p => p.FilePath);
Expand Down Expand Up @@ -95,6 +96,11 @@ protected virtual void HandleMainResourceFile(string mainFilePath, IEnumerable<(

private IEnumerable<(string Lang, string FilePath)> GetCommentResourceFileNames(IFieldProperty property, string tag)
{
if (Config.TranslateProperties != true)
{
return [];
}

return _translationStore.Translations
.Select(lang => (lang: lang.Key, file: GetCommentResourceFilePath(property.CommentResourceProperty, tag, lang.Key)!))
.Where(g => g.file != null);
Expand All @@ -109,6 +115,11 @@ protected virtual void HandleMainResourceFile(string mainFilePath, IEnumerable<(

private IEnumerable<(string Lang, string FilePath)> GetResourceFileNames(IFieldProperty property, string tag)
{
if (Config.TranslateProperties != true && (Config.TranslateReferences != true || !(property.Class?.Values.Any() ?? false)))
{
return [];
}

return _translationStore.Translations
.Select(lang => (lang: lang.Key, file: GetResourceFilePath(property.ResourceProperty, tag, lang.Key)!))
.Where(g => g.file != null);
Expand Down
10 changes: 10 additions & 0 deletions TopModel.Generator.Csharp/csharp.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
"type": "string",
"description": "Racine du répertoire de génération."
},
"translateReferences": {
"type": "boolean",
"description": "Si les libellés des listes de références doivent être traduits.",
"default": "true"
},
"translateProperties": {
"type": "boolean",
"description": "Si les libellés des propriétés doivent être traduits.",
"default": "true"
},
"ignoreDefaultValues": {
"type": "boolean",
"description": "Désactive la génération des valeurs par défaut des propriétés dans les classes et endpoints générés avec cette configuration."
Expand Down
2 changes: 1 addition & 1 deletion TopModel.Generator.Javascript/GeneratorRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Register(IServiceCollection services, JavascriptConfig config, int n
}
}

if (config.ResourceRootPath != null)
if (config.ResourceRootPath != null && config.TranslateProperties == true || config.TranslateReferences == true)
{
services.AddGenerator<JavascriptResourceGenerator, JavascriptConfig>(config, number);
}
Expand Down
27 changes: 15 additions & 12 deletions TopModel.Generator.Javascript/JavascriptResourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,26 @@ private void WriteClasseNode(FileWriter fw, IGrouping<IPropertyContainer, IField
fw.WriteLine(indentLevel, $"{Quote(container.Key.NameCamel)}: {{");

var i = 1;
foreach (var property in container.OrderBy(p => p.NameCamel, StringComparer.Ordinal))
if (Config.TranslateProperties == true)
{
var translation = isComment
? property.CommentResourceProperty.Comment.Replace(Environment.NewLine, " ").Replace("\"", "'")
: _translationStore.GetTranslation(property, lang);
if (translation == string.Empty)
foreach (var property in container.OrderBy(p => p.NameCamel, StringComparer.Ordinal))
{
translation = property.Name;
}
var translation = isComment
? property.CommentResourceProperty.Comment.Replace(Environment.NewLine, " ").Replace("\"", "'")
: _translationStore.GetTranslation(property, lang);

if (translation == string.Empty)
{
translation = property.Name;
}

fw.Write(indentLevel + 1, $"{Quote(property.NameCamel)}: ");
fw.Write($@"""{translation}""");
fw.WriteLine(container.Count() == i++ && !(_modelConfig.I18n.TranslateReferences && container.Key is Class { DefaultProperty: not null, Enum: true } && ((container.Key as Class)?.Values.Any() ?? false)) ? string.Empty : ",");
fw.Write(indentLevel + 1, $"{Quote(property.NameCamel)}: ");
fw.Write($@"""{translation}""");
fw.WriteLine(container.Count() == i++ && !(Config.TranslateReferences == true && container.Key is Class { DefaultProperty: not null, Enum: true } && ((container.Key as Class)?.Values.Any() ?? false)) ? string.Empty : ",");
}
}

if (_modelConfig.I18n.TranslateReferences && container.Key is Class { DefaultProperty: not null, Enum: true } classe && (classe?.Values.Any() ?? false))
if (Config.TranslateReferences == true && container.Key is Class { DefaultProperty: not null, Enum: true } classe && (classe?.Values.Any() ?? false))
{
i = 1;
fw.WriteLine(indentLevel + 1, @$"{Quote("values")}: {{");
Expand Down
36 changes: 29 additions & 7 deletions TopModel.Generator.Javascript/TypescriptDefinitionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,25 @@ protected override void HandleClass(string fileName, Class classe, string tag)
fw.WriteLine($" defaultValue: {defaultValue},");
}

fw.WriteLine($" label: \"{field.ResourceKey}\"{(Config.GenerateComments ? "," : string.Empty)}");
if (Config.TranslateProperties == true)
{
fw.WriteLine($" label: \"{field.ResourceKey}\"{(Config.GenerateComments ? "," : string.Empty)}");
}
else
{
fw.WriteLine($" label: \"{field.Label}\"{(Config.GenerateComments ? "," : string.Empty)}");
}

if (Config.GenerateComments)
{
fw.WriteLine($" comment: \"{field.CommentResourceKey}\"");
if (Config.TranslateProperties == true)
{
fw.WriteLine($" comment: \"{field.Comment}\"");
}
else
{
fw.WriteLine($" comment: \"{field.CommentResourceKey}\"");
}
}
}
else if (property is CompositionProperty cp3 && cp3.Domain != null && !Config.IsListComposition(cp3))
Expand All @@ -212,11 +226,19 @@ protected override void HandleClass(string fileName, Class classe, string tag)
fw.Write(cp3.Domain.Name);
fw.Write(",\r\n isRequired: true");
fw.Write(",\r\n label: \"");
fw.Write(classe.Namespace.ModuleCamel);
fw.Write(".");
fw.Write(classe.NameCamel);
fw.Write(".");
fw.Write(property.NameCamel);
if (Config.TranslateProperties == true)
{
fw.Write(classe.Namespace.ModuleCamel);
fw.Write(".");
fw.Write(classe.NameCamel);
fw.Write(".");
fw.Write(property.NameCamel);
}
else
{
fw.Write(property.Label);
}

fw.Write("\"\r\n");
}
else if (property is CompositionProperty cp2 && cp2.Composition.Name != classe.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void WriteReferenceValues(FileWriter fw, Class reference)
{
fw.WriteLine(" {");
fw.Write(" ");
fw.Write(string.Join(",\n ", refValue.Value.Where(p => p.Value != "null").Select(property => $"{property.Key.NameCamel}: {(Config.GetImplementation(property.Key.Domain)?.Type == "string" ? @$"""{(_modelConfig.I18n.TranslateReferences && property.Key == property.Key.Class.DefaultProperty ? refValue.ResourceKey : property.Value)}""" : @$"{property.Value}")}")));
fw.Write(string.Join(",\n ", refValue.Value.Where(p => p.Value != "null").Select(property => $"{property.Key.NameCamel}: {(Config.GetImplementation(property.Key.Domain)?.Type == "string" ? @$"""{(Config.TranslateReferences == true && property.Key == property.Key.Class.DefaultProperty ? refValue.ResourceKey : property.Value)}""" : @$"{property.Value}")}")));
fw.WriteLine();
fw.WriteLine(" },");
}
Expand Down
5 changes: 5 additions & 0 deletions TopModel.Generator.Javascript/javascript.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
"description": "Si les listes de référence doivent être traduites",
"default": "true"
},
"translateProperties": {
"type": "boolean",
"description": "Si les libellés des propriétés doivent être traduits",
"default": "true"
},
"generateComments": {
"type": "boolean",
"description": "Ajoute les commentaires dans les entités JS générées."
Expand Down
6 changes: 3 additions & 3 deletions TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List<Class> availa
foreach (var prop in classe.GetProperties(availableClasses).OfType<IFieldProperty>()
.Where(p => p != codeProperty))
{
var isString = _config.GetType((IFieldProperty)prop) == "String";
var value = refValue.Value.ContainsKey((IFieldProperty)prop) ? refValue.Value[(IFieldProperty)prop] : "null";
var isString = _config.GetType(prop) == "String";
var value = refValue.Value.ContainsKey(prop) ? refValue.Value[prop] : "null";
if (prop is AssociationProperty ap && codeProperty.PrimaryKey && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property) && r.Value[ap.Property] == value))
{
value = ap.Association.NamePascal + "." + value;
Expand All @@ -59,7 +59,7 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List<Class> availa
value = _config.GetType(prop) + "." + value;
}

if (modelConfig.I18n.TranslateReferences && classe.DefaultProperty == prop && !_config.CanClassUseEnums(classe, prop: prop))
if (_config.TranslateReferences == true && classe.DefaultProperty == prop && !_config.CanClassUseEnums(classe, prop: prop))
{
value = refValue.ResourceKey;
}
Expand Down
11 changes: 7 additions & 4 deletions TopModel.Generator.Jpa/JpaResourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,18 @@ protected override void HandleResourceFile(string filePath, string lang, IEnumer
/// <param name="container">Classe.</param>
private void WriteClasse(FileWriter fw, IGrouping<IPropertyContainer, IFieldProperty> container, string lang)
{
foreach (var property in container)
if (Config.TranslateProperties == true)
{
if (property.Label != null)
foreach (var property in container)
{
fw.WriteLine($"{property.ResourceKey}={_translationStore.GetTranslation(property, lang)}");
if (property.Label != null)
{
fw.WriteLine($"{property.ResourceKey}={_translationStore.GetTranslation(property, lang)}");
}
}
}

if (container.Key is Class classe && classe.DefaultProperty != null)
if (container.Key is Class classe && classe.DefaultProperty != null && Config.TranslateReferences == true)
{
foreach (var val in classe.Values)
{
Expand Down
10 changes: 10 additions & 0 deletions TopModel.Generator.Jpa/jpa.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
"type": "string",
"description": "Racine du répertoire de génération."
},
"translateReferences": {
"type": "boolean",
"description": "Si les libellés des listes de références doivent être traduits.",
"default": "true"
},
"translateProperties": {
"type": "boolean",
"description": "Si les libellés des propriétés doivent être traduits.",
"default": "true"
},
"ignoreDefaultValues": {
"type": "boolean",
"description": "Désactive la génération des valeurs par défaut des propriétés dans les classes et endpoints générés avec cette configuration."
Expand Down
10 changes: 10 additions & 0 deletions TopModel.Generator.Php/php.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
"type": "string",
"description": "Racine du répertoire de génération."
},
"translateReferences": {
"type": "boolean",
"description": "Si les libellés des listes de références doivent être traduits.",
"default": "true"
},
"translateProperties": {
"type": "boolean",
"description": "Si les libellés des propriétés doivent être traduits.",
"default": "true"
},
"ignoreDefaultValues": {
"type": "boolean",
"description": "Désactive la génération des valeurs par défaut des propriétés dans les classes et endpoints générés avec cette configuration."
Expand Down
1 change: 1 addition & 0 deletions TopModel.Generator.Sql/GeneratorRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Register(IServiceCollection services, SqlConfig config, int number)
CombinePath(config.OutputDirectory, config.Procedural, c => c.TypeFile);
CombinePath(config.OutputDirectory, config.Procedural, c => c.UniqueKeysFile);
CombinePath(config.OutputDirectory, config.Procedural, c => c.CommentFile);
CombinePath(config.OutputDirectory, config.Procedural, c => c.ResourceFile);

services.AddGenerator<ProceduralSqlGenerator, SqlConfig>(config, number);
}
Expand Down
Loading

0 comments on commit ca15fe3

Please sign in to comment.