PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 31, 2022

[FIXED] How to make custom Wordpress plugin aware of .po and .mo files?

 August 31, 2022     plugins, poedit, translation, wordpress     No comments   

Issue

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/' );
}

Solution

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.



Answered By - csalmeida
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing