Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active January 7, 2019 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davilera/b8e0f464bdd854909cf3b105f215ddc1 to your computer and use it in GitHub Desktop.
Save davilera/b8e0f464bdd854909cf3b105f215ddc1 to your computer and use it in GitHub Desktop.

Examples of good and bad practices in WordPress plugin i18n.

<?php
/**
* Aquesta funció retorna el usuaris que...
*/
function nelio_cerca_usuaris() {
// Inicialitzem la llista d'usuaris.
$usuaris = array();
// Executem la cerca i afegim els resultats a la llista.
// ...
wp_send_json( $usuaris );
}//end nelio_cerca_usuaris()
<?php
/**
* This function returns the list of users that...
*/
function nelio_search_users() {
// Initializing user list.
$users = array();
// Search users and populate $user list.
// ...
wp_send_json( $users );
}//end nelio_search_users()
<?php
echo esc_html( 'This is a non-translatable message.' );
<?php
echo esc_html( __( 'This string can be translated.', 'plugin-name' ) );
# Plugins:
php /path/to/makepot.php wp-plugin . languages/plugin-name.pot
# Themes:
php /path/to/makepot.php wp-theme . languages/plugin-name.pot
# Plugins:
php /ruta/al/fichero/makepot.php wp-plugin . languages/plugin-name.pot
# Themes:
php /ruta/al/fichero/makepot.php wp-theme . languages/plugin-name.pot
<?php
/**
* The plugin bootstrap file.
*
* Plugin Name: Example
* Plugin URI: https://example.com
* Description: Describe your plugin here.
* Version: 1.0.0
*
* Author: Nelio Software
* Author URI: https://neliosoftware.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* Text Domain: plugin-name
* Domain Path: /languages
*
* @author David Aguilera <david.aguilera@neliosoftware.com>
* @since 1.0.0
*/
/**
* Load i18n strings.
*
* @since 1.0.0
*/
function plugin_name_i18n() {
load_plugin_textdomain( 'plugin-name', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}//end plugin_name_i18n()
add_action( 'plugins_loaded', 'plugin_name_i18n' );
<?php
if ( $something ) {
$elem = __( 'user', 'plugin-name' );
} else {
$elem = __( 'post', 'plugin-name' );
}//end if
echo esc_html( __( 'The ', 'plugin-name' ) . $elem . __( ' has been deleted.', 'plugin-name' ) );
<?php
if ( 1 === $count ) {
echo esc_html( __( '1 result', 'plugin-name' ) );
} else {
echo esc_html( $count . __( ' results', 'plugin-name' ) );
}//end if
<?php
echo esc_html( $count . __( ' result', 'plugin-name' ) . ( 1 !== $count ) ? 's' : '' );
<?php
if ( $something ) {
echo esc_html( __( 'The user has been deleted.', 'plugin-name' ) );
} else {
echo esc_html( __( 'The post has been deleted.', 'plugin-name' ) );
}//end if
<?php
echo esc_html( __( 'Hello, ', 'plugin-name' ) . $name );
<?php
printf(
esc_html( __( 'Hello, %s', 'plugin-name' ) ),
$name
);
<?php
if ( 1 === $count ) {
echo esc_html( __( '1 result', 'plugin-name' ) );
} else {
printf(
esc_html( __( '%d results', 'plugin-name' ) ),
$count
);
}//end if
<?php
printf(
esc_html( _n( '%d result', '%d results', $count, 'plugin-name' ) ),
$count
);
<h1><?php echo esc_html( _x( 'Comment', 'text', 'plugin-name' ) ); ?></h1>
<form ...>
...
<input type="submit" value="<?php echo esc_attr( _x( 'Comment', 'command', 'plugin-name' ) ); ?>" />
</form>
<?php
echo esc_html( __( 'g:i:s a', 'my-text-domain' ) );
<?php
/* translators: draft saved date format, see http://php.net/date */
echo esc_html( __( 'g:i:s a', 'my-text-domain' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment