Skip to main content
Dublin Library

The Publishing Project

Internationalizing WordPress Themes and Plugins (part 2)

 

## Internationalizing a plugin After setting up the text domain in your plugin metadata we need to load the translated files to use them. For plugins we use the [load\_plugin\_textdomain](https://developer.wordpress.org/reference/functions/load_plugin_textdomain/) function; it takes 4 parameters: 1. The name of text domain 2. This parameter is deprecated so always use FALSE 3. The path to the directory where your plugin's translations are stored Then add an action for the `plugins_loaded` hook and call the function we just created. ```php { return (

{ __( 'Hello World', 'myguten' ) }

); }, save: () => { return (

{ __( 'Hello World', 'myguten' ) }

); }, } ); ``` This will not work with external scripts. For that we need to use [wp\_localize\_script](https://developer.wordpress.org/reference/functions/wp_localize_script/) The function will only work with scripts we've enqueued to the system using `wp-enqueue-script`. ```js wp_enqueue_script( 'rivendellweb-navigation', get_template_directory_uri() . '/js/navigation.js', array('jquery'), '20151215', true ); ``` `wp_localize_script` takes three attributes: - The name of the enqueued script - The name of the javascript object we want to work with - An array of internationalized objects ```js wp_localize_script( 'rivendellweb-navigation', 'rivendellwebScreenReaderText', array( 'expand' => __( 'Expand child menu', 'rivendellweb'), 'collapse' => __( 'Collapse child menu', 'rivendellweb'), ) ); ``` ## Additional considerations: RTL languages If you're planning on distributing your theme and plugin there's one final consideration that I want to mention on this post; right to left language and how much our designs need to change to accommodate those languages. Compare the next two images. The first one is in English, left to right, top to bottom language. The second one is in Arabic, a right to left, top to bottom language. ![English version of the United Nations Websie](https://res.cloudinary.com/dfh6ihzvj/image/upload/v1587718299/publishing-project.rivendellweb.net/un-site-english.png)   ![Arabic version of the United Nations Website](https://res.cloudinary.com/dfh6ihzvj/image/upload/v1587718292/publishing-project.rivendellweb.net/un-site-arabic.png) English and Arabic versions of the United Nations Website [www.un.org](https://www.un.org/) The content flows differently and we should take these languages into account when deciding on margin and padding for our content. Because we don't know where our themes and plugins will be used we need to keep these differences in mind and provide some level of support for RTL languages. You can convert your stylesheets to work with RTL languages manually or using tools like [gulp-rtlcss](https://github.com/jjlharrison/gulp-rtlcss#readme)

Edit on Github