How can I setup my PHP website as a multilingual site?

1.9k views Asked by At

I'm trying to see what the best way to translate my PHP website into other languages is. My website is currently in English, and I know I can create different language files (e.g. en.php, pt.php, es.php, etc.) with the different language variables setup.

However, I've read using *.po or *.pot files can make translating a website easier.

Is there a tool that extracts all the English text in my website and then puts it in a *.po file?

1

There are 1 answers

0
S.Visser On BEST ANSWER

The GetText function in PHP is a great way to work with multiple languages.

First the difference between .po, .mo and .pot:

.POT Portable Object Template. This is the file that you get when you extract texts from the application. Normally, you send this file to your translators.

.PO Portable Object. This is the file that you receive back from the translators. It’s a text file that includes the original texts and the translations.

.MO Machine Object. The MO file includes the exact same contents as PO file. The two files differ in their format. While a PO file is a text file and is easy for humans to read, MO files are compiled and are easy for computers to read. Your web server will use the MO file to display the translations.


Usage in PHP

<?php
// Set language to German
putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');

// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");

// Choose domain
textdomain("myPHPApp");

// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now

// Print a test message
echo gettext("Welcome to My PHP Application");

// Or use the alias _() for gettext()
echo _("Have a nice day");
?>

Tools

The tool I use is Poedit. The tools allows you to merdge between new texts in a POT and produces PO or MO files then.