Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

templates: Support context variables in Handlebars {{t}} helper.

Anders Kaseorgba5a2c8866ff
diff --git a/docs/translating/internationalization.md b/docs/translating/internationalization.md
index 5bcfd54a6a7..2e4b40121a2 100644
--- a/docs/translating/internationalization.md
+++ b/docs/translating/internationalization.md
@@ -258,6 +258,8 @@ Handlebars [helpers][] that Zulip registers. The syntax for simple strings is:
```html+handlebars
{{t 'English text' }}
+
+{{t 'Block of English text with a {variable}.' }}
```
If you are passing a translated string to a Handlebars partial, you can use:
@@ -268,17 +270,17 @@ If you are passing a translated string to a Handlebars partial, you can use:
}}
```
-The syntax for block strings or strings containing variables is:
+The syntax for HTML strings is:
<!-- The html+handlebars lexer fails to lex the single braces. -->
```text
{{#tr}}
- Block of English text.
+ <p>Block of English text.</p>
{{/tr}}
{{#tr}}
- Block of English text with a {variable}.
+ <p>Block of English text with a {variable}.</p>
{{/tr}}
```
diff --git a/web/src/templates.js b/web/src/templates.js
index 11fd72100cf..4cb4ece6a65 100644
--- a/web/src/templates.js
+++ b/web/src/templates.js
@@ -51,13 +51,19 @@ Handlebars.registerHelper({
},
});
-Handlebars.registerHelper("t", (message) => {
+Handlebars.registerHelper("t", function (message) {
// Marks a string for translation.
- // Example usage:
+ // Example usage 1:
// {{t "some English text"}}
+ //
+ // Example usage 2:
+ // {{t "This {variable} will get value from the current context"}}
+ //
+ // Note: use `{` and `}` instead of `{{` and `}}` to declare
+ // variables.
const descriptor = {id: message, defaultMessage: message};
- return intl.formatMessage(descriptor);
+ return intl.formatMessage(descriptor, this);
});
Handlebars.registerHelper("tr", function (options) {
@@ -90,7 +96,7 @@ Handlebars.registerHelper("tr", function (options) {
]),
),
...Object.fromEntries(
- Object.entries(this ?? {}).map(([key, value]) => [
+ Object.entries(this).map(([key, value]) => [
key,
Handlebars.Utils.escapeExpression(value),
]),