Skip to content
Snippets Groups Projects

Finalize the Great Translation Purge

Merged Chris Oelmueller requested to merge refactor/translation-purge into master
3 files
+ 28
15
Compare changes
  • Side-by-side
  • Inline
Files
3
# Internationalization
We want the code to be free from German-specific texts to enable non-German speakers to use the site and to make it easier for groups in other countries to use the code.
Unfortunately, we are not there yet. At the moment there are three ways how fixed text on the website is given in the source code:
Unfortunately, we are not quite there yet, but a lot of progress has been made and the remaining hardcoded strings are indicative of obvious legacy code.
- Hardcoded text (obviously legacy code), e.g.
We use the Symfony `TranslatorInterface` to replace translation keys (given in code) with their translated values. Translations are read from files in YAML format: for German that would be `/translations/messages.de.yml`.
Aside from those German strings, there are four mail ways in which fixed text on the website can be given in the source code with our translation mechanisms:
- In PHP frontend/view code
Classes derived from Control and View already contain the TranslatorInterface, which is accessible as `$this->translator`, and its `trans()` function that resolves translation keys.
Other helpers are including it like this:
```
use Symfony\Contracts\Translation\TranslatorInterface;
// instantiate or inject (usually autowired)
$this->translator->trans('fsp.acceptName', ['{name}' => $foodSharePoint['name']])
```
- From JavaScript code
In `client/src/i18n.js` we build a basic replacement function that imports translation resources, replaces variables if needed, and returns the translated value if one exists. Fallbacks are implemented for English and German, which are currently the only languages for which we bundle translations.
To use this function elsewhere, import the module like so:
```
$this->pageHelper->addContent('<...>Updates-Übersicht<...>')
import i18n from '@/i18n'
pulseInfo(i18n('basket.not_active'))
```
Do not create more of this hardcoded German!
- References to the translation files in `/lang/DE/....php` via the function `s` in `/src/Utility/TranslationHelper.php`, e.g.
- As part of Vue templates
In `client/src/vue.js` we define the `$i18n` prototype and attach it to all Vue templates. This is the preferred way of frontend translation. If you're translating component parameters, make sure to :bind them accordingly: `:title="$i18n('button.clear_filter')"`
- As part of Twig templates (Legacy)
The [`trans` filter](https://symfony.com/doc/current/translation/templates.html#using-twig-filters) allows passing translation keys to the translation engine from Twig files. Examples:
```
$this->translationHelper->s('close_foodbaskets')
{% embed 'components/field.twig' with {'title': 'events.bread'|trans} %}
<h4>{{ 'dashboard.push.title'|trans }}</h4>
```
- References to the translation file `/lang/lang.de.yml`.
TODO: describe how to use this data. It has something to do with symfony.
This is the way to go when you create new text on the website.
Do not create more of these unless you really know what you're doing! You will need to be very careful with the syntax for replacing variables (`|trans({'%name%': name})`).
Loading