Skip to content

Commit

Permalink
[i18n] Gérer les traductions de listes de référence en SQL (et C#)
Browse files Browse the repository at this point in the history
Fixes #174
  • Loading branch information
gideruette committed Mar 12, 2024
1 parent 4ded351 commit c857b68
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 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 Down
9 changes: 8 additions & 1 deletion TopModel.Generator.Sql/Procedural/AbstractSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public abstract class AbstractSchemaGenerator

private readonly SqlConfig _config;
private readonly ILogger<ProceduralSqlGenerator> _logger;
private readonly ModelConfig _modelConfig;

public AbstractSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger)
public AbstractSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger, ModelConfig modelConfig)
{
_config = config;
_logger = logger;
_modelConfig = modelConfig;
}

protected ProceduralSqlConfig Config => _config.Procedural!;
Expand Down Expand Up @@ -200,6 +202,11 @@ protected static string CheckIdentifierLength(string identifier)
{
definition.TryGetValue(property, out var value);
nameValueDict[property.SqlName] = _config.GetValue(property, availableClasses, value);

if (_modelConfig.I18n.TranslateReferences && modelClass.DefaultProperty == property && !_config.CanClassUseEnums(modelClass, prop: property))
{
nameValueDict[property.SqlName] = $@"""{initItem.ResourceKey}""";
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions TopModel.Generator.Sql/Procedural/PostgreSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace TopModel.Generator.Sql.Procedural;
/// </summary>
public class PostgreSchemaGenerator : AbstractSchemaGenerator
{
public PostgreSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger)
: base(config, logger)
public PostgreSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger, ModelConfig modelConfig)
: base(config, logger, modelConfig)
{
}

Expand Down
9 changes: 6 additions & 3 deletions TopModel.Generator.Sql/Procedural/ProceduralSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ public class ProceduralSqlGenerator : GeneratorBase<SqlConfig>
{
private readonly ILogger<ProceduralSqlGenerator> _logger;

private ModelConfig _modelConfig;

private AbstractSchemaGenerator? _schemaGenerator;

public ProceduralSqlGenerator(ILogger<ProceduralSqlGenerator> logger)
public ProceduralSqlGenerator(ILogger<ProceduralSqlGenerator> logger, ModelConfig modelConfig)
: base(logger)
{
_logger = logger;
_modelConfig = modelConfig;
}

public override string Name => "ProceduralSqlGen";
Expand All @@ -36,8 +39,8 @@ private AbstractSchemaGenerator SchemaGenerator
get
{
_schemaGenerator ??= Config.TargetDBMS == TargetDBMS.Postgre
? new PostgreSchemaGenerator(Config, _logger)
: new SqlServerSchemaGenerator(Config, _logger);
? new PostgreSchemaGenerator(Config, _logger, _modelConfig)
: new SqlServerSchemaGenerator(Config, _logger, _modelConfig);

return _schemaGenerator;
}
Expand Down
4 changes: 2 additions & 2 deletions TopModel.Generator.Sql/Procedural/SqlServerSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace TopModel.Generator.Sql.Procedural;
/// </summary>
public class SqlServerSchemaGenerator : AbstractSchemaGenerator
{
public SqlServerSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger)
: base(config, logger)
public SqlServerSchemaGenerator(SqlConfig config, ILogger<ProceduralSqlGenerator> logger, ModelConfig modelConfig)
: base(config, logger, modelConfig)
{
}

Expand Down
20 changes: 10 additions & 10 deletions samples/generators/pg/src/04_references.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
-- Description : Script d'insertion des données de références
-- ===========================================================================================
/** Initialisation de la table TYPE_DROIT **/
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('READ', 'Lecture');
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('WRITE', 'Ecriture');
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('ADMIN', 'Administration');
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('READ', "securite.profil.typeDroit.values.Read");
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('WRITE', "securite.profil.typeDroit.values.Write");
INSERT INTO TYPE_DROIT(TDR_CODE, TDR_LIBELLE) VALUES('ADMIN', "securite.profil.typeDroit.values.Admin");

/** Initialisation de la table DROIT **/
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('CREATE', 'Création', 'WRITE');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('READ', 'Lecture', 'READ');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('UPDATE', 'Mise à jour', 'WRITE');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('DELETE', 'Suppression', 'ADMIN');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('CREATE', "securite.profil.droit.values.Create", 'WRITE');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('READ', "securite.profil.droit.values.Read", 'READ');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('UPDATE', "securite.profil.droit.values.Update", 'WRITE');
INSERT INTO DROIT(DRO_CODE, DRO_LIBELLE, TDR_CODE) VALUES('DELETE', "securite.profil.droit.values.Delete", 'ADMIN');

/** Initialisation de la table TYPE_UTILISATEUR **/
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('ADMIN', 'Administrateur');
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('GEST', 'Gestionnaire');
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('CLIENT', 'Client');
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('ADMIN', "securite.utilisateur.typeUtilisateur.values.Admin");
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('GEST', "securite.utilisateur.typeUtilisateur.values.Gestionnaire");
INSERT INTO TYPE_UTILISATEUR(TUT_CODE, TUT_LIBELLE) VALUES('CLIENT', "securite.utilisateur.typeUtilisateur.values.Client");

4 changes: 3 additions & 1 deletion samples/generators/pg/topmodel.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# yaml-language-server: $schema=./topmodel.config.schema.json
# yaml-language-server: $schema=./topmodel.config.schema.json
---
app: pg
modelRoot: ../../model
lockFileName: pg.topmodel.lock
i18n:
translateReferences: true
sql:
- tags:
- back
Expand Down

0 comments on commit c857b68

Please sign in to comment.