What is Internationalization?
We’re going to talk about internationalization in a WordPress widget. Internationalization is essentially the ability to allow your child theme or your plugin or your widget to be translated. It’s very easy to prepare the data to be translated so even if you don’t know how to do all the rest of the translations prepare your inputs for being translated now. When you decide you actually want to internationalized your data, it removes all the tedious work of doing that.
Later on I’m going to show you how to internationalize your child theme and how you can provide for translations for your child theme. However, in the meantime, I’m going to show you how to prepare your data to be internationalized without actually internationalizing it.
How Internationalization Works
The abbreviation for internationalization is I18n. You may have seen this abbreviation around and it just stands for preparing things to be translated. There is an automatic function in PHP and in WordPress that allows for this internationalization to happen fairly easily.
The way it works is, first you do is declare a text domain which we’re not going to do today, you prepare your data to be translated and when somebody is going to translate your child theme or your widget or plugin, they write a translation file that is linked to that text domain. As long as you prepare the data properly, those are entirely translated for you.
Functions To Prepare Your Data To Be Translated
Now, preparing your data to be translated is actually pretty easy and there are a whole bunch of functions. If you look at this I18n for WordPress developers, you’ll see there’s an awful lot of things going on in here. What we’re going to be taking a look at is pretty much this simple one which is the way of echoing out your information and there are two functions.
There is the _e function and there is the __ function. I’m just going to start off by showing you how to do that here in this plugin. The things you would want to internationalize or translate are your things like labels. The label is here instead of title, what you would do is wrap that in parenthesis, open up php and _e(Title) and we need a semicolon and our closing PHP tag. Title gets placed inside of single quotes, then a comma and your text domain.
Using a Unique Identifier as Text Domain
Now, the convention is to use your unique identifier for your child theme as your text domain. In this case, it’s crtc and if you look at Thesis code, you’ll see that Thesis uses thesis. You could use something else but by convention, the unique identifier for your functions and your variables is the same thing as your text domain so here, we’re going to use crtc as our text domain.
It’s essentially php echo, the string that you’re going to translate and the text domain that’s going to be used for the translation. It’s really that simple, you just use this function.
Now this is going to echo the title. If you didn’t want to echo it but if all you wanted to do was return it, then you would just use the double underscore rather than the _e. There are times when you do that but for our purposes we are echoing this information out.
If we come along here and just copy this, open php tag, the _e, the opening parenthesis and opening single quote then just come down here to each one of our labels and paste that in front of each one of our labels, then come back over here and grab the closing single quote, the comma through the closing php tag and copy that and come over to this.
Now we have displayed the widget title question mark and the closing part of the function. We’ll do the same thing right down here and all of our angry messages will go away. Inside of our input area, we have now internationalized “Enter some text”, we’ve internationalized the “Display the widget title” and “Title”.
This function will look at the language that has been established for the website, will look for language files with this text domain and will automatically translate title with whatever has been set up in that language text domain. From now on, everything we do is going to be configured this way to allow us to have these things translated.