pot • Lang string translations - martindubenet/Wordpress GitHub Wiki

A link tink to my Ui common Fr/En strings to translate

References

  1. «Translating WordPress» project to get .po or contribute in translations for popular themes or plugins.
  2. «Theme Guide: Internationalization» by Developer.Wordpress.com
  3. «Translating the theme you created» by WPML.
  4. «Internationalize your wordpress site» by Lizzie Kardon on Smashing Magazine.
  5. «How to Localize a WordPress Theme and Make it Translation Ready» by Raelene Morey on WP MU Dev.
  6. «gettext» system and «i18n» internationalization and localization protocols.

String translation format

  1. String to translate,
  2. Plural format,
  3. Counter to specify starting from how many does the Single form switch to it's Plural form. Usually « 2 ».
  4. Context are comments to help the translators understand what this string is about,
  5. Text Domain where the string is used according to gettext/i18n standards.
    In Wordpress that would be your theme’s Text Domain slug declared in /style.css.

 

Method

en_US first!

Since 2014 the «Language Chooser in 4.0» is setting the WPLANG parameter that defines the language of the Wordspress solution. This parameter’s value is useful ONLY for unilingual websites or specific function like the one below to debug slug.

When dealign with translations (in WordPress) it is mandatory to compose your strings of text in US English (en_US), not in any other (Canadian) English variant. Since Wordpress, like most of the webapps today, is coded by/for Americans. And both our primary tools and the vanilla Wordpress.org applications are set as en_US by default.

This means that if you composed your strings of text in French, Spanish or whatever other language this means that you FIRST need to translate your sources files right away in en_US BEFORE starting your pot template otherwise you will hit a wall later on.

In your theme/plugin…

  1. Regroup all your strings within a .pot file named according to the EXACT name of your Theme/Plugin directory,
  2. In your PHP Theme/Plugin declaration, the comments at the biginning of the main file (./anyplugin/anyplugin.php), add the Domain Path parameter : Domain Path: /languages/.
  3. Open yourPlugin.pot using the Poedit free app,
    1. Specify in what lang you want to translate the PO Template,
    2. Translate the strings,
    3. Save the file as « Language » and « Country » in their ISO-639 format : fr_CA.po.

Files

./yourPlugin/languages/en_CA.mo
./yourPlugin/languages/en_CA.po
./yourPlugin/languages/fr_CA.mo
./yourPlugin/languages/fr_CA.po
./yourPlugin/languages/fr_FR.mo
./yourPlugin/languages/fr_FR.po
./yourPlugin/languages/yourPlugin.pot

PHP method

If you need to integrate a <br> (break line) within a string use the Regular Expression «\n».

«/// TRANSLATORS:» _is

call method description example
__() Echo the string within an argument of a function OR within an already existing echo(). <?php function {
    echo( __('String', 'Comment', Context') );
}; ?>
_e() Shorthand for « echo( __() ) » <?= _e('String', 'Comment', Context'); ?>
printf() PHP Print Function to output (echo) a formatted string to the screen. Where its sibling, the sprintf() function returns the value.
/// TRANSLATORS: Comments starting with with this nomenclature are used to give translators hints. /// TRANSLATORS: %s contains the user's name as specified in Preferences
printf(_("My name is %s.\n"), my_name);
_n() Same as ngettext() in a Wordpress function that translates and retrieves the singular or plural form based on the supplied count number (usually 2). $cont = 2;
printf( _n( '%s person', '%s people', $count, 'Context' ), number_format_i18n( $count ));
_nx() Same type of Wordpress function as _n() but with with gettext context. printf( _nx( '%s group', '%s groups', $people, 'group of people', 'Context' ), number_format_i18n( $people ) );
_n_noop() Registers plural strings in POT file, but does not translate them until declared within a printf( translate_nooped_plural(…)).
%s gettext function for dynamic text translation. to allow one parameter within a string
%1$s & %2$s gettext function to allow multiple parameters within a string. Requires to be echo within a printf() function. printf( __( 'Contains %1$s and %2$s' ), $var1, $var2 );
esc_url, esc_html & esc_html__ Escaping HTML strings with dynamic attributes.
esc_attr & esc_attr_e Without the _e (echo) suffix is for escaping simple HTML attributes. With _e it display translated text that has been escaped for safe use in an attribute.

 

Portable Object Template (POT) file

Both po and mo are generated simultaniously by Poedit.net when editing a .pot file.

nomenclature name definition
.pot Portable Object Template The «t» stands for « Template », it is the parent file. Regroup all your strings within this .pot and keep it up-to-date. DO NOT DELETE EXISTING ENTRIES, this will generate conflicts with the .po/.mo child files.
.po Portable Object Human readable version of the translated keys from the pot parent file.
.mo Machine Object Binary data version of the translated keys from the pot parent file.
#: Reference This parameter is for (relative to root) path and line number (example.php:19) where to find the string. You can list multiple values by seperating them with a white space.
#. Comment for translators Use with plural IDs so the editor app knows how to handle it.
msgctxt "" Message Context OPTIONAL! Comments for the translators. Helps to explain where the string(s) is/are used.
msgid "" Message ID The original string (in en_US)
msgid_plural "" Plural Message ID OPTIONAL! Plural variant of the ID string
msgstr "" Message String The translation. ALWYS LEAVE the message string value empty in pot files

underscore/languages/underscore.pot

# Copyright (C) 2017 Automattic
msgid ""
msgstr ""
"Project-Id-Version: Any Plugin 1.0.0\n"
"Report-Msgid-Bugs-To: https://example.com/anyplugin\n"
"Last-Translator: Me <[email protected]>\n"
"POT-Creation-Date: 2016-12-23 16:00+0100\n"
"PO-Revision-Date: 2021-10-06 HO:MI+ZONE\n"
"X-Generator: Poedit 3.0\n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: grunt-wp-i18n 0.5.4\n"

#: content.php:40
#. translators: 1: Comments count number, 2: Post title.
msgctxt "Comments title"
msgid "%1$s thought on %2$s"
msgid_plural "%1$s thoughts on %2$s"
msgstr[0] ""
msgstr[1] ""

underscore/content.php

printf( // WPCS: XSS OK.
	/* translators: 1: Comments count number, 2: Post title. */
	esc_html( _nx( '%1$s thought on %2$s', '%1$s thoughts on %2$s', $anyplugin_comment_count, 'Comments title', 'anyplugin Comments' ) ),
	number_format_i18n( $anyplugin_comment_count ),
	'<q>' . get_the_title() . '</q>'
);

 

Debug slug translation

Back in October 10th 2011 Christopher Davis answered the StackExchange question «Theme localization of "slugs" (custom post types, taxonomies)». His detailed answer stayed since as THE work around for translating the 'rewrite => array('slug') parameter. He propose two options:

  1. Use his «Custom-Post-Type Base» plugin to add a fields in the Dashboard menu SettingPermalinks so the site editor can define his own slug value,
  2. Using a function to rewrite the slug values based on a predefined set of WPLANG values.

 

Tools

Force the language

It is possible to over-rule the language set that a dashboard user has define in the database by declaring the « language and country » in its i18n format.

// Force language and country code for this site.
define ('WPLANG', 'fr_CA');

Useful PHP lines to get the first two letters from the WPLANG parameter in your theme plage.

$page_lang	= substr( get_bloginfo ( 'language' ), 0, 2 );
if ($page_lang === 'fr') {
	$page_lang_fr = true;
} else {
	$page_lang_fr = false;
}
…
if ( $page_lang_fr === true ) {
    …
}

App

Save yourself nightmares and install the free Poedit.net standalone app.

⚠️ **GitHub.com Fallback** ⚠️