[ADD] Translation feature

This commit brings back the possibility to translate text nodes and
the attributes "label", "title", "placeholder", and "alt" in an app
configured with a suitable translation function.

It is also possible to deactivate translations under a node via
the directive t-translation="off".

For flexibility it is possible to define the list of translatable
attributes in the app.
This commit is contained in:
Mathieu Duckerts-Antoine
2021-10-13 16:49:34 +02:00
committed by Géry Debongnie
parent e2d98e4c26
commit b8649f1add
8 changed files with 363 additions and 64 deletions
+21 -1
View File
@@ -19,6 +19,7 @@ export const enum ASTType {
TLog,
TSlot,
TCallBlock,
TTranslation,
}
export interface ASTText {
@@ -131,6 +132,11 @@ export interface ASTLog {
content: AST | null;
}
export interface ASTTranslation {
type: ASTType.TTranslation;
content: AST | null;
}
export type AST =
| ASTText
| ASTComment
@@ -147,7 +153,8 @@ export type AST =
| ASTSlot
| ASTTCallBlock
| ASTLog
| ASTDebug;
| ASTDebug
| ASTTranslation;
// -----------------------------------------------------------------------------
// Parser
@@ -179,6 +186,7 @@ function parseNode(node: ChildNode, ctx: ParsingContext): AST | null {
parseTCallBlock(node, ctx) ||
parseTEscNode(node, ctx) ||
parseTKey(node, ctx) ||
parseTTranslation(node, ctx) ||
parseTSlot(node, ctx) ||
parseTRawNode(node, ctx) ||
parseComponent(node, ctx) ||
@@ -457,6 +465,7 @@ function hasNoComponent(ast: AST): boolean {
return hasNoComponent(ast.content);
case ASTType.TDebug:
case ASTType.TLog:
case ASTType.TTranslation:
return ast.content ? hasNoComponent(ast.content) : true;
case ASTType.TForEach:
return ast.hasNoComponent;
@@ -711,6 +720,17 @@ function parseTSlot(node: Element, ctx: ParsingContext): AST | null {
};
}
function parseTTranslation(node: Element, ctx: ParsingContext): AST | null {
if (node.getAttribute("t-translation") !== "off") {
return null;
}
node.removeAttribute("t-translation");
return {
type: ASTType.TTranslation,
content: parseNode(node, ctx),
};
}
// -----------------------------------------------------------------------------
// helpers
// -----------------------------------------------------------------------------