4

I'm working on a WordPress plugin and want to make sure that all text that is displayed is prepared for localization. I'm having trouble localizing the text that resides inside an array.

For instance, I have the following code inside a class

var $control_type_default = array(
    array(
        'key' => 'automatic',
        'value' => 'Automatic'
    )
);

var $control_types = array(
    array(
        'key' => 'manual',
        'value' => 'Manual'
    )
);

function process_class_vars()
{
    // Add default control type value to beginning of control types array
    $this->control_types = array_merge($this->control_type_default, $this->control_types);

    // Prepare values for localization
    $temp_array = array();
    foreach ($this->control_types as $control_type_array => $values) 
    {
        $temp_array[] = array(
            'key' => $values['key'],
            'value' => __($values['value'], 'my_plugin')
        );
    }
    $this->control_type = $temp_array;
}

Note that the "process_class_vars" function is called by the constructor. As you can see, I am attempting to pass the value in the array through the __() function (As a side note, I'm doing this with the function "process_class_vars" function instead of when I initially create the arrays because passing that function when I created the arrays initially threw an error).

The problem is that when I run this through a .po generator, the "Automatic" and "Manual" string are completely missed. How can these values be processed correctly?

1 Answer 1

2

You can't do that, at least not from a GUI translation helper app, because these are set to pick up strings from standard functions like __(), _e etc, they can't read variables (including string variables)

Instead try:

var $control_types = array(
    array(
        'key' => 'manual',
        'value' => __('Manual', 'yourtextdomain')
    )
);

But if $control_types is a static variable from a class then you won't be able to assign the return value of a function as it's value. Why not make it a normal variable inside the process_class_vars() method? Or just make it a function:

public static function control_types(){
 return
    array(
        'manual' => __('Manual', 'yourtextdomain'),
        ...
    )
);

...
foreach ($this->control_types() as $control_type => $label) 
{
    // I'm pretty sure you don't need 'key' / and 'value' as key name here either...
    $temp_array[] = array(
        'key' => $control_type,
        'value' => $label,
    );
}
5
  • So, I guess you are saying that I cannot automate the generation of .po file for strings inside an array. I would have to manually do that? The programming pattern is by design. I would like to have the array as a static class variable; however when I do as you suggest in your first code sample, I always get an error. Next, I don't want to set the array up as you suggest because I like to have the default values as key '0'. It makes it easier to find later in the code. I do agree that labeling these as 'key' and 'value' is a bit weird. I couldn't think of anything better at the moment.
    – tollmanz
    Commented Jul 2, 2011 at 20:05
  • not for strings - for variables, of any kind, inside anything not just arrays. But you could try to add those strings in the .po file and compile it manually. It should work, but it's kind of ugly Commented Jul 2, 2011 at 20:09
  • Is there a better way to do this then? The reason I collected the 'automatic' and 'manual' values is because I use that to generate radio buttons on a settings page and later to validate input on submission of the page. To me, it's cleaner to collect that information in one place rather than repeat it over and over. Would you tackle this in a different manner?
    – tollmanz
    Commented Jul 2, 2011 at 20:14
  • You can return that array from static function. It's really the same thing. Or serialize that array and define it as a constant. There are probably many other ways to do this... Commented Jul 2, 2011 at 20:18
  • Thanks! I think this finally clicked. I got it to work by simply defining the class variable, then using a function to assign the array to the variable. As such, I'm not using __() inside a foreach statement, which allows me to wrap each label in the function and make it available to the .po generator. Thanks!
    – tollmanz
    Commented Jul 2, 2011 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.