Examples of good and bad practices in WordPress plugin i18n.
Last active
January 7, 2019 09:41
-
-
Save davilera/b8e0f464bdd854909cf3b105f215ddc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo esc_html( 'This is a non-translatable message.' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo esc_html( __( 'This string can be translated.', 'plugin-name' ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plugins: | |
php /path/to/makepot.php wp-plugin . languages/plugin-name.pot | |
# Themes: | |
php /path/to/makepot.php wp-theme . languages/plugin-name.pot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
* @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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( 1 === $count ) { | |
echo esc_html( __( '1 result', 'plugin-name' ) ); | |
} else { | |
echo esc_html( $count . __( ' results', 'plugin-name' ) ); | |
}//end if |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo esc_html( $count . __( ' result', 'plugin-name' ) . ( 1 !== $count ) ? 's' : '' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo esc_html( __( 'Hello, ', 'plugin-name' ) . $name ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
printf( | |
esc_html( __( 'Hello, %s', 'plugin-name' ) ), | |
$name | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( 1 === $count ) { | |
echo esc_html( __( '1 result', 'plugin-name' ) ); | |
} else { | |
printf( | |
esc_html( __( '%d results', 'plugin-name' ) ), | |
$count | |
); | |
}//end if |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
printf( | |
esc_html( _n( '%d result', '%d results', $count, 'plugin-name' ) ), | |
$count | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo esc_html( __( 'g:i:s a', 'my-text-domain' ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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