I'm working on a list of products that are written in multiple languages. I have an array for each product that displays it's languages like this:
Array ( [0] => DA [1] => DE [2] => EN [3] => ES [4] => FI [5] => FR [6] => IT [7] => JA [8] => KO [9] => NL [10] => NO [11] => PL [12] => PT [13] => RU [14] => SV [15] => ZH )
I need to replace these individual codes with their language names (so EN => English). I have the following code, and it works fine with regular strings, but I can't get it to work with this array. Any thoughts?
$trans = array(
"EN" => "English",
"ZH" => "Chinese",
"DA" => "Danish",
"NL" => "Dutch",
"FI" => "Finnish",
"FR" => "French",
"DE" => "German",
"IT" => "Italian",
"JA" => "Japanese",
"KO" => "Korean",
"NO" => "Norwegian",
"PL" => "Polish",
"PT" => "Portuguese",
"RU" => "Russian",
"ES" => "Spanish",
"SV" => "Swedish",
);
echo strtr($langcodes, $trans);
$langcodes holds the array values.
Proof that it works: http://codepad.org/PR5pPqcX
@David check out my answer. See below. If I'm correct, please credit me so I get points. Points motivate me to answer more questions.
$language_codes = array(0 => 'DA', 1 => 'DE', 2 => 'EN', 3 => 'ES', 4 => 'FI', 5 => 'FR', 6 => 'IT', 7 => 'JA', 8 => 'KO', 9 => 'NL', 10 => 'NO', 11 => 'PL', 12 => PT, 13 => 'RU', 14 => 'SV', 15 => 'ZH' );
$trans = array(
"EN" => "English",
"ZH" => "Chinese",
"DA" => "Danish",
"NL" => "Dutch",
"FI" => "Finnish",
"FR" => "French",
"DE" => "German",
"IT" => "Italian",
"JA" => "Japanese",
"KO" => "Korean",
"NO" => "Norwegian",
"PL" => "Polish",
"PT" => "Portuguese",
"RU" => "Russian",
"ES" => "Spanish",
"SV" => "Swedish",
);
foreach ($language_codes as $key => $code)
if (!empty($trans[$code]))
$language_codes[$key] = $trans[$code];
var_dump($language_codes);
Proof that it works: http://codepad.org/PR5pPqcX
I think you have to loop through $langcodes
and call strtr()
for each code. According to the PHP manual, the first parameter has to be a string, not an array of strings.
The PHP docs for strtr()
make no mention of array support for argument #1. Arrays are only supported for argument #2. This simply will not work. You'll have to roll your own loop. Here is how to do it:
<?php
$languages = array('DA', 'DE', 'EN', 'ES', 'FI', 'FR', 'IT', 'JA', 'KO', 'NL', 'NO', 'PL', 'PT', 'RU', 'SV', 'ZH');
$trans = array(
'EN' => 'English',
'ZH' => 'Chinese',
'DA' => 'Danish',
'NL' => 'Dutch',
'FI' => 'Finnish',
'FR' => 'French',
'DE' => 'German',
'IT' => 'Italian',
'JA' => 'Japanese',
'KO' => 'Korean',
'NO' => 'Norwegian',
'PL' => 'Polish',
'PT' => 'Portuguese',
'RU' => 'Russian',
'ES' => 'Spanish',
'SV' => 'Swedish',
);
foreach($languages as &$language) {
$language = strtr($language, $trans);
}
print_r($languages);
?>
How about using array_map function like this:
function mapLang($l) {
global $trans;
return $trans[$l];
}
$langcodes = array_map("mapLang", $langcodes);
print_r($langcodes);
Array
(
[0] => Danish
[1] => German
[2] => English
[3] => Spanish
[4] => Finnish
[5] => French
[6] => Italian
[7] => Japanese
[8] => Korean
[9] => Dutch
[10] => Norwegian
[11] => Polish
[12] => Portuguese
[13] => Russian
[14] => Swedish
[15] => Chinese
)