Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress i18n Cheatsheet

WordPress i18n CheatSheet

Whenever I write plugins or themes, there is one thing that needs a little extra attention and is quite frankly hard to get right: Translatable text. This list should helps me to find the right way fast.

Props to Alex Kirk, there list it inside a quiz.

You want to output the username in a sentence.

Assume that the $username has been escaped using esc_html().

<?php
printf(
  // Translators: %s is a username.
  __( 'Howdy, %s!' ),
  $username
);

Include a link in a sentence.

<?php
printf(
  __( 'Publish something using our <a href="%s">Post by Email</a> feature.'),
  'http://support.wordpress.com/post-by-email/'
);

Correct way for the single/plural _n() function.

printf(
  _n(
    '%d person has seen this post.',
    '%d people have seen this post.',
    $view_count
  ),
  $view_count
);

Outputting a variable in the context of a translation.

printf(
  __( 'Hello %s' ),
  esc_html( $world )
);

Include formatted numbers in strings.

printf(
  _n(
    'Today you already got %s view.',
    'Today you already got %s views.',
    $view_count
  ),
  number_format_i18n( $view_count )
);

Deal with multiple variables in a translated string.

printf(
  // Translators: %1$s is a date, %2$s is a username.
  __( 'Posted on %1$s by %2$s.' ),
  $date,
  $username
);

Last Hint.

Developers should to give translators full sentences/phrases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment