Best practices for Timber and WordPress with multilanguage support

Problem


As I’ve seen in the Timber documentation it is possible to add multilanguage support like this:

Read More
// 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?

Related posts

1 comment

  1. 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.

Comments are closed.