Translating static content with Craft CMS

jake’s headshot By Jake, 17 Jun 2020

In this video, we walk through how you can use Craft’s built-in translation features to translate static content that is not translated in the CMS.

Video transcript

Hey everyone,

In this video, we're going to talk about static translations with Craft CMS. So, what is a static translation? Well, Craft has a couple of ways for you to translate content when you're working with it. You can use the CMS and actually use different translations of the content in your fields when you're putting any content in the CMS, but it also allows you to translate static content that isn't managed in the CMS. Maybe something like a heading or a form.

The first thing we need to do to get static translations set up in Craft is actually get the translations. So if you look in my directory structure you'll see that I've added a translations folder and then inside of that there's a folder for each language that we're gonna add a translation for. Right now we have de for German and es for Spanish, and Spanish is what we'll be looking at today.

I can open up the site.php file that contains all those translations for any words that we want to translate. We have name which will translate to nombre, email, message, and we're gonna translate the word hello.

So, we have these translations here but our text isn't being translated. That's because we need to use the translate filter that Craft gives us in our Twig templates. So I'm going to wrap name in a string and we'll run the translate filter on it and then we'll do the same thing for email and message. Just wrap those as a string and we'll say Craft render the string but translate it.

You'll see nothing change but if I jump over to the Spanish version of the site all of these field names have been translated. Craft looks at the static text which are passing it, checks if it matches any of the keys that you've given it in the translation PHP file, and if it does then it goes ahead and translates it for you.

So let's try doing the same thing on this "Hello, Jake!" text. We'll just wrap it as a string and then we will translate it. Now you see here that nothing happens. This is because there's not an exact match on this string. So it sees the word "hello" but because we don't have "Hello, Jake!" in here it doesn't do substring translation it has to match the string exactly.

So for something like this where we want to match part of a string we know the format of this will always be "hello comma space something exclamation point", we can use a substring with a variable. So, what we'll do is we'll do Hello, {name}! will be our key and then we'll translate that to Hola, {name}! and then what we need to do when you use our translation is pass it some params. So, here we'll pass an object of params and we'll say the name should be Jake and here we'll say hello name and then it will translate that string for us.

So... this is a basic translation going from English to Spanish using static translations in Craft CMS!

Further reading