I try to insert a third language in an existing page wich is already in french and english. I'm kind of beginner with php.
Here's the code:
<?php
$lang = (isset($_GET['lang']) && $_GET['lang'] == 'en') ? 'en' : 'fr' ;
$langParam = ($lang == 'en') ? '&lang=en' : '';
$typeTerms = array(
'fr' => array(
'WINES' => 'vins',
'BEERS' => 'Bières',
'MALTCOOLER' => 'boisson à base de malt',
'CIDER' => 'cidres',
'ALCOHOLFREE' => 'produits sans alcool<br/>et à faible teneur en alcool',
'OTHERS' => 'autres'
),
'en' => array(
'WINES' => 'Wines',
'BEERS' => 'Beers',
'MALTCOOLER' => 'Malt Based Cooler',
'CIDER' => 'Cider',
'ALCOHOLFREE' => 'Alcohol-free and low-alcoholic products',
'OTHERS' => 'Others'
)
);
?>
And I want to insert this array, and make it work!
'zh' => array(
'WINES' => '葡萄酒',
'BEERS' => '啤酒',
'MALTCOOLER' => '麦芽果酒',
'CIDER' => '苹果酒',
'ALCOHOLFREE' => '无酒精及低度酒精饮料',
'OTHERS' => '其它'
)
Thanks!
Just
$lang = isset($_GET['lang'])?$_GET['lang']:"en";
if(!in_array($lang,array("en","fr","zh"))) $lang = "en";
To add a new one:
$typeTerms['zh'] = array(
'WINES' => '葡萄酒',
'BEERS' => '啤酒',
'MALTCOOLER' => '麦芽果酒',
'CIDER' => '苹果酒',
'ALCOHOLFREE' => '无酒精及低度酒精饮料',
'OTHERS' => '其它'
);
Using the switch statement to check if the language is either en
or zh
. If it's fr
or something else, fallbacks to the main language fr
.
<?php
switch($_GET['lang']) {
case 'en' : $lang = 'en'; break;
case 'zh' : $lang = 'zh'; break;
case 'fr' :
default: $lang = 'fr'; break;
}
$langParam = in_array( $lang, array('en', 'zh') ) ? '&lang=' . $lang : '';
$typeTerms = array(
'fr' => array(
'WINES' => 'vins',
'BEERS' => 'Bières',
'MALTCOOLER' => 'boisson à base de malt',
'CIDER' => 'cidres',
'ALCOHOLFREE' => 'produits sans alcool<br/>et à faible teneur en alcool',
'OTHERS' => 'autres'
),
'en' => array(
'WINES' => 'Wines',
'BEERS' => 'Beers',
'MALTCOOLER' => 'Malt Based Cooler',
'CIDER' => 'Cider',
'ALCOHOLFREE' => 'Alcohol-free and low-alcoholic products',
'OTHERS' => 'Others'
),
'zh' => array(
'WINES' => '葡萄酒',
'BEERS' => '啤酒',
'MALTCOOLER' => '麦芽果酒',
'CIDER' => '苹果酒',
'ALCOHOLFREE' => '无酒精及低度酒精饮料',
'OTHERS' => '其它'
)
);
?>
simply check if $_GET['lang']
is set:
if(isset($_GET['lang']) && in_array($_GET['lang'], array('en, 'fr', 'zh')))
$lang = $_GET['lang'];
else
$lang = 'en'; // your Default lang
Then define words array:
$words = array(
'en' => array(...),
'fr' => array(...),
'zh' => array(...)
);
and to use array:
echo $words[$lang]['beers'];
Check if it exists in your language array, and if not, fall back to fr
:
$typeTerms = array(
// terms go here
);
$lang = isset( $typeTerms[ $_GET[ 'lang' ] ] ) ? $_GET[ 'lang' ] : 'fr';
$langParam = ($lang != 'fr') ? "&lang=$lang" : '';
Note that you need to place this code after you define the $typeTerms
.
This way, you can add as many languages to your $typeTerms
and never have to worry about making other changes to the code.