SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
INTERNATIONALISEZ
VOS DEVELOPPEMENTS
__( 'My text to translate', 'domain' )
__( 'My text to translate', 'domain' )
Fichiers domain-fr_FR.po et domain-fr_FR.mo
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
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' );
INCORRECT
__( 'My text to translate', $domain )
__( 'My text to translate', DOMAIN )
CORRECT
__( 'My text to translate', 'domain' )
https://wordpress.org/plugins/advanced-custom-fields/
INCORRECT
__( 'My text to translate', 'acf' )
CORRECT
__( 'My text to translate', 'advanced-custom-fields' )
INCORRECT
__( 'My custom text to translate', 'woocommerce' )
CORRECT MAIS RISQUÉ
__( 'My account', 'woocommerce' )
__( 'My text to translate', 'domain' )
INCORRECT
echo __( 'Long sentence…' . 'splitted in two.', 'domain' );
echo __( "Use $id instead.", 'domain' ) ;
echo __( 'Use', 'domain ) . $id . __( 'instead.', 'domain' ) ;
CORRECT
printf( __( 'Use %s instead.', 'domain' ), $id ) ;
Traduit en 'Utilisez plutôt %s'
ENCORE MIEUX
/* translators : %s is a user id */
printf( __( 'Use %s instead.', 'domain' ), $id ) ;
INCORRECT
__( 'Draft created on %s at %s', 'domain' )
CORRECT
__( 'Draft created on %1$s at %2$s', 'domain' )
/* translators: 1: month, 2: day, 3: year, 4: hour, 5: minute */
__( %1$s %2$s, %3$s @ %4$s:%5$s )
Traduit en '%2$s %1$s %3$s à %4$s h %5$s min'
__( 'Book', 'domain' )
Quelle traduction choisir ?
'Livre' ?
'Réserver' ?
_x( 'Book', 'verb', 'domain' )
Contexte de traduction
Ce n'est pas un commentaire
LE PROBLEME DU PLURIEL
ANGLAIS
0 books
1 book
2 books
FRANÇAIS
0 livre
1 livre
2 livres
ANGLAIS
nplurals=2; plural=n != 1;
FRANÇAIS
nplurals=2; plural=n > 1;
CROATE
nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2
&& n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
ARABE
nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3
&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;
_n( '%d comment', '%d comments', $n, 'domain' )
Singulier Anglais
Pluriel Anglais
Nombre
__( 'You are not the only participant', 'domain' )
Quelle traduction choisir ?
'Vous n'êtes pas le seul participant' ?
'Vous n'êtes pas la seule participante' ?
Les fonctions __(), _x() et _n() se combinent :
_e(), _ex(), _nx()
esc_html_e(), esc_attr_e(), etc...
$labels = array(
'name' => __( 'Books', 'wptech' ),
'singular_name' => __( 'Book', 'wptech' ),
);
$args = array(
'labels' => $labels,
'public' => true,
);
register_post_type( __( 'book', 'wptech' ), $args );
UTILISATION INCORRECTE
FORMATER UN NOMBRE
number_format_i18n( $number, $decimals )
1,234.56 en anglais
1 234,56 en français
FORMATER UNE DATE
date_i18n( get_option( 'date_format'), time() )
date_i18n( __( F j, Y ), time() )
LANGUE ADMIN PERSONNALISÉE (WP 4.7)
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)
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)
switch_to_locale( 'fr_FR' ) ;
restore_previous_locale() ;
restore_current_locale() ;
CHANGER LA LANGUE D'AFFICHAGE (WP 4.7)
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 );
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'
);
}
RECHARGER LES TYPES DE CONTENU ET TAXINOMIES
add_action( 'change_locale', 'wptech_register_post_type' );
function wptech_register_post_type() {
register_post_type( … ) ;
}
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() );
...
}
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
MAIS WORDPRESS 5.0 ARRIVE...
UN EXTRAIT DU CODE JS DE GUTENBERG...
if ( this.state.filterValue && !! searchResults.length ) {
this.props.debouncedSpeak( sprintf( _n(
'%d result found',
'%d results found',
searchResults.length
), searchResults.length ), 'assertive' );
} else if ( this.state.filterValue ) {
this.props.debouncedSpeak( __( 'No results.' ), 'assertive' );
}
RESSOURCES
https://developer.wordpress.org/themes/functionality/internationalization/
https://developer.wordpress.org/plugins/internationalization/
http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/
https://boiteaweb.fr/traduction-wordpress-vous-faites-fausse-route-8518.html
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/l10n.php
https://translate.wordpress.org/
Internationalisez vos développements - WP Tech 2018

Contenu connexe

Similaire à Internationalisez vos développements - WP Tech 2018

Similaire à Internationalisez vos développements - WP Tech 2018 (20)

Les stratégies de localisation d'une application Flex
Les stratégies de localisation d'une application FlexLes stratégies de localisation d'une application Flex
Les stratégies de localisation d'une application Flex
 
Lp web tp3_idse
Lp web tp3_idseLp web tp3_idse
Lp web tp3_idse
 
Etes vous-pret pour php8 ?
Etes vous-pret pour php8 ?Etes vous-pret pour php8 ?
Etes vous-pret pour php8 ?
 
PHP mysql Xml.pdf
PHP mysql Xml.pdfPHP mysql Xml.pdf
PHP mysql Xml.pdf
 
Initiation au langage PHP
Initiation au langage PHPInitiation au langage PHP
Initiation au langage PHP
 
PHP mysql Xml.doc
PHP mysql Xml.docPHP mysql Xml.doc
PHP mysql Xml.doc
 
Le client FTP de PHP5
Le client FTP de PHP5Le client FTP de PHP5
Le client FTP de PHP5
 
Email et PHP5
Email et PHP5Email et PHP5
Email et PHP5
 
Playing With PHP 5.3
Playing With PHP 5.3Playing With PHP 5.3
Playing With PHP 5.3
 
PHP_intro.pdf
PHP_intro.pdfPHP_intro.pdf
PHP_intro.pdf
 
PHP 6, la prochaine frontière
PHP 6, la prochaine frontièrePHP 6, la prochaine frontière
PHP 6, la prochaine frontière
 
Internationaliser les projets VCL / FMX
Internationaliser les projets VCL / FMXInternationaliser les projets VCL / FMX
Internationaliser les projets VCL / FMX
 
Les principes de base de PHP
 Les principes de base de PHP  Les principes de base de PHP
Les principes de base de PHP
 
Php cours
Php coursPhp cours
Php cours
 
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfCours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
 
PHP.pptx
PHP.pptxPHP.pptx
PHP.pptx
 
Des tests modernes pour Drupal
Des tests modernes pour DrupalDes tests modernes pour Drupal
Des tests modernes pour Drupal
 
Quelques astuces pour débogguer Terraform.pdf
Quelques astuces pour débogguer Terraform.pdfQuelques astuces pour débogguer Terraform.pdf
Quelques astuces pour débogguer Terraform.pdf
 
Php 7.4 2020-01-28 - afup
Php 7.4   2020-01-28 - afupPhp 7.4   2020-01-28 - afup
Php 7.4 2020-01-28 - afup
 
Php1
Php1Php1
Php1
 

Internationalisez vos développements - WP Tech 2018