You have a plugin, but you want users to be able to use it in their native language. Learn how to get it ready for translation, things to watch out for, and tips for maintaining it as you change the plugin over time.
16. __( ’Fetch a single event using the ID of
that event’, ‘event-calendar-newsletter’ );
_ _( $string, $textdomain )
17. __( ’Fetch a single event using the ID of
that event’, ‘event-calendar-newsletter’ );
_ _( $string, $textdomain )
18. __( ’Fetch a single event using the ID of
that event’, ‘event-calendar-newsletter’ );
_ _( $string, $textdomain )
19. __( ’Fetch a single event using the ID of
that event’, ‘event-calendar-newsletter’ );
_ _( $string, $textdomain )
Type this out!
20. // can’t do this
define( ‘ECN_TEXT_DOMAIN’, ‘event-
calendar-newsletter’ );
// ...
__( ’Fetch a single event using the ID
of that event’, ECN_TEXT_DOMAIN );
Wrong Way
21. __( ’Fetch a single event using the ID of
that event’, ‘event-calendar-newsletter’ );
Right Way
22. echo __( ’Fetch a single event
using the ID of that event’,
‘event-calendar-newsletter’ );
Outputting the String
23. _e( ’Fetch a single event using
the ID of that event’, ‘event-
calendar-newsletter’ );
Outputting the String
_e === echo + __
24. What if you have
something you don’t want
translated?
25. Exclude a single event from the
listing. Use "current" when using the
shortcode on an event page to exclude
the current event.
Using Placeholders
26. Exclude a single event from the
listing. Use "current" when using the
shortcode on an event page to exclude
the current event.
Using Placeholders
27. __( ‘Exclude a single event from the
listing. Use "current" when using the
shortcode on an event page to exclude
the current event.’, ‘my-shortcode’ )
Using Placeholders
28. sprintf( __( ‘Exclude a single event
from the listing. Use "%s" when using
the shortcode on an event page to
exclude the current event.’, ‘my-
shortcode’ ), ‘current’ );
Using Placeholders
29. echo sprintf( __( ‘Exclude a single
event from the listing. Use "%s" when
using the shortcode on an event page to
exclude the current event.’, ‘my-
shortcode’ ), ‘current’ );
Using Placeholders
30. Exclude a single event from the
listing. Use "current" when using the
shortcode on an event page to exclude
the current event.
Using Placeholders
32. Can't find the option you're looking
for? <a href="https://mysite.com/
support">Submit a support request</a>
and we'll do our best to help!
Outputting HTML
33. _e( "Can’t find the option you're looking
for? <a href="https://mysite.com/support
">Submit a support request</a> and we'll
do our best to help!", ‘my-shortcode’ );
Outputting HTML
34. _e( "Can’t find the option you're
looking for? %sSubmit a support request
%s and we'll do our best to help!",
‘my-shortcode’ );
Outputting HTML
35. echo sprintf( __( "Can’t find the option
you're looking for? %sSubmit a support
request%s and we'll do our best to help!",
‘my-shortcode’ ), '<a href="https://
mysite.com/support">', ‘</a>' );
Outputting HTML
38. Can’t find the option you're looking
for? %sSubmit a support request%s and
we'll do our best to help!
39. Sie können die gewünschte Option nicht
finden? %sSenden Sie eine Support-Anfrage
%s und wir werden unser Bestes tun, um zu
helfen!
40. Sie können die gewünschte Option nicht
finden? %sSenden Sie eine Support-Anfrage
%s <script>...</script>und wir werden
unser Bestes tun, um zu helfen!
44. esc_html( __( "bla bla bla hihihihi”,
‘my-shortcode’ ) );
Escape Functions
45. esc_html( __( "bla bla bla <script>alert(‘test’);</
script> hihihihi”, ‘my-shortcode’ ) );
Escape Functions
46. esc_html( __( "bla bla bla <script>alert(‘test’);</
script> hihihihi”, ‘my-shortcode’ ) );
Escape Functions
bla bla bla <script>alert(‘test’);</
script> hihihihi
72. Best Practices
• Decent English style
• Entire sentences
• Split at paragraphs
• Use format strings instead of string concatenation—
sprintf(__('Replace %1$s with %2$s'), $a, $b); is
always better than __('Replace ').$a.__(' with ').$b;
• Avoid unusual markup and unusual control characters
• Do not leave leading or trailing whitespace in a translatable
phrase
https://codex.wordpress.org/
I18n_for_WordPress_Developers#Best_Practices
74. Now What?
• Generate the POT file (base language)
• Copy to a PO file (for each translation)
• Generate the MO file (compiled for
quick string access)
• Put them all in the languages/ folder
of your plugin
https://brianhogg.com/how-wordpress-org-plugin-
translations-are-handled/
75. ...
#: core/templates/admin-page.php:2 edd/edd.php:46
msgid "The Events Calendar Shortcode"
msgstr ""
#: core/templates/admin-page.php:4
msgid ""
"The shortcode displays lists of your events. For
example the shortcode to "
"show next 8 events in the category "%s" in ASC order
with date showing:"
msgstr ""
my-plugin.pot
76. #: core/templates/admin-page.php:2 edd/edd.php:46
msgid "The Events Calendar Shortcode"
msgstr "The Events Calendar Shortcode"
#: core/templates/admin-page.php:4
msgid ""
"The shortcode displays lists of your events. For example the
shortcode to "
"show next 8 events in the category "%s" in ASC order with
date showing:"
msgstr ""
"O shortcode mostra listas de seus eventos. Por exemplo, o
shortcode para "
"mostrar os próximos 8 eventos na categoria "%s", em ordem
ASC exibindo a "
"data:"
my-plugin-pt_BR.po