how to make multilingual plugins and themes
TRANSCRIPT
How to Make Multilingual Plugins and Themes
John P Bloch ● 2015-09-12 ● WordCamp DFW
About MeJohn P Bloch
Lead Web Engineer at 10up
Yes 10up is hiring
Tell them I sent you
Seriously, we’re always hiring
Not a designer
Do these slides look designed?
DFW Local (Irving)
Definitions
Definitions
Internationalization
The process of making your code translatable at all.
Abbreviated as i18n
Localization
The process of using translated text in internationalized code.
Abbreviated as l10n
Definitions
Internationalization
The process of making your code translatable at all.
Abbreviated as i18n
Localization
The process of using translated text in internationalized code.
Abbreviated as l10n
Why
Why
• Empathy• Grow your potential user-base• Potentially make more money• Make more of an impact in the world
How
How translation works in WordPress
English text goes into a translation functionWP looks for translations of the text in the current languageIf a translation is found, the function uses thatOtherwise it uses the English text
Text Domains
A text domain is a namespace for your code’s translations
If you use the wordpress.org repositories, you must use your plugin or theme’s slug as the text domain
Even if you’re not on the official repos it’s still a good idea
Text Domains – Loading
Pluginsload_plugin_textdomain()Run on plugins_loaded actionArguments• The text domain• false // deprecated• Location of translations
• Relative to plugins directory
Themesload_theme_textdomain()Run on after_setup_themeArguments• The text domain• Location of translations
• Absolute path
Plugin and Theme Headers
Plugins and themes have two special headers:
Text DomainDomain Path
Basic Translation Functions
__()
Basic Translation Functions
__()_e()
Basic Translation Functions
__()_e()_n()
Basic Translation Functions
__()_e()_n()
_x()_ex()_nx()
Basic Translation Functions
You can leave longer comments for the translators
Advanced Translation Functions
number_format_i18n()
Advanced Translation Functions
number_format_i18n()date_i18n()
Advanced Translation Functions
number_format_i18n()date_i18n()wp_localize_script()
For adding translated strings to javascriptTranslates text in PHP and outputs as a global javascript variable
wp_localize_script()
wp_localize_script()
wp_localize_script()
Final Steps
Final Steps
• Generate .pot file• Official repositories provide web tools• Generate locally with PoEdit or makepot.php
• Publish .pot file• Usually packaged with plugin or theme in the languages directory
• Add translation files to your plugin or theme
Gotchas
• Line breaks• Empty strings• Formatted strings vs. interpolation
_e( "You live in $state", 'my-plugin' );
_e( "You live in $state", 'my-plugin' );
printf( __( 'You live in %s', 'my-plugin' ), $state );
printf( __( 'Hi %s, you live in %s', 'my-plugin' ), $user_name, $state);
printf( __( 'Hi %s, you live in %s', 'my-plugin' ), $user_name, $state);
printf( __( 'Hi %s, you live in %s', 'my-plugin' ), $user_name, $state);
printf( __( 'Hi %1$s, you live in %2$s', 'my-plugin' ), $user_name, $state);
Best Practices
Best Practices
• Use decent English style• Avoid slang and abbreviations
• Keep text together• Break at paragraphs• Use formatted strings instead of
concatenation• Translate phrases not words
Best Practices
Bad:echo __( 'Click ', 'my-plugin' ) . '<a href="/there">' . __( 'here', 'my-plugin' ) . '</a>' . __( ' for stuff.', 'my-plugin' );
Better:_e( 'Click <a href="/there">here</a> for stuff.', 'my-plugin' );
Best Practices
Best:echo '<a href="/there">' . __( 'Click here for stuff.', 'my-plugin' ) . '</a>';
Best Practices
Bad:echo __( 'Thanks for using ', 'my-plugin' ) . 'My Plugin!';
Better:echo sprintf( __( 'Thanks for using %s', 'my-plugin' ), 'My Plugin' ) . '!';
Best:printf( __( 'Thanks for using %s!', 'my-plugin' ), 'My Plugin' );
Best Practices
• Use decent English style• Avoid slang and abbreviations
• Keep text together• Break at paragraphs• Use formatted strings instead of
concatenation• Translate phrases not words
• No unnecessary HTML
• Make no assumptions about the translated text
• Length• Word order• Direction• Punctuation
• Avoid translating URLs that don’t have alternate languages available
Tools
• Pig Latin (http://w.org/plugins/piglatin/)• WP i18n Tools (develop.svn.wordpress.org/trunk/tools/i18n/)• PoEdit (https://poedit.net/)• GlotPress (http://blog.glotpress.org/)• Language Packs
Questions?Twitter: @johnpbloch
Website: https://johnpbloch.comEmail: [email protected]