Ask Your Question

How can the multi language WordPress translation using __() be implemented in an external PHP file of a plugin?

asked 2022-05-06 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-12-31 21:00:00 +0000

devzero gravatar image

To implement the multi language WordPress translation using __() in an external PHP file of a plugin, follow these steps:

  1. Create a new .pot file for your plugin using a tool like WP-CLI or Poedit.
  2. Add your plugin's strings to the .pot file and save it.
  3. Generate the .po and .mo files for each language using a tool like Poedit or WP-CLI.
  4. Load the .mo files for the user's current language in your plugin using the loadplugintextdomain() function.
  5. Use the __() function to translate your plugin's strings in the PHP file.

Here's an example code snippet:

// Load the text domain for the current language
load_plugin_textdomain( 'your-plugin-textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

// Use the __() function to translate your strings
echo __( 'Hello world!', 'your-plugin-textdomain' );

This will translate the string 'Hello world!' into the user's current language, using the translation files located in the 'languages' folder of your plugin.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-05-06 11:00:00 +0000

Seen: 11 times

Last updated: Dec 31 '21