A better way to include localized labels in WordPress plugins

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:

Read More
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!

Related posts

1 comment

  1. 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.

Comments are closed.