WordPress
- Internationalization
- Localization
- Multilingual
(April 16, 2016)
Do
It
Right
Me and Company
Dat Hoang
Community & Support at OnTheGoSystems
Image credit: Denise VanDeCruze - OnTheGoSystems.
Me and Company
OnTheGoSystems
wp-types.comwpml.org icanlocalize.com
Main Content
● What are they?
● Internationalization
● Localization
● Multilingual (with WPML)
What are They?
Internationalization (i18n) is the process of
developing your theme/plugin language, so it can
easily be translated into other languages.
Localization (l10n) describes the subsequent
process of translating an internationalized
theme/plugin.
Multilingual means there are more than one
language in sites.
Internationalization (1)
Wrong
echo 'WordPress is the best!';
Right (use gettext functions)
echo __( 'WordPress is the best!', 'my-text-domain' );
Wrong (don’t be clever!)
__( 'Translate me.' , $my_text_domain );
Right (no variable, constant… for text domains)
__( 'Translate me.' , 'my-text-domain' );
Internationalization (2)
Basic functions:
● __(), _e()
● _x(), _ex() - Disambiguation by context
_x( 'Post', 'noun', 'my-text-domain' );
_x( 'Post', 'verb', 'my-text-domain' );
● _n(), _nx() - Plural/singular nouns
sprintf(
_n( '%s star', '%s stars', $rating, 'text-domain' ),
$rating );
● _n_noop(), _nx_noop(), translate_nooped_plural()
Internationalization (3) - JavaScript
function add_my_script() {
// Register the script
wp_register_script( 'some_handle',
get_template_directory_uri() . '/custom.js' );
// Localize the script with new data
$translation_array = array (
'some_string' => __( 'Some string to translate',
'text-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name',
$translation_array );
// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
Internationalization (4) - JavaScript
custom.js
<script>
// alerts 'Some string to translate'
alert( object_name.some_string);
</script>
HTML:
<script type='text/javascript'>
/* <![CDATA[ */
var object_name = {"some_string":"Some string to
translate","a_value":"10"};
/* ]]> */
</script>
Internationalization (5)
Date and number functions:
- number_format_i18n()
- date_i18n()
Escape functions:
esc_html__()
esc_html_e()
esc_html_x()
esc_attr__()
esc_attr_e()
esc_attr_x()
Internationalization (6)
Load .mo files:
function my_plugin_load_plugin_textdomain() {
load_plugin_textdomain( 'text-domain', FALSE,
basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded',
'my_plugin_load_plugin_textdomain' );
Last but not least: original language = ENGLISH
Localization (1)
File Types
● POT (Portable Object Template)
#: plugin-name.php:123
msgid "Page Title"
msgstr ""
● PO (Portable Object)
● MO (Machine Object)
Localization (2)
Generates POT / PO / MO files
● Plugin Repository Admin Tools
● WordPress i18n tools
● Grunt
● WPML String Translation
● MO files:
○ msgfmt -o filename.mo filename.po
○ Online tools
○ grunt-po2mo
Localization (3)
Translate
● Poedit
● Plugins:
○ Loco Translate
○ WPML String Translation
● Google Translator Toolkit
● translate.wordpress.org
Localization (4)
Feel localization!
● Settings > General > Site language
● Put the correct folders
○ wp-content/languages/plugins/text-domain-vi_VN.mo
○ wp-content/plugins/my-plugin/languages/text-domain-vi_VN.mo
Localization (5)
Contribute: Everyone can help
● https://wpvi.wordpress.com/
● https://translate.wordpress.
org/locale/vi/default/wp/dev
● https://translate.wordpress.org/projects/wp-
plugins/woocommerce
Multilingual (with WPML)
● Gettext strings
● Options (saved in wp_options)
● Content (posts, categories, menus)
● WooCommerce
● Common issues
Multilingual (1)
Gettext strings
● Important: theme and plugins are ready for
internalization/localization.
● Use .mo files
● Use WPML > String Translation
Multilingual (2)
Options - translate in WPML String Translation
● Automatically: wpml-config.xml
● Manually: adding strings in wp-admin
Multilingual (3)
Content - posts
Multilingual (4)
Content - categories
Multilingual (5)
Content - menus
Multilingual (6)
WooCommerce - install WCML
Multilingual (7)
Common issues:
● Translate custom strings
- wpml_register_single_string
- wpml_translate_single_string
● wp_query($args) or get_posts($args) doesn’t
filter out correct posts IDs for the current
language
- add "suppress_filters=0" to arguments.
Multilingual (8)
Common issues:
● A custom post is different in a second
language
$post = get_post(
apply_filters( 'wpml_object_id', $post->ID, 'cpt' ) );
● A custom taxonomy ID is different in a second
language
$tax_id =
apply_filters( 'wpml_object_id', $tax_id, 'tax' );
Discussion
Image credit: http://sweetclipart.com/diverse-people-raising-hands-968
Useful Links
1. https://developer.wordpress.
org/plugins/internationalization/localization
2. https://developer.wordpress.
org/plugin/internationalization/how-to-internationalize-your-
plugin
3. https://docs.woothemes.com/document/woocommerce-
localization/
4. https://wpml.org/documentation/related-
projects/woocommerce-multilingual/
5. https://wpml.org/documentation/support/debugging-theme-
compatibility/
6. https://wpml.org/documentation/translating-your-contents/
Big Thanks!
Dat Hoang
Community & Support
E: dat.h@icanlocalize.com
W: wpml.org | wp-types.com | icanlocalize.com
Saigon, April 16, 2016

WordPress Internationalization, Localization and Multilingual - Do It Right

  • 1.
    WordPress - Internationalization - Localization -Multilingual (April 16, 2016) Do It Right
  • 2.
    Me and Company DatHoang Community & Support at OnTheGoSystems Image credit: Denise VanDeCruze - OnTheGoSystems.
  • 3.
  • 4.
    Main Content ● Whatare they? ● Internationalization ● Localization ● Multilingual (with WPML)
  • 5.
    What are They? Internationalization(i18n) is the process of developing your theme/plugin language, so it can easily be translated into other languages. Localization (l10n) describes the subsequent process of translating an internationalized theme/plugin. Multilingual means there are more than one language in sites.
  • 6.
    Internationalization (1) Wrong echo 'WordPressis the best!'; Right (use gettext functions) echo __( 'WordPress is the best!', 'my-text-domain' ); Wrong (don’t be clever!) __( 'Translate me.' , $my_text_domain ); Right (no variable, constant… for text domains) __( 'Translate me.' , 'my-text-domain' );
  • 7.
    Internationalization (2) Basic functions: ●__(), _e() ● _x(), _ex() - Disambiguation by context _x( 'Post', 'noun', 'my-text-domain' ); _x( 'Post', 'verb', 'my-text-domain' ); ● _n(), _nx() - Plural/singular nouns sprintf( _n( '%s star', '%s stars', $rating, 'text-domain' ), $rating ); ● _n_noop(), _nx_noop(), translate_nooped_plural()
  • 8.
    Internationalization (3) -JavaScript function add_my_script() { // Register the script wp_register_script( 'some_handle', get_template_directory_uri() . '/custom.js' ); // Localize the script with new data $translation_array = array ( 'some_string' => __( 'Some string to translate', 'text-domain' ), 'a_value' => '10' ); wp_localize_script( 'some_handle', 'object_name', $translation_array ); // Enqueued script with localized data. wp_enqueue_script( 'some_handle' ); } add_action( 'wp_enqueue_scripts', 'add_my_script' );
  • 9.
    Internationalization (4) -JavaScript custom.js <script> // alerts 'Some string to translate' alert( object_name.some_string); </script> HTML: <script type='text/javascript'> /* <![CDATA[ */ var object_name = {"some_string":"Some string to translate","a_value":"10"}; /* ]]> */ </script>
  • 10.
    Internationalization (5) Date andnumber functions: - number_format_i18n() - date_i18n() Escape functions: esc_html__() esc_html_e() esc_html_x() esc_attr__() esc_attr_e() esc_attr_x()
  • 11.
    Internationalization (6) Load .mofiles: function my_plugin_load_plugin_textdomain() { load_plugin_textdomain( 'text-domain', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); } add_action( 'plugins_loaded', 'my_plugin_load_plugin_textdomain' ); Last but not least: original language = ENGLISH
  • 12.
    Localization (1) File Types ●POT (Portable Object Template) #: plugin-name.php:123 msgid "Page Title" msgstr "" ● PO (Portable Object) ● MO (Machine Object)
  • 13.
    Localization (2) Generates POT/ PO / MO files ● Plugin Repository Admin Tools ● WordPress i18n tools ● Grunt ● WPML String Translation ● MO files: ○ msgfmt -o filename.mo filename.po ○ Online tools ○ grunt-po2mo
  • 14.
    Localization (3) Translate ● Poedit ●Plugins: ○ Loco Translate ○ WPML String Translation ● Google Translator Toolkit ● translate.wordpress.org
  • 15.
    Localization (4) Feel localization! ●Settings > General > Site language ● Put the correct folders ○ wp-content/languages/plugins/text-domain-vi_VN.mo ○ wp-content/plugins/my-plugin/languages/text-domain-vi_VN.mo
  • 16.
    Localization (5) Contribute: Everyonecan help ● https://wpvi.wordpress.com/ ● https://translate.wordpress. org/locale/vi/default/wp/dev ● https://translate.wordpress.org/projects/wp- plugins/woocommerce
  • 17.
    Multilingual (with WPML) ●Gettext strings ● Options (saved in wp_options) ● Content (posts, categories, menus) ● WooCommerce ● Common issues
  • 18.
    Multilingual (1) Gettext strings ●Important: theme and plugins are ready for internalization/localization. ● Use .mo files ● Use WPML > String Translation
  • 19.
    Multilingual (2) Options -translate in WPML String Translation ● Automatically: wpml-config.xml ● Manually: adding strings in wp-admin
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    Multilingual (7) Common issues: ●Translate custom strings - wpml_register_single_string - wpml_translate_single_string ● wp_query($args) or get_posts($args) doesn’t filter out correct posts IDs for the current language - add "suppress_filters=0" to arguments.
  • 25.
    Multilingual (8) Common issues: ●A custom post is different in a second language $post = get_post( apply_filters( 'wpml_object_id', $post->ID, 'cpt' ) ); ● A custom taxonomy ID is different in a second language $tax_id = apply_filters( 'wpml_object_id', $tax_id, 'tax' );
  • 26.
  • 27.
    Useful Links 1. https://developer.wordpress. org/plugins/internationalization/localization 2.https://developer.wordpress. org/plugin/internationalization/how-to-internationalize-your- plugin 3. https://docs.woothemes.com/document/woocommerce- localization/ 4. https://wpml.org/documentation/related- projects/woocommerce-multilingual/ 5. https://wpml.org/documentation/support/debugging-theme- compatibility/ 6. https://wpml.org/documentation/translating-your-contents/
  • 28.
    Big Thanks! Dat Hoang Community& Support E: dat.h@icanlocalize.com W: wpml.org | wp-types.com | icanlocalize.com Saigon, April 16, 2016