Jump to content

Recommended Posts

Two simple ways are to store language files that define constants for common words or phrases that you use in your application interface:

 

//lang-eng.php
define('SUBMIT', 'Submit');
//lang-esp.php
define('SUBMIT', 'Presentar');

 

You could also do this in the DB which is nice because other content such as large blocks of text, news articles, etc. would probably be better in the DB:

 

xlate_table
id   lang   key        text
1    eng    SUBMIT     Submit
2    esp    SUBMIT     Presentar

news_table
id   lang   title               text
8    eng    Some news           Just some news text here
9    esp    Algunas noticias    Sólo un poco de noticias de texto aquí

 

These are just simple examples.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159401
Share on other sites

I would do it like this:

 

index.tpl:

<b>{DICT.SomeText}</b><i>{DICT.SomeOtherText}</i>

 

I would put this in some method or function:

Note: I didn't test the query

$tpl = file_get_contents('index.tpl');
preg_match_all("/{DICT.(.+?)}/", $tpl, $matches);
$indexes = $matches[1];

switch(LANGUAGE){
case 'en':
	$lan = 'EN';
	break;
case 'sp':
	$lan = 'SP';
	break;
default:
	$lan = 'EN';
	break;
}
$searchStr = implode(',', $indexes);
$sql = mysql_query("SELECT t.translation, t.code FROM translations t LEFT JOIN languages l ON (t.lan_id = l.id) WHERE t.code IN($searchStr) AND l.code = '$lan'");
$dict = array();
while($row = mysql_fetch_assoc($sql)){
$dict[$row['code']] = $row[$lan];
}

foreach($dict as $code => $translation){
$tpl = preg_replace("/{DICT.$code}/", $translation, $tpl);
}
echo $tpl;

 

So basically you have 2 tables "languages" with fields:

- id (auto_increment)

- code (EN, SP, JP, CN, etc.)

 

And "translations" with fields:

- id (auto_increment)

- lan_id (relates to id in languages table)

- code (SomeText, SomeOtherText, etc.)

- translation (the actual text to be displayed)

 

That is a basic route to take (Similar to AbraCadaver second method).

Link to comment
https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159419
Share on other sites

The obvious problem with TLG's solution is: what if there's a missing translation? Currently the customer would see {DICT.somethingHere} or whitespace, worst case would be that your entire menu would be invisible due to missing translations or file permissions.

 

Personally I think the best approach is to simply write the website in a default language, pass it through some function that returns the input if it fails to load a translation.

 

<li><?php print $this->translate('Home'); ?></li>
<li><?php print $this->translate('About us'); ?></li>
<li><?php print $this->translate('Contact us'); ?></li>

Link to comment
https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159799
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.