WordPress Plugin Localization

Ronald Huereca
Ronald HuerecaOwner at MediaRon LLC
WordPress Plugin
Localization
         By Ronald Huereca
              Twitter: @ronalfy

     Presented at WordCamp Raleigh
         Slides and code available at: http://ithem.es/localize




                                                                  May 22, 2011
                                                                             1
About Ronald Huereca
            Plugin developer at iThemes (the
            creators of Builder and BackupBuddy)
            Author of the book WordPress and
            Ajax
            Plugin author of Ajax Edit Comments
            Karaoke singer, gamer, and Vodka
            drinker (sometimes at the same time)




                                                   2
What is Localization (aka,
Internationalization)?
  Allows your strings to be translated
  into multiple languages
  Provides a framework to format your
  strings in a translatable way




                                         3
What are the benefits of
Localization?
  Your plugins or themes can be
  translated by others
  Your audience isn’t limited by one
  locale or language




                                       4
So what does localization
       look like?




                            5
Example: Un-localized
strings :(
These strings are very sad :(

  <div class='wrap'>
  ! <h2>My Plugin Settings</h2>
  ! <div class='updated'><p>Plugin Settings have
  been saved.</p></div>
  </div>




                                                   6
Example: Localized
strings :-)
These strings are very happy :-)

  <div class='wrap'>
  ! <h2><?php _e( 'My Plugin Settings',
  'DOMAIN' ); ?></h2>
  ! <div class='updated'><p><?php _e( 'Plugin
  Settings have been saved.', 'DOMAIN' ); ?></p></
  div>
  </div>




                                                     7
How to enable localization
for a plugin?
  Use the WordPress ‘init’ action
  Function: load_plugin_textdomain (for
  plugins)
  Function: load_theme_textdomain (for
  themes)




                                          8
Enabling Localization for a
Plugin
Simple, simple, simple...

  <?php
  //Plugin header goes here
  add_action( 'init', 'pb_msp_init' );
  function pb_msp_init() {
  ! //Admin menu
  ! add_action( 'admin_menu', 'pb_msp_menu' );
  ! //Assuming you have a languages folder in your plugin -
  Typically run within the 'init' action
  ! load_plugin_textdomain( 'unique_domain', false, dirname
  ( plugin_basename( __FILE__ ) ) . '/languages/' );
   
  } //end pb_msp_init
  //More functions
  ?>




                                                              9
Let’s do some basic
     localization




                      10
__( ‘string’, ‘domain’ )

Returns a localized string - domain should be unique

  <?php
  ! echo __( 'Translate me!', 'unique_domain' );
  ! //Do stuff with localized string here
   ?>




                                                       11
_e( ‘string’, ‘domain’ )

Echos a localized string

  <?php
  ! _e( 'Translate me!', 'unique_domain' );
  ! //Output is: Translate Me!
  ?>




                                              12
For most cases, _e and __
  are all you’ll ever care
           about



                             13
But what if you have
singular and plural forms of
          a string?



                           14
For example: 1 comment
     vs. 2 comments




                         15
Let’s use the _n() function
  Provide a singular string
  Provide a plural string
  Provide a count integer (e.g., 2 )




                                       16
_n( ‘singular’, ‘plural’,
$count, ‘domain’ )
Returns a singular or plural form of a string

  <?php
  ! $total_count = 1;
  ! if ( $total_count > 1 || $total_count == 0 )
  $count = 2;
  ! else $count = 1;
  ! printf( _n( 'You have voted %d time', 'You
  have voted %d times', $count, 'unique_domain' ),
  $total_count );
  ! //Output is: You have voted 1 time
  ?>



                                                     17
What about context?
  Context provides... context
  How many ways can you interpret blue
  (i.e., feeling blue or literally being blue)




                                                 18
The _x function helps
provide context
  It helps your translators figure out
  what the string is for
  It’ll also make you think, should I use a
  different phrase or word?




                                              19
_x( ‘string’, ‘context’,
‘domain’ )
Returns a string in a specific context

  <?php
  ! echo _x( 'I am blue today.', 'noun',
  'unique_domain' );
  ! //You are literally blue
   
  ! echo _x( 'I am blue today.', 'adjective',
  'unique_domain' );
  ! //You are feeling blue (i.e., depressed) today
  ?>




                                                     20
What about localizing
strings in JavaScript?




                         21
Why would you ever want to
    localize strings in
       JavaScript?



                         22
Why the hell not?




                    23
Localizing JavaScript
  Queue your script like normal
  (wp_enqueue_script)
  Provide localization (via
  wp_localize_script)
  Update your scripts to use it




                                  24
wp_localize_script( ‘handler’,
‘object_name’, $li0n )
Outputs a JavaScript object with localized variables

  <?php
  ! wp_enqueue_script( 'my_handler',
  get_stylesheet_directory_uri() . '/my_script.js',
  array( 'jquery' ), '1.0.0', false );
  ! wp_localize_script( 'my_handler',
  'my_script_object', array( 'text' => __
  ( 'Localization is absolutely awesome',
  'unique_domain' ) ) );
  ?>




                                                       25
wp_localize_script - Continued


Access the localized variable in JavaScript file my_script.js

  jQuery(document).ready(function( $ ) {
  ! alert( my_script_object.text );
  });




                                                                26
There are a ton of helper
       functions




                            27
Localization Functions
  __( ‘string’, ‘domain’ ) - Returns a localized string
  _e( ‘string’, ‘domain’ ) - Echos a localized string
  _n( ‘singular’, ‘plural’, $count, ‘domain’ ) - Returns a singular or plural
  form of a string
  _x( ‘string’, ‘context’, ‘domain’ ) - Returns a string in a context (context
  can be anything really, but it helps give the translators an idea of what
  you’re going for)
  esc_html_e or esc_html__ (works like __ and _e, but returns
  encoded text)
  esc_attr_e or esc_attr__ (works for encoding attribute values)
  wp_localize_script - Helper function for localizing JavaScript strings

                                                                                 28
Even more...
  _nx( ‘singular’, ‘plural’, $number, ‘context’, ‘domain’ ) - Combines the _n
  and _x functions
  _esc_attr_x( ‘string’, ‘context’, ‘domain’ ) - Like the esc_attr__
  function, but with context
  _esc_html_x( ‘string’, ‘context’, ‘domain’ ) - Like the esc_html__
  function, but with context
  _n_noop( ‘singular’, ‘plural’ ) - Provide _n strings for translation, but
  will not be translated in the output (yet)
  _nx_noop( ‘singular’, ‘plural’, ‘context’ ) - Provide _nx strings for
  translation, but will not be translated in the output (yet)
  translate_nooped_plural ( $noop_output, $number, ‘domain’ ) - Take
  the results from _n_noop and _nx_noop and translate
                                                                                29
And some helper functions
  sprintf( ‘string format’, $arg1, $arg2, etc... ) - Returns a formatted string
  printf( ‘string format’ $arg1, $arg2, etc...) - Prints a formatted string




                                                                                  30
How About a Video Demonstrating
Localization?




                                  31
How About a Video Demonstrating
Localization?




                                  31
H o w t o G e t Tr a n s l a t o r s ?
    Release a useful plugin that is localization ready
    Make sure translators can contact you
    The translators will come to you
    Package their “.mo” file with future releases
    Keep track of your translators and notify them before any
    major updates (some translators can act as beta testers)




                                                                32
H o w t o H e l p Tr a n s l a t o r s
    Try to minimize da slang, ya know?
    Limit jargon and acronomese FTW!
    Translate sentences and paragraphs using sprintf or printf
    to provide context
    Avoid loooooong portions of translatable text - Split at
    paragraphs or sentences
    Avoid translating single words, and if you do, provide
    context (_x function)
    Avoid beginning or ending your string with whitespace
    Try translating some of your own work - Even if it’s not
    going in the final product, it’ll help you see what the
    translators see
                                                                 33
How to Generate POT Files
  Use Poedit with the path and keywords (ewwwww, and way too
  advanced - Would require another presentation)
  With Poedit, just generate the “po” file and rename to “pot”. At least
  that part is simple.
  Use the WordPress Repo to do it (yay!!!)




                                                                          34
WordPress Repo FTW!!!



                     Make sure
                   you’re logged in



              Get your POT




                                      35
<?php _e( ‘Conclusion’ ); ?>

  Localization is super easy
  Localization can increase your plugin audience
  The WordPress Plugin Repo is ideal for generating
  “pot” files. Poedit has issues.




                                                      36
<?php echo __( “Questions?” ); ?>

     Slides and code available at: http://ithem.es/localize




                                                              37
More code examples...




                        38
sprintf( ‘string’, [arg1,arg2,
etc...] )
Returns a formatted string

  <?php
  //%s is for a string placeholder, %d is for an integer (there's a
  lot more of these)
  echo sprintf( 'My name is %s and I am %d years old', 'Ronald',
  29 );
  //Output is My name is Ronald and I am 29 years old
   
  //Reuse the same string over again
  echo sprintf( 'My name is %1$s and here I am again %1$s and I am
  %d years old', 'Ronald', 29 );
  //Output is My name is Ronald and here I am again Ronald and I am
  29 years old
  ?>




                                                                      39
printf( ‘string’, [arg1,arg2,
etc...] )
Prints a formatted string

  <?php
  //%s is for a string placeholder, %d is for an integer (there's a
  lot more of these)
  printf( 'My name is %s and I am %d years old', 'Ronald', 29 );
  //Output is My name is Ronald and I am 29 years old
   
  //Reuse the same string over again
  printf( 'My name is %1$s and here I am again %1$s and I am %d
  years old', 'Ronald', 29 );
  //Output is My name is Ronald and here I am again Ronald and I am
  29 years old
  ?>




                                                                      40
__ and sprintf

sprintf allows you to keep strings in context without concatenation

  <?php
  ! $total_count = 100; //retrieved from somewhere
  ! $my_localized_string = sprintf( __( 'You have
  voted %d times', 'unique_domain' ),
  $total_count );
  ! echo $my_localized_string;
  ! //Output is: You have voted 100 times
   ?>




                                                                      41
esc_html_e( ‘string’,
‘domain’ )
Translates, encodes, and echoes

  <div><?php esc_html_e( 'String to be outputted in
  HTML', 'unique_domain' ); ?></div>




                                                      42
esc_html__( ‘string’,
‘domain’ )
Translates, encodes, and returns

  <?php
  ! $dynamic_string = 'Tigerblood';
   ?>
  <div><?php printf( esc_html__( 'I drink %s',
  'unique_domain' ), $dynamic_string ); ?></div>




                                                   43
esc_attr_e( ‘string’,
‘domain’ )
Translates, encodes, and echoes

  <a href='http://pluginbuddy.com' title='<?php
  esc_attr_e( 'PluginBuddy - The Creators of
  BackupBuddy', 'unique_domain' ); ?
  >'>PluginBuddy</a>




                                                  44
esc_attr__( ‘string’,
‘domain’ )
Translates, encodes, and returns

  <a href='http://pluginbuddy.com' title='<?php printf
  ( esc_attr__( '%s - The Creators of %s',
  'unique_domain' ), 'PluginBuddy',
  'BackupBuddy' ); ?>'>PluginBuddy</a>




                                                         45
_e equivalent with printf
and __
Combine printf and __ for a _e equivalent with formatting

  <?php
  ! printf( __( 'You have voted %d times',
  'unique_domain' ), $total_count );
  ! //Output is: You have voted 100 times
  ?>




                                                            46
1 of 47

Recommended

PHP Powerpoint -- Teach PHP with this by
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
54K views354 slides
Fantastic DSL in Python by
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Pythonkwatch
13K views61 slides
Tips by
TipsTips
Tipsmclee
751 views53 slides
SPL to the Rescue - Tek 09 by
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09Elizabeth Smith
2.4K views55 slides
Rapid Development with Ruby/JRuby and Rails by
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
901 views45 slides
Creating Domain Specific Languages in Python by
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonSiddhi
35.3K views48 slides

More Related Content

What's hot

Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013 by
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013Puppet
8.9K views50 slides
Streams, sockets and filters oh my! by
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Elizabeth Smith
4.2K views60 slides
Php Development With Eclipde PDT by
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDTBastian Feder
2K views32 slides
PHP7. Game Changer. by
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer. Haim Michael
312 views74 slides
Introduction to Perl - Day 1 by
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
58.6K views141 slides
Spl to the Rescue - Zendcon 09 by
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
2K views69 slides

What's hot(20)

Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013 by Puppet
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
Puppet8.9K views
Streams, sockets and filters oh my! by Elizabeth Smith
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
Elizabeth Smith4.2K views
Php Development With Eclipde PDT by Bastian Feder
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDT
Bastian Feder2K views
PHP7. Game Changer. by Haim Michael
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael312 views
Introduction to Perl - Day 1 by Dave Cross
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross58.6K views
Good Evils In Perl by Kang-min Liu
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu13.8K views
Power of Puppet 4 by Martin Alfke
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
Martin Alfke9.3K views
Php on the desktop and php gtk2 by Elizabeth Smith
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
Elizabeth Smith4.2K views
The Beauty And The Beast Php N W09 by Bastian Feder
The Beauty And The Beast Php N W09The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09
Bastian Feder4.5K views
perl usage at database applications by Joe Jiang
perl usage at database applicationsperl usage at database applications
perl usage at database applications
Joe Jiang1.2K views
Yapc::NA::2009 - Command Line Perl by Bruce Gray
Yapc::NA::2009 - Command Line PerlYapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line Perl
Bruce Gray2.6K views
Create your own PHP extension, step by step - phpDay 2012 Verona by Patrick Allaert
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert41.8K views
Natural Language Processing and Python by anntp
Natural Language Processing and PythonNatural Language Processing and Python
Natural Language Processing and Python
anntp8.3K views

Viewers also liked

WordPress Must-Use Plugins (Quick Overview) by
WordPress Must-Use Plugins (Quick Overview)WordPress Must-Use Plugins (Quick Overview)
WordPress Must-Use Plugins (Quick Overview)Ronald Huereca
1.1K views16 slides
Designing Plugins for Release by
Designing Plugins for ReleaseDesigning Plugins for Release
Designing Plugins for ReleaseRonald Huereca
1K views32 slides
Jobvite Social Recruiting Survey Results - 2011 by
Jobvite Social Recruiting Survey Results - 2011Jobvite Social Recruiting Survey Results - 2011
Jobvite Social Recruiting Survey Results - 2011Steven Duque
879 views21 slides
Selection policy evaluation by
Selection policy evaluationSelection policy evaluation
Selection policy evaluationLaurie Roberts
470 views4 slides
Budget simulation assignment by
Budget simulation assignmentBudget simulation assignment
Budget simulation assignmentLaurie Roberts
407 views7 slides
How to Successfully Take Over a WordPress Plugin by
How to Successfully Take Over a WordPress PluginHow to Successfully Take Over a WordPress Plugin
How to Successfully Take Over a WordPress PluginRonald Huereca
423 views46 slides

Viewers also liked(15)

WordPress Must-Use Plugins (Quick Overview) by Ronald Huereca
WordPress Must-Use Plugins (Quick Overview)WordPress Must-Use Plugins (Quick Overview)
WordPress Must-Use Plugins (Quick Overview)
Ronald Huereca1.1K views
Jobvite Social Recruiting Survey Results - 2011 by Steven Duque
Jobvite Social Recruiting Survey Results - 2011Jobvite Social Recruiting Survey Results - 2011
Jobvite Social Recruiting Survey Results - 2011
Steven Duque879 views
Budget simulation assignment by Laurie Roberts
Budget simulation assignmentBudget simulation assignment
Budget simulation assignment
Laurie Roberts407 views
How to Successfully Take Over a WordPress Plugin by Ronald Huereca
How to Successfully Take Over a WordPress PluginHow to Successfully Take Over a WordPress Plugin
How to Successfully Take Over a WordPress Plugin
Ronald Huereca423 views
What Is A Wiki by kwilfert
What Is A WikiWhat Is A Wiki
What Is A Wiki
kwilfert886 views
Las wikis by taloxa
Las wikisLas wikis
Las wikis
taloxa406 views
WordPress Multisite General Overview by Ronald Huereca
WordPress Multisite General OverviewWordPress Multisite General Overview
WordPress Multisite General Overview
Ronald Huereca1.3K views
The Wikification of blackboard by Stevemac121
The Wikification of blackboard The Wikification of blackboard
The Wikification of blackboard
Stevemac121306 views
How to create an educational wiki with sound by Laurie Roberts
How to create an educational wiki with soundHow to create an educational wiki with sound
How to create an educational wiki with sound
Laurie Roberts437 views

Similar to WordPress Plugin Localization

Design patterns illustrated 010PHP by
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHPHerman Peeren
3.7K views61 slides
Living With Legacy Code by
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
25.3K views80 slides
&lt;img src="../i/r_14.png" /> by
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
1.7K views163 slides
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b> by
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>tutorialsruby
3.7K views163 slides
Php mysql training-in-mumbai by
Php mysql training-in-mumbaiPhp mysql training-in-mumbai
Php mysql training-in-mumbaiUnmesh Baile
248 views41 slides
Php training100%placement-in-mumbai by
Php training100%placement-in-mumbaiPhp training100%placement-in-mumbai
Php training100%placement-in-mumbaivibrantuser
90 views77 slides

Similar to WordPress Plugin Localization(20)

Design patterns illustrated 010PHP by Herman Peeren
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHP
Herman Peeren3.7K views
Living With Legacy Code by Rowan Merewood
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
Rowan Merewood25.3K views
&lt;img src="../i/r_14.png" /> by tutorialsruby
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby1.7K views
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b> by tutorialsruby
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
tutorialsruby3.7K views
Php mysql training-in-mumbai by Unmesh Baile
Php mysql training-in-mumbaiPhp mysql training-in-mumbai
Php mysql training-in-mumbai
Unmesh Baile248 views
Php training100%placement-in-mumbai by vibrantuser
Php training100%placement-in-mumbaiPhp training100%placement-in-mumbai
Php training100%placement-in-mumbai
vibrantuser90 views
Supercharging WordPress Development in 2018 by Adam Tomat
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
Adam Tomat212 views
Mastering Namespaces in PHP by Nick Belhomme
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
Nick Belhomme16.7K views
PHP from soup to nuts Course Deck by rICh morrow
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
rICh morrow2.2K views
Sugar Presentation - YULHackers March 2009 by spierre
Sugar Presentation - YULHackers March 2009Sugar Presentation - YULHackers March 2009
Sugar Presentation - YULHackers March 2009
spierre1K views
Ran Mizrahi - Symfony2 meets Drupal8 by Ran Mizrahi
Ran Mizrahi - Symfony2 meets Drupal8Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi - Symfony2 meets Drupal8
Ran Mizrahi2.7K views
PHP-03-Functions.ppt by Jamers2
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
Jamers29 views

Recently uploaded

STRATEGIC MANAGEMENT MODULE 1_UNIT1 _UNIT2.pdf by
STRATEGIC MANAGEMENT MODULE 1_UNIT1 _UNIT2.pdfSTRATEGIC MANAGEMENT MODULE 1_UNIT1 _UNIT2.pdf
STRATEGIC MANAGEMENT MODULE 1_UNIT1 _UNIT2.pdfDr Vijay Vishwakarma
90 views68 slides
JQUERY.pdf by
JQUERY.pdfJQUERY.pdf
JQUERY.pdfArthyR3
103 views22 slides
Six Sigma Concept by Sahil Srivastava.pptx by
Six Sigma Concept by Sahil Srivastava.pptxSix Sigma Concept by Sahil Srivastava.pptx
Six Sigma Concept by Sahil Srivastava.pptxSahil Srivastava
40 views11 slides
Volf work.pdf by
Volf work.pdfVolf work.pdf
Volf work.pdfMariaKenney3
75 views43 slides
INT-244 Topic 6b Confucianism by
INT-244 Topic 6b ConfucianismINT-244 Topic 6b Confucianism
INT-244 Topic 6b ConfucianismS Meyer
44 views77 slides
Pharmaceutical Analysis PPT (BP 102T) by
Pharmaceutical Analysis PPT (BP 102T) Pharmaceutical Analysis PPT (BP 102T)
Pharmaceutical Analysis PPT (BP 102T) yakshpharmacy009
101 views29 slides

Recently uploaded(20)

JQUERY.pdf by ArthyR3
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
ArthyR3103 views
Six Sigma Concept by Sahil Srivastava.pptx by Sahil Srivastava
Six Sigma Concept by Sahil Srivastava.pptxSix Sigma Concept by Sahil Srivastava.pptx
Six Sigma Concept by Sahil Srivastava.pptx
Sahil Srivastava40 views
INT-244 Topic 6b Confucianism by S Meyer
INT-244 Topic 6b ConfucianismINT-244 Topic 6b Confucianism
INT-244 Topic 6b Confucianism
S Meyer44 views
Pharmaceutical Analysis PPT (BP 102T) by yakshpharmacy009
Pharmaceutical Analysis PPT (BP 102T) Pharmaceutical Analysis PPT (BP 102T)
Pharmaceutical Analysis PPT (BP 102T)
yakshpharmacy009101 views
Nelson_RecordStore.pdf by BrynNelson5
Nelson_RecordStore.pdfNelson_RecordStore.pdf
Nelson_RecordStore.pdf
BrynNelson546 views
Class 9 lesson plans by TARIQ KHAN
Class 9 lesson plansClass 9 lesson plans
Class 9 lesson plans
TARIQ KHAN68 views
Create a Structure in VBNet.pptx by Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P82 views
JRN 362 - Lecture Twenty-Three (Epilogue) by Rich Hanley
JRN 362 - Lecture Twenty-Three (Epilogue)JRN 362 - Lecture Twenty-Three (Epilogue)
JRN 362 - Lecture Twenty-Three (Epilogue)
Rich Hanley41 views
Parts of Speech (1).pptx by mhkpreet001
Parts of Speech (1).pptxParts of Speech (1).pptx
Parts of Speech (1).pptx
mhkpreet00143 views
The Future of Micro-credentials: Is Small Really Beautiful? by Mark Brown
The Future of Micro-credentials:  Is Small Really Beautiful?The Future of Micro-credentials:  Is Small Really Beautiful?
The Future of Micro-credentials: Is Small Really Beautiful?
Mark Brown48 views
NodeJS and ExpressJS.pdf by ArthyR3
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
ArthyR347 views

WordPress Plugin Localization

  • 1. WordPress Plugin Localization By Ronald Huereca Twitter: @ronalfy Presented at WordCamp Raleigh Slides and code available at: http://ithem.es/localize May 22, 2011 1
  • 2. About Ronald Huereca Plugin developer at iThemes (the creators of Builder and BackupBuddy) Author of the book WordPress and Ajax Plugin author of Ajax Edit Comments Karaoke singer, gamer, and Vodka drinker (sometimes at the same time) 2
  • 3. What is Localization (aka, Internationalization)? Allows your strings to be translated into multiple languages Provides a framework to format your strings in a translatable way 3
  • 4. What are the benefits of Localization? Your plugins or themes can be translated by others Your audience isn’t limited by one locale or language 4
  • 5. So what does localization look like? 5
  • 6. Example: Un-localized strings :( These strings are very sad :( <div class='wrap'> ! <h2>My Plugin Settings</h2> ! <div class='updated'><p>Plugin Settings have been saved.</p></div> </div> 6
  • 7. Example: Localized strings :-) These strings are very happy :-) <div class='wrap'> ! <h2><?php _e( 'My Plugin Settings', 'DOMAIN' ); ?></h2> ! <div class='updated'><p><?php _e( 'Plugin Settings have been saved.', 'DOMAIN' ); ?></p></ div> </div> 7
  • 8. How to enable localization for a plugin? Use the WordPress ‘init’ action Function: load_plugin_textdomain (for plugins) Function: load_theme_textdomain (for themes) 8
  • 9. Enabling Localization for a Plugin Simple, simple, simple... <?php //Plugin header goes here add_action( 'init', 'pb_msp_init' ); function pb_msp_init() { ! //Admin menu ! add_action( 'admin_menu', 'pb_msp_menu' ); ! //Assuming you have a languages folder in your plugin - Typically run within the 'init' action ! load_plugin_textdomain( 'unique_domain', false, dirname ( plugin_basename( __FILE__ ) ) . '/languages/' );   } //end pb_msp_init //More functions ?> 9
  • 10. Let’s do some basic localization 10
  • 11. __( ‘string’, ‘domain’ ) Returns a localized string - domain should be unique <?php ! echo __( 'Translate me!', 'unique_domain' ); ! //Do stuff with localized string here ?> 11
  • 12. _e( ‘string’, ‘domain’ ) Echos a localized string <?php ! _e( 'Translate me!', 'unique_domain' ); ! //Output is: Translate Me! ?> 12
  • 13. For most cases, _e and __ are all you’ll ever care about 13
  • 14. But what if you have singular and plural forms of a string? 14
  • 15. For example: 1 comment vs. 2 comments 15
  • 16. Let’s use the _n() function Provide a singular string Provide a plural string Provide a count integer (e.g., 2 ) 16
  • 17. _n( ‘singular’, ‘plural’, $count, ‘domain’ ) Returns a singular or plural form of a string <?php ! $total_count = 1; ! if ( $total_count > 1 || $total_count == 0 ) $count = 2; ! else $count = 1; ! printf( _n( 'You have voted %d time', 'You have voted %d times', $count, 'unique_domain' ), $total_count ); ! //Output is: You have voted 1 time ?> 17
  • 18. What about context? Context provides... context How many ways can you interpret blue (i.e., feeling blue or literally being blue) 18
  • 19. The _x function helps provide context It helps your translators figure out what the string is for It’ll also make you think, should I use a different phrase or word? 19
  • 20. _x( ‘string’, ‘context’, ‘domain’ ) Returns a string in a specific context <?php ! echo _x( 'I am blue today.', 'noun', 'unique_domain' ); ! //You are literally blue   ! echo _x( 'I am blue today.', 'adjective', 'unique_domain' ); ! //You are feeling blue (i.e., depressed) today ?> 20
  • 21. What about localizing strings in JavaScript? 21
  • 22. Why would you ever want to localize strings in JavaScript? 22
  • 23. Why the hell not? 23
  • 24. Localizing JavaScript Queue your script like normal (wp_enqueue_script) Provide localization (via wp_localize_script) Update your scripts to use it 24
  • 25. wp_localize_script( ‘handler’, ‘object_name’, $li0n ) Outputs a JavaScript object with localized variables <?php ! wp_enqueue_script( 'my_handler', get_stylesheet_directory_uri() . '/my_script.js', array( 'jquery' ), '1.0.0', false ); ! wp_localize_script( 'my_handler', 'my_script_object', array( 'text' => __ ( 'Localization is absolutely awesome', 'unique_domain' ) ) ); ?> 25
  • 26. wp_localize_script - Continued Access the localized variable in JavaScript file my_script.js jQuery(document).ready(function( $ ) { ! alert( my_script_object.text ); }); 26
  • 27. There are a ton of helper functions 27
  • 28. Localization Functions __( ‘string’, ‘domain’ ) - Returns a localized string _e( ‘string’, ‘domain’ ) - Echos a localized string _n( ‘singular’, ‘plural’, $count, ‘domain’ ) - Returns a singular or plural form of a string _x( ‘string’, ‘context’, ‘domain’ ) - Returns a string in a context (context can be anything really, but it helps give the translators an idea of what you’re going for) esc_html_e or esc_html__ (works like __ and _e, but returns encoded text) esc_attr_e or esc_attr__ (works for encoding attribute values) wp_localize_script - Helper function for localizing JavaScript strings 28
  • 29. Even more... _nx( ‘singular’, ‘plural’, $number, ‘context’, ‘domain’ ) - Combines the _n and _x functions _esc_attr_x( ‘string’, ‘context’, ‘domain’ ) - Like the esc_attr__ function, but with context _esc_html_x( ‘string’, ‘context’, ‘domain’ ) - Like the esc_html__ function, but with context _n_noop( ‘singular’, ‘plural’ ) - Provide _n strings for translation, but will not be translated in the output (yet) _nx_noop( ‘singular’, ‘plural’, ‘context’ ) - Provide _nx strings for translation, but will not be translated in the output (yet) translate_nooped_plural ( $noop_output, $number, ‘domain’ ) - Take the results from _n_noop and _nx_noop and translate 29
  • 30. And some helper functions sprintf( ‘string format’, $arg1, $arg2, etc... ) - Returns a formatted string printf( ‘string format’ $arg1, $arg2, etc...) - Prints a formatted string 30
  • 31. How About a Video Demonstrating Localization? 31
  • 32. How About a Video Demonstrating Localization? 31
  • 33. H o w t o G e t Tr a n s l a t o r s ? Release a useful plugin that is localization ready Make sure translators can contact you The translators will come to you Package their “.mo” file with future releases Keep track of your translators and notify them before any major updates (some translators can act as beta testers) 32
  • 34. H o w t o H e l p Tr a n s l a t o r s Try to minimize da slang, ya know? Limit jargon and acronomese FTW! Translate sentences and paragraphs using sprintf or printf to provide context Avoid loooooong portions of translatable text - Split at paragraphs or sentences Avoid translating single words, and if you do, provide context (_x function) Avoid beginning or ending your string with whitespace Try translating some of your own work - Even if it’s not going in the final product, it’ll help you see what the translators see 33
  • 35. How to Generate POT Files Use Poedit with the path and keywords (ewwwww, and way too advanced - Would require another presentation) With Poedit, just generate the “po” file and rename to “pot”. At least that part is simple. Use the WordPress Repo to do it (yay!!!) 34
  • 36. WordPress Repo FTW!!! Make sure you’re logged in Get your POT 35
  • 37. <?php _e( ‘Conclusion’ ); ?> Localization is super easy Localization can increase your plugin audience The WordPress Plugin Repo is ideal for generating “pot” files. Poedit has issues. 36
  • 38. <?php echo __( “Questions?” ); ?> Slides and code available at: http://ithem.es/localize 37
  • 40. sprintf( ‘string’, [arg1,arg2, etc...] ) Returns a formatted string <?php //%s is for a string placeholder, %d is for an integer (there's a lot more of these) echo sprintf( 'My name is %s and I am %d years old', 'Ronald', 29 ); //Output is My name is Ronald and I am 29 years old   //Reuse the same string over again echo sprintf( 'My name is %1$s and here I am again %1$s and I am %d years old', 'Ronald', 29 ); //Output is My name is Ronald and here I am again Ronald and I am 29 years old ?> 39
  • 41. printf( ‘string’, [arg1,arg2, etc...] ) Prints a formatted string <?php //%s is for a string placeholder, %d is for an integer (there's a lot more of these) printf( 'My name is %s and I am %d years old', 'Ronald', 29 ); //Output is My name is Ronald and I am 29 years old   //Reuse the same string over again printf( 'My name is %1$s and here I am again %1$s and I am %d years old', 'Ronald', 29 ); //Output is My name is Ronald and here I am again Ronald and I am 29 years old ?> 40
  • 42. __ and sprintf sprintf allows you to keep strings in context without concatenation <?php ! $total_count = 100; //retrieved from somewhere ! $my_localized_string = sprintf( __( 'You have voted %d times', 'unique_domain' ), $total_count ); ! echo $my_localized_string; ! //Output is: You have voted 100 times ?> 41
  • 43. esc_html_e( ‘string’, ‘domain’ ) Translates, encodes, and echoes <div><?php esc_html_e( 'String to be outputted in HTML', 'unique_domain' ); ?></div> 42
  • 44. esc_html__( ‘string’, ‘domain’ ) Translates, encodes, and returns <?php ! $dynamic_string = 'Tigerblood'; ?> <div><?php printf( esc_html__( 'I drink %s', 'unique_domain' ), $dynamic_string ); ?></div> 43
  • 45. esc_attr_e( ‘string’, ‘domain’ ) Translates, encodes, and echoes <a href='http://pluginbuddy.com' title='<?php esc_attr_e( 'PluginBuddy - The Creators of BackupBuddy', 'unique_domain' ); ? >'>PluginBuddy</a> 44
  • 46. esc_attr__( ‘string’, ‘domain’ ) Translates, encodes, and returns <a href='http://pluginbuddy.com' title='<?php printf ( esc_attr__( '%s - The Creators of %s', 'unique_domain' ), 'PluginBuddy', 'BackupBuddy' ); ?>'>PluginBuddy</a> 45
  • 47. _e equivalent with printf and __ Combine printf and __ for a _e equivalent with formatting <?php ! printf( __( 'You have voted %d times', 'unique_domain' ), $total_count ); ! //Output is: You have voted 100 times ?> 46