1

is there a better way to include labels to be localized in Wordpress plugins and themes? My current setup involves calling the method below to get the labels:

   public function plug_get_labels(){

      $labels = array(
          'details' => __( 'Details', 'plug' ),
          'specs' => __( 'Specifications', 'plug' ),
      );

      return $labels;
    }

But for widgets for example I feel like it would be pointless to call the plug_get_labels method just to supply labels which are really part of the widget:

public function __construct() {

    parent::__construct(
        'plug_widget', __('Plug Widget', 'plug'),
        array( 'description' => __( 'A widget for displaying something', 'plug' ) )
    );

}

So what I'm doing is just using the plug_get_labels method to supply labels for AJAX related stuff so when I only use it when I need a label to load dynamically.

But my problem is that someone senior in my team said that the code and labels should be separate from each other. That I have to load the label from somewhere else so as to separate it from the code.

So the solution that I had in mind was to put all the labels in a JSON file then inside the plug_get_labels method I read from that file, convert it to an array, then loop through it to supply the text domain for each label:

public function plug_get_labels(){
  $file = file_get_contents(WP_CONTENT_DIR . '/plug-labels.json');
  $labels = json_decode($file, true);

  $new_labels = array();
  foreach($labels as $key => $label){
    $new_labels[$key] = __($label, 'plug');
  }

  return $new_labels;
}

I can only imagine that the senior developer in this case would also want to read from the file for the labels on every widget and theme that the plugin integrates with just so the labels will be separated from the code.

Is this a good idea? It seems like other Wordpress plugins also directly embed their labels on their widgets and code. If I implement this would it affect the performance? Can you suggest any other options that I have so I can separate the code from the labels? Thanks in advance!

2

there is no need to separate code from translatable labels. your plug_get_labels function suffers from the following issues

  1. Translation tools can show you the context in which the label is being used which helps in making a good translation faster. With your function you lose context.

  2. the use of such function encourage you to call it every time your code is being run and not only when a translation is needed. this might result in negative performance as loading the translation file and parsing it is relatively slow, and it makes you use extra memory (translation files are loaded only when a translation is required). of course if the number of translated strings is small there will not be a big impact.

Your json solution is truly bad. You lose the ability to use tools like poedit to do the translation because their use require that the first parameter to __ will be a literal string and not a variable.

  • so what's your preferred solution then? Should I just stick to just embedding the labels to where its needed? but how about labels loaded via ajax how do I deal with those? – soul Oct 30 '13 at 4:48
  • I don't see why AJAX should be different in any way from non AJAX code. wordress uses AJAX in parts of its admin and all labels are translated "inline" except for translations needed in javascript which uses some different mechanism which is more similar to your translation function. If you have any specific problem with translations in AJAX you can ask about it here. it is much easier to give answers to specific problems then to speak in general terms like I have done here. – Mark Kaplun Oct 30 '13 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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