3. __( 'My text to translate', 'domain' )
Fichiers domain-fr_FR.po et domain-fr_FR.mo
4. LES FICHIERS DE TRADUCTION
POUR LES EXTENSIONS
wp-content/languages/plugins/domain-{$locale}.mo
wp-content/plugins/your-plugin-lang-dir/domain-{$locale}.mo
POUR LE THEME ET LE THEME ENFANT
wp-content/languages/themes/domain-{$locale}.mo
wp-content/themes/your-theme-lang-dir/{$locale}.mo
5. CHARGER LES FICHIERS DE TRADUCTION
Si les fichiers ne sont pas dans wp-content/languages
load_plugin_textdomain( 'domain', false, 'your-plugin-lang-dir' );
load_theme_textdomain( 'domain', 'your-theme-lang-dir' );
load_child_theme_texdomain( 'domain', 'your-theme-lang-dir' );
6. INCORRECT
__( 'My text to translate', $domain )
__( 'My text to translate', DOMAIN )
CORRECT
__( 'My text to translate', 'domain' )
22. __( 'You are not the only participant', 'domain' )
Quelle traduction choisir ?
'Vous n'êtes pas le seul participant' ?
'Vous n'êtes pas la seule participante' ?
23. Les fonctions __(), _x() et _n() se combinent :
_e(), _ex(), _nx()
esc_html_e(), esc_attr_e(), etc...
28. Conséquence n°1
La langue d'affichage est obtenue par :
$locale = is_admin() ? get_user_locale() : get_locale();
LANGUE ADMIN PERSONNALISÉE (WP 4.7)
29. Conséquence n°2
Une requête ajax coté site est localisée dans la langue de
l'utilisateur si celui-ci est connecté.
LANGUE ADMIN PERSONNALISÉE (WP 4.7)
31. FORCER LA LANGUE DE LA BARRE D'ADMINISTRATION
// On charge le français juste avant d'afficher la barre
add_action( 'admin_bar_menu', 'force_admin_bar_in_fr', -1 ) ;
function force_admin_bar_in_fr() {
switch_to_locale( 'fr_FR' );
}
// Retour à la langue courante juste après
add_action( 'admin_bar_menu', 'restore_previous_locale', 999 );
32.
33. RECHARGER LES FICHIERS DE TRADUCTION
// Les fichiers po/mo ne sont pas dans wp-content/languages
add_action( 'change_locale', 'wptech_plugin_load_textdomain' );
function wptech_plugin_load_textdomain() {
load_plugin_textdomain(
'wptech',
false,
basename( dirname( __FILE__ ) ) . '/languages'
);
}
34.
35. RECHARGER LES TYPES DE CONTENU ET TAXINOMIES
add_action( 'change_locale', 'wptech_register_post_type' );
function wptech_register_post_type() {
register_post_type( … ) ;
}
36.
37. CORRIGER LE PROBLEME AJAX
add_action( 'wp_ajax_nopriv_wptech_ajax', 'wptech_ajax' );
add_action( 'wp_ajax_wptech_ajax', 'wptech_ajax' );
// Réponse à une requête ajax coté front
function wptech_ajax() {
switch_to_locale( get_locale() );
...
}
38. POUR LE JAVASCRIPT
Les traductions sont préparées en PHP :
$strings['some_text'] = __( 'My text to translate', 'domain' );
wp_localize_script( 'script_id', 'object', $strings );
On récupère les chaînes traduites :
str = object.some_text;
PAS DE GESTION CORRECTE DU PLURIEL