Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming Multi-Language PHP applications

I'm developing a PHP application and I'm wondering about the best way to include multi-language support for users in other countries.

I'm proficient with PHP but have never developed anything with support for other languages.

I was thinking of putting the language into a PHP file with constants, example:

en.php could contain:

define('HZ_DB_CONN_ERR', 'There was an error connecting to the database.');

and fr.php could contain:

define('HZ_DB_CONN_ERR', 'whatever the french is for the above...');

I could then call a function and automatically have the correct language passed in.

hz_die('HZ_DB_CONN_ERR', $this);

Is this a good way of going about it?

-- morristhebear.

like image 289
morristhebear Avatar asked Jan 16 '09 15:01

morristhebear


People also ask

How do I add PHP multilingual support to my website?

Ways to add multi-language support to a website There are various ways to enable Multi-language support for an application. By having duplicate pages with the same content in the required languages. By managing language configuration (localization) with files containing texts in different languages.

Can you build an app with multiple languages?

With Appy Pie's multilingual app builder, creating Android or iOS apps in different languages is now as easy as pie. Our unique drag-and-drop DIY multilingual app maker lets you select your target countries and make your app live in multiple languages in a matter of minutes.

What is localization PHP?

Localization in software engineering is the process of adapting the content of a system to a particular locale so the target audience in that locale can understand it. It is not just about changing content from one language to another.


1 Answers

Weird. People seem to be ignoring the obvious solution. Your idea of locale-specific files is fine. Have en.php:

define('LOGIN_INCORRECT','Your login details are incorrect.');
...

Then, assuming you have a global config/constants file (which I'd suggest for many reasons), have code like this:

if ($language == 'en') {
  reqire_once 'en.php';
} else if ($language == 'de') {
  require_once 'de.php';
}

You can define functions for number and currency display, comparison/sorting (ie German, French and English all have different collation methods), etc.

People often forget PHP is a dynamic language so things like this:

if ($language == 'en') {
  function cmp($a, $b) { ... }
} else if ($language == 'de') {
  function cmp($a, $b) { ... }
}

are in fact perfectly legal. Use them.

like image 110
cletus Avatar answered Sep 25 '22 03:09

cletus