how to make multilingual plugins and themes

41
How to Make Multilingual Plugins and Themes John P Bloch ● 2015-09-12 ● WordCamp DFW

Upload: johnpbloch

Post on 16-Apr-2017

457 views

Category:

Internet


3 download

TRANSCRIPT

Page 1: How to make multilingual plugins and themes

How to Make Multilingual Plugins and Themes

John P Bloch ● 2015-09-12 ● WordCamp DFW

Page 2: How to make multilingual plugins and themes

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)

Page 3: How to make multilingual plugins and themes

Definitions

Page 4: How to make multilingual plugins and themes

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

Page 5: How to make multilingual plugins and themes

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

Page 6: How to make multilingual plugins and themes

Why

Page 7: How to make multilingual plugins and themes
Page 8: How to make multilingual plugins and themes
Page 9: How to make multilingual plugins and themes

Why

• Empathy• Grow your potential user-base• Potentially make more money• Make more of an impact in the world

Page 10: How to make multilingual plugins and themes

How

Page 11: How to make multilingual plugins and themes

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

Page 12: How to make multilingual plugins and themes

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

Page 13: How to make multilingual plugins and themes

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

Page 14: How to make multilingual plugins and themes

Plugin and Theme Headers

Plugins and themes have two special headers:

Text DomainDomain Path

Page 15: How to make multilingual plugins and themes

Basic Translation Functions

__()

Page 16: How to make multilingual plugins and themes

Basic Translation Functions

__()_e()

Page 17: How to make multilingual plugins and themes

Basic Translation Functions

__()_e()_n()

Page 18: How to make multilingual plugins and themes

Basic Translation Functions

__()_e()_n()

_x()_ex()_nx()

Page 19: How to make multilingual plugins and themes

Basic Translation Functions

You can leave longer comments for the translators

Page 20: How to make multilingual plugins and themes

Advanced Translation Functions

number_format_i18n()

Page 21: How to make multilingual plugins and themes

Advanced Translation Functions

number_format_i18n()date_i18n()

Page 22: How to make multilingual plugins and themes

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

Page 23: How to make multilingual plugins and themes

wp_localize_script()

Page 24: How to make multilingual plugins and themes

wp_localize_script()

Page 25: How to make multilingual plugins and themes

wp_localize_script()

Page 26: How to make multilingual plugins and themes

Final Steps

Page 27: How to make multilingual plugins and themes

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

Page 28: How to make multilingual plugins and themes

Gotchas

• Line breaks• Empty strings• Formatted strings vs. interpolation

Page 29: How to make multilingual plugins and themes

_e( "You live in $state", 'my-plugin' );

Page 30: How to make multilingual plugins and themes

_e( "You live in $state", 'my-plugin' );

printf( __( 'You live in %s', 'my-plugin' ), $state );

Page 31: How to make multilingual plugins and themes

printf( __( 'Hi %s, you live in %s', 'my-plugin' ), $user_name, $state);

Page 32: How to make multilingual plugins and themes

printf( __( 'Hi %s, you live in %s', 'my-plugin' ), $user_name, $state);

Page 33: How to make multilingual plugins and themes

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);

Page 34: How to make multilingual plugins and themes

Best Practices

Page 35: How to make multilingual plugins and themes

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

Page 36: How to make multilingual plugins and themes

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

Page 37: How to make multilingual plugins and themes

Best Practices

Best:echo '<a href="/there">' . __( 'Click here for stuff.', 'my-plugin' ) . '</a>';

Page 38: How to make multilingual plugins and themes

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

Page 39: How to make multilingual plugins and themes

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

Page 40: How to make multilingual plugins and themes

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