Jump to content

language file


etrader

Recommended Posts

I want to create a multi-language website. For translating the terms, I have two options. For using define() for each sentence; e.g.

DEFINE('WORD1', 'word1 in German');

DEFINE('WORD2', 'word2 in German');

......

 

and include this php language file.

 

Another option which is more standard is to create a mo file and call it by php. I prefer the first method as everything is just based on php; but I doubt if it is the best way for a website with 1000+ sentences. Any idea?

 

Link to comment
Share on other sites

Why not define it in a multi-dimensional array, be a hell of a lot more organised. And regardlesss, your going to end up with lots of keys in the array for translating purposes.

 

$lang = array(
    'en' => array(
        'welcome_text' => 'Your welcome text in english'
    )
);

 

Of course you could have separate files for each language and so on.

Link to comment
Share on other sites

Thanks, it seems to be a perfect solution. I can call the translations by $lang['word']

 

BUT how I can write a function to return (or echo) based on this scheme. I mean

 

function language($word) {
echo $lang[$word];
}

 

then using language('word') for displaying the translation.

Link to comment
Share on other sites

Generally, in most multi-lingual based applications they'll use a function like __() to echo out the translations. Most of these use the dot separated syntax. Take my first array for example, to access the welcome_text property you'd call something like this:

 

echo __('en.welcome_text');

 

The __() function would then break it apart and check the array for the existence of said keys, and if so return the resulting text.

Something to point out is that you shouldn't echo from within a function like that, it's nicer if you return the string.

Link to comment
Share on other sites

I believe _() is a function in PHP, but not __().

 

In your __() function you'd need to explode on the dots. Then, you'd need to loop over each bit and see if it exists in the language array.

 

function __($line)
{
include('lang/en/en.php');

// Explode the $line variable into bits.
$bits = explode('.', $line);

// Start the phrase off as false, that way the first if statement will evaluate to true, assuming the bit exists in the language array.
$phrase = false;
foreach($bits as $bit)
{
	if($phrase === false && isset($lang[$bit])
	{
		// Now set $phrase to the current bit in the language array.
		$phrase = $lang[$bit];
	}
	elseif(isset($phrase[$bit]))
	{
		// And continue to update it accordingly.
		$phrase = $phrase[$bit];
	}
	else
	{
		// Could not find the correct phrase.
		return false;
	}
}

// Return the phrase
return $phrase;
}

 

So again let's use the $lang array I defined earlier.

If we were to echo __('en.welcome_text'); our function would break that apart into an array like:

Array (
    [0] => 'en',
    [1] => 'welcome_text'
)

So we run this through the loop, and the first check is to see if $lang['en'] exists, and it does, so we update $phrase to be $lang['en']. Then the next itteration occurs. And the elseif() is fired which also evaluates to true because $lang['en']['welcome_text'] exists in the $lang array. After the loop it returns this phrase and hey presto we have our phrase we wanted translated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.