Last active
August 29, 2015 14:13
-
-
Save brandondove/a0f43ce016a9566f3912 to your computer and use it in GitHub Desktop.
WordPress Translation Function Code Samples
This file contains 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 | |
/* | |
Plugin Name: WordPress Translation Examples | |
Description: Sample code to show how to do translations | |
Plugin URI: https://gist.github.com/brandondove/a0f43ce016a9566f3912 | |
Author: Pixel Jar | |
Author URI: http://www.pixeljar.com | |
Version: 1.0 | |
License: GPL2 | |
Text Domain: brandon | |
Domain Path: /lang | |
Copyright (C) 2015 Pixel Jar info@pixeljar.com | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
// Wait until the plugin is loaded | |
add_action( 'plugins_loaded', 'brandon_load_textdomain' ); | |
function brandon_load_textdomain() { | |
load_plugin_textdomain( | |
'brandon', | |
false, | |
dirname( plugin_basename( __FILE__ ) ) . '/langs' | |
); | |
} | |
/** | |
* Examples of WordPress translation functions | |
*/ | |
/* Example usage for _e(). This function directly outputs the translated text. */ | |
_e( 'This text is awesome!', 'brandon' ); | |
/* Example usage for __(). This function returns the translated text. */ | |
$output = __( 'This text is awesome!', 'brandon' ); | |
/* Example usage for _n(). This function returns the singular or plural version of the translated | |
text. */ | |
$singular = _n( 'Developer', 'Developers', 1, 'brandon' ); // returns Developer | |
$plural = _n( 'Developer', 'Developers', 2, 'brandon' ); // returns Developers | |
/* Example usage for _x(). This function returns the translated text, and provides context for the | |
translator */ | |
$output = _x( 'Read', 'past participle: books I have read', 'brandon' ); | |
/* Example usage for _ex(). This function directly outputs the translated text, and provides | |
context for the translator */ | |
_ex( 'Read', 'past participle: books I have read', 'brandon' ); | |
/* Example usage for _nx(). This function returns the singular or plural version of the translated | |
text, and provides context for the translator */ | |
$singular = _nx( 'Birdie', 'Birdies', 1, 'golf term: I shot a birdie on hole 9', 'brandon' ); // returns Birdie | |
$plural = _nx( 'Birdie', 'Birdies', 2, 'golf term: I shot two birdies on holes 9 and 10', 'brandon' ); // returns Birdies | |
/** | |
* Examples of escape and translate functions | |
*/ | |
/* Example usage for esc_attr__(). Returns an excaped and translated version of the text with the | |
html attribute context. */ | |
$output = sprintf( | |
'<a href="http://brandondove.com/" title="%s">Click Here</a>', | |
esc_attr__( 'This is a link!', 'brandon' ) | |
); | |
/* Example usage for esc_attr__(). Returns an excaped and translated version of the text with the | |
html attribute context. */ | |
?><a href="http://brandondove.com/" title="<?php esc_attr_e( 'This is a link!', 'brandon' ); ?>">Click Here</a><?php | |
/* Example usage for esc_attr_x(). Returns an excaped and translated version of the text with the | |
html attribute context with context for translators. */ | |
$output = sprintf( | |
'<a href="http://brandondove.com/" title="%s">Click Here</a>', | |
esc_attr_x( 'Check out this birdie shot!', 'birdie shot is a golf term', 'brandon' ) | |
); | |
/* Example usage for esc_html__(). Returns an excaped and translated version of the text with the | |
html context. */ | |
$output = sprintf( | |
'<a href="http://brandondove.com/">%s</a>', | |
esc_html__( 'Click Here!', 'brandon' ) | |
); | |
/* Example usage for esc_html__(). Returns an excaped and translated version of the text with the | |
html context. */ | |
?><a href="http://brandondove.com/"><?php esc_html_e( 'Click Here!', 'brandon' ); ?></a><?php | |
/* Example usage for esc_html_x(). Returns an excaped and translated version of the text with the | |
html context with context for translators. */ | |
$output = sprintf( | |
'<a href="http://brandondove.com/">%s</a>', | |
esc_html_x( 'Check out this birdie shot!', 'birdie shot is a golf term', 'brandon' ) | |
); | |
/** | |
* Examples of localization functions | |
*/ | |
// Hook in and enqueue script | |
add_action( 'wp_enqueue_script', 'brandon_enqueue_script' ); | |
function brandon_enqueue_script() { | |
// Register the script | |
wp_register_script( | |
'brandon-script', | |
get_template_directory_uri() . 'src/brandon.js', | |
array('jquery'), | |
'1.0', | |
false | |
); | |
// Localize data | |
wp_localize_script( | |
'brandon-script', | |
'brandon', | |
array( | |
'first_name' => $first_name, | |
'middle_name' => 'Michael', | |
'last_name' => 'Dove', | |
'intro' => esc_js( __( 'Hi', 'brandon' ) ) | |
) | |
); | |
// Enqueue the script | |
wp_enqueue_script( 'brandon-script' ); | |
} | |
?> | |
<!-- Usage in javascript file--> | |
<script src="/wp-content/theme/src/brandon.js"> | |
// Alert the user's first name | |
alert( brandon.intro + ' ' + brandon.first_name + '!' ); | |
</script> | |
<?php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment