3

I'm trying to implode an array of values that are wrapped in a i18n function as shown here:

<?php echo implode( ', ', __($joblanguages, 'my-text-domain') ); ?>

I'm getting the following error message:

Warning: Illegal offset type in isset or empty in /Applications/XAMPP/xamppfiles/htdocs/vemas-2012/wp-includes/pomo/translations.php on line 72

Is there any way to fix this and get the values in the chosen language?

Thanks in advance!

aurrutia
  • 77
  • 1
  • 7

2 Answers2

3

I think the function you need is array_map().

Your problem is that WP's __() function wants a single string to translate, whereas you want to translate a whole bunch of strings all at once. array_map() will do this for you. Something like this should do the trick:

implode(array_map(function($e) {return __($e,'my-text-domain');},$joblanguages);

Hope that helps.

SDC
  • 14,192
  • 2
  • 35
  • 48
  • Note: the line of code above assumes you're using PHP 5.3 or higher, since I've used a closure function so that the `'my-text-domain'` string can be static. PHP 5.2 doesn't support this syntax, but if you're stuck on 5.2 then you have bigger problems to worry about, as it's been unsupported for two years) – SDC Jan 17 '13 at 12:42
  • Indeed I'm running it on PHP Version 5.3.1 and despite a missed Parenthesis on the code it works perfectly! Thanks so much! Here you can find the code chunk that works for me (also added a separator for every item): `echo implode(', ', array_map(function($e) {return __($e,'my-text-domain');},$joblanguages));` Thanks so much everyone! – aurrutia Jan 18 '13 at 01:43
  • @aurrutia - glad that helped. On thing though: I suggest updating your PHP 5.3 to a more recent patch version -- the latest 5.3 version is 5.3.21; there have been a *lot* of security patches and bug fixes between .1 and .21, and there should be no compatibility problems because it's still 5.3. I strongly recommend updating. – SDC Jan 21 '13 at 10:16
  • Thanks again, and yes, I will update the php version. Regards. – aurrutia Jan 23 '13 at 09:21
1

Your $joblanguages is an array - didn't get that at first. So none of the things I previously mentioned will work.

$translated=array();

foreach($joblanguages as $jl){
    $translated[]=__($jl, 'my-text-domain');
}

echo implode( ', ', $translated);

Try this instead.

povilasp
  • 2,386
  • 1
  • 22
  • 36
  • Thanks for the code, but still get a warning message: `Warning: explode() expects parameter 2 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/vemas-2012/wp-content/themes/vemas-2012/content-careers-single.php on line 80 Warning: implode() [function.implode]: Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/vemas-2012/wp-content/themes/vemas-2012/content-careers-single.php on line 80` – aurrutia Jan 17 '13 at 09:47
  • 1
    can you paste the `var_dump(__($joblanguages, 'my-text-domain'));` I'm not sure what you get there – povilasp Jan 17 '13 at 09:48
  • `array(2) { [0]=> string(7) "English" [1]=> string(7) "Chinese" } ` – aurrutia Jan 17 '13 at 09:54
  • although my problem was solved with SDC's solution, I will try your solution anyway. Thanks! – aurrutia Jan 23 '13 at 09:23