0
votes

I am trying to add a translation to a plugin I wrote from scratch but after experimenting a while I'm not sure how to make Wordpress aware that there's a translation available for my plugin.

I'm not sure what else to try so I thought that someone more experienced could point out things I might need to change.

What I did so far:

  1. Added either _e() or __() to the sentences I want to be translated in my plugin files.
  2. Used Loco Translate plugin to generate a .pot file.
  3. Opened .pot file in Poedit, (it displayed a list of all the strings I wanted to translate) translated plugin and generated .po and .mo files from it.
  4. Moved .pot, .po and .mo to my-plugin/languages/.
  5. Renamed files to my-plugin-pt.po and my-plugin-pt.mo.
  6. Changed Wordpress site language to translated language. Language changed everywhere else but the plugin still renders in english.

Not really sure what to do next.

I've created a method that runs load_plugin_textdomain() while following these instructions from Wordpress and have added it as an action to my-plugin __construct():

my-plugin.php

public function __construct() {
  // Other filters and actions...
  add_action( 'plugins_loaded', array( $this, 'translation_init' ) );
} // __construct

function translation_init() {
    load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
1

1 Answers

0
votes

The language code at the end of the .po and .mo files was incorrect. Changed from -pt.po to -pt_PT.po (did the same for .mo file) and it started working perfectly.

There's a list of the available language codes for Wordpress which I should have had a look before naming the files in the first place.