Use Ructe (#327)
All the template are now compiled at compile-time with the `ructe` crate.
I preferred to use it instead of askama because it allows more complex Rust expressions, where askama only supports a small subset of expressions and doesn't allow them everywhere (for instance, `{{ macro!() | filter }}` would result in a parsing error).
The diff is quite huge, but there is normally no changes in functionality.
Fixes #161 and unblocks #110 and #273
This commit is contained in:
+14
-11
@@ -41,23 +41,26 @@ Now, make any changes to the code you want. After committing your changes, push
|
||||
|
||||
The project maintainers may suggest further changes to improve the pull request even more. After implementing this locally, you can push to your upstream fork again and the changes will immediately show up in the pull request after pushing. Once all the suggested changes are made, the pull request may be accepted. Thanks for contributing.
|
||||
|
||||
## When working with Tera templates
|
||||
## When working with Ructe templates
|
||||
|
||||
When working with the interface, or any message that will be displayed to the final user, keep in mind that Plume is an internationalized software. To make sure that the parts of the interface you are changing are translatable, you should:
|
||||
When working with the interface, or any message that will be displayed to the final user,
|
||||
keep in mind that Plume is an internationalized software.
|
||||
To make sure that the parts of the interface you are changing are translatable, you should:
|
||||
|
||||
- Use the `_` and `_n` filters instead of directly writing strings in your HTML markup
|
||||
- Wrap strings to translate in the `i18n!` macro (see [rocket_i18n docs](https://docs.rs/rocket_i18n/)
|
||||
for more details about its arguments).The `Catalog` argument is usually `ctx.1`.
|
||||
- Add the strings to translate to the `po/plume.pot` file
|
||||
|
||||
Here is an example: let's say we want to add two strings, a simple one and one that may deal with plurals. The first step is to add them to whatever template we want to display them in:
|
||||
Here is an example: let's say we want to add two strings, a simple one and one
|
||||
that may deal with plurals. The first step is to add them to whatever
|
||||
template we want to display them in:
|
||||
|
||||
```jinja
|
||||
<p>{{ "Hello, world!" | _ }}</p>
|
||||
```html
|
||||
<p>@i18n!(ctx.1, "Hello, world!")</p>
|
||||
|
||||
<p>{{ "You have {{ count }} new notifications" | _n(singular="You have one new notification", count=n_notifications) }}</p>
|
||||
<p>@i18n!(ctx.1, "You have one new notification", "You have {0} new notifications", n_notifications)</p>
|
||||
```
|
||||
|
||||
As you can see, the `_` doesn't need any special argument to work, but `_n` requires `singular` (the singular form, in English) and `count` (the number of items, to determine which form to use) to be present. Note that any parameters given to these filters can be used as regular Tera variables inside of the translated strings, like we are doing with the `count` variable in the second string above.
|
||||
|
||||
The second step is to add them to POT file. To add a simple message, just do:
|
||||
|
||||
```po
|
||||
@@ -69,7 +72,7 @@ For plural forms, the syntax is a bit different:
|
||||
|
||||
```po
|
||||
msgid "You have one new notification" # The singular form
|
||||
msgid_plural "You have {{ count }} new notifications" # The plural one
|
||||
msgid_plural "You have {0} new notifications" # The plural one
|
||||
msgstr[0] ""
|
||||
msgstr[1] ""
|
||||
```
|
||||
@@ -84,4 +87,4 @@ For CSS, the only rule is to use One True Brace Style.
|
||||
|
||||
For JavaScript, we use [the JavaScript Standard Style](https://standardjs.com/).
|
||||
|
||||
For HTML/Tera templates, we use HTML5 syntax.
|
||||
For HTML/Ructe templates, we use HTML5 syntax.
|
||||
|
||||
@@ -23,16 +23,21 @@ Sometimes, strings may change depending on a number (for instance, a post counte
|
||||
|
||||
```
|
||||
msgid "One post"
|
||||
msgid_plural "{{ count }} posts"
|
||||
msgid_plural "{0} posts"
|
||||
msgstr[0] ""
|
||||
msgstr[1] ""
|
||||
```
|
||||
|
||||
Then you should fill the two `msgstr` field, one with the singular form, the second with the plural one. If your language as more than two forms, you can add another one by following the same pattern (`msgstr[n] ""`).
|
||||
Then you should fill the two `msgstr` field, one with the singular form,
|
||||
the second with the plural one. If your language as more than two forms,
|
||||
you can add another one by following the same pattern (`msgstr[n] ""`).
|
||||
|
||||
## Interpolation
|
||||
|
||||
Strings you translate may contain data from Plume (a username for instance). To tell Plume where to put these data, surround their identifier by `{{` and `}}`. The identifier is also present in this form in the English string to translate (this what you can see above, with the `{{ count }} posts` message).
|
||||
Strings you translate may contain data from Plume (a username for instance).
|
||||
To tell Plume where to put these data, surround the number that identifies
|
||||
them by `{` and `}`. The identifier is also present in this form in the English
|
||||
string to translate (this what you can see above, with the `{0} posts` message).
|
||||
|
||||
## Note
|
||||
|
||||
|
||||
Reference in New Issue
Block a user