![]() The textdomain function selects that domain as the default domain. The next two lines actually set the current locale to that value and the bindtextdomain call creates a text domain which will use our messages.mo file in the locale directory. You should replace this by real code to select the locale (perhaps by looking at the Accept-Language header), but it works for our tutorial. The second line allows you to override this by appending ?locale=… to the URL. In the first line, we set the default locale to “de_DE”. If ( isSet ( $_GET ) ) $locale = $_GET īindtextdomain ( “messages”, “./locale” ) Copy the following code into localization.php ( Note: this code is for the php gettext extension, php libraries such as php-gettext may require different initialization): Let’s create a new file called “localization.php” that will handle all the gettext initialization. The first thing you have to do is telling the gettext library which locale you want to use and where the language files are stored. Let’s see if our script can now display the localized string. This is the compiled version that will later be used by PHP. poEdit automatically created “messages.mo” in the same directory as “messages.po”. If you had more entries in your file, you could navigate between them by pressing ctrl-up and ctrl-down. The first one contains the original string, and the second one is still empty. In the lower left hand corner, you have 2 text boxes. In the upper half of the poEdit window, you have a list of strings (the original string on the left and your translation on the right). Now that’s cool! poEdit just extracted all strings you want to be localized. poEdit will now automatically scan all source files inside the path you specified earlier and extract all strings that are passed to gettext() or _() (or any other methods you may have added in the keywords tab). Save the file inside that directory as “messages.po”. You should now have a directory structure like locale/de_DE/LC_MESSAGES/. Inside that folder create another one called “LC_MESSAGES”. We’ll create one for “de_DE” (the first part is the language, the second part is the country). Create a subdirectory inside locale for every language you support. Now create a sub folder called “locale” in your script directory. You may want to use this if you’re using php-gettext. On the keywords tab you can add names of additional functions that call gettext. On the paths tab, set the base path to the directory containing your test.php file, and add “.” as a path. The last option (plural forms) is an advanced feature of gettext which I’m going to explain later, so for now, just leave it blank. I’m going to use German, GERMANY and iso-8859-1 for both character sets. Choose the language you want to translate the application to. Create a new catalog (File -> new catalog). In order to do that, you either need the gettext utilities ( windows version) or the graphical editor poEdit, which we will be using. ![]() ![]() Now that you have created a first version of your script, you may want to create localized versions for different languages. ![]() When you open that page in your browser, you will see “Hello World!”. Create a new PHP file (we’ll call it test.php), and insert the following code: If you’re lazy, you can use _() instead of gettext(). Just call gettext(“Text to be translated”) and you get a localized version of “Text to be translated” if available, or “Text to be translated” otherwise. Using gettext to get translated strings couldn’t be easier. I will use the native PHP extension, but once you have read the post you should be able to use the php-gettext-library, too (have a look at the included example). You can use the native gettext PHP extension or you can use a library implemented in PHP that does not need any extension, such as php-gettext. There are basically two ways to use gettext in your PHP applications. It can be used with a variety of programming languages, including PHP. What happens if a newly added string is not yet translated into a specific language? Using gettext with PHPĪ widely used framework for internationalization is gettext. First of all, when the application is updated and additional strings are added, there is no way to determine which new strings were added and if they are present in every language (unless you write a script for it). However there are several problems with this approach. A common approach is having an include file for every supported language which contains an array that maps string ids to localized text (for example “WelcomeText” => “Welcome to our homepage.” would be included using something like ). Developing multi language web sites using PHP is actually very easy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |