Translating Static Text

Craft Pro’s localization features are great for translating your dynamic content. But a lot of the copy on your site probably doesn't come from custom fields. Sometimes it comes from a non-translatable field in the CP (such as the Site Name), and other times it’s hard-coded right into the templates. So how do you translate that stuff?

You can translate all of that static copy using translation files.

Translation files are PHP files that live in craft/translations/ (a folder you must create yourself). They simply return an array which maps the source language’s strings to the target language.

For example, if you have a section called “News” in your English site, and you want it to be called “Noticias” in your Spanish site, create a file in craft/translations/ called es.php (the filename should match the locale you wish to target). Save the file with this:

<?php

return array(
    "News" => "Noticias",
);

Add a new line to that array with anything else you wish to translate.

Once your translations are in place, you have to update your template to utilize them.

In this case you would perhaps open up your news/index.html template, and find the News heading:

<h1>News</h1>

Replace the static text with a Twig tag that passes the text through Craft’s translate filter:

<h1>{{ "News" | translate }}</h1>

The next time you view that template in Spanish, you will find that rather than “News”, the heading will read “Noticias”. Just like that!