Problem
As I’ve seen in the Timber documentation it is possible to add multilanguage support like this:
// Set language to French
putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');
// Specify the location of the translation tables
bindtextdomain('myAppPhp', 'includes/locale');
bind_textdomain_codeset('myAppPhp', 'UTF-8');
// Choose domain
textdomain('myAppPhp');
{% trans "Hello World!" %}
{% trans string_var %}
{% trans %}
Hello World!
{% endtrans %}
The problem is, like this I have static site – which will be translated to French, but I’m not able to add multilanguage content to it from the wp-backend. So I’m sure that there is a better way to realize multilanguage support with a WordPress Theme written with Timber.
Question
I’d like to know what is the best and most reliable way to build a WordPress Theme with Timber and realizing multilanguage support for it.
Are there any best practices?
Multilingual WordPress is a topic where thereâs still a lot of debate about how to do it best. So currently thereâs probably not a best and most reliable way to do it.
Using multilanguage with Timber is not really different from the way youâd approach it when you build a normal WordPress theme. Be aware that Timber is not the same as Twig. Timber uses Twig, which is a templating engine to make it easier to separate data and logic from you display (HTML) concerns. So if you want to find out how to add multilanguage support, youâd first look how to do it the Timber/WordPress way. You probably donât need any of the functionality provided by Twigâs i18n Extension, with which you made your first steps.
Read the Codex page about Multilingual WordPress to get started.
Youâll probably want to use a plugin if you want to have content that is pulled from the database translated. Among the popular ones that I have used myself are:
When you have one of those installed and your content ready in multiple languages, then youâre good to go. You donât really have to change anything in your theme to make the basics work. Adding language switchers or linking to other posts outside of menus is a little different. There youâd have to refer to the documentation of the plugin youâll be using.
If you have static strings in your theme that you also want to translate, you will have to consider adding internationalization (i18n) to your theme.
Since Timber version 1.x you can use most of the common translation functions right in your Twig files. Thereâs a guide about internationalization in the Timber Docs.