Skip to content

Instantly share code, notes, and snippets.

@dimadin
Created December 9, 2012 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimadin/4247285 to your computer and use it in GitHub Desktop.
Save dimadin/4247285 to your computer and use it in GitHub Desktop.
Load only admin bar translation on WordPress frontend
/**
* Set en_US lang code if not in admin.
*/
function md_set_en_us_locale( $locale ) {
if ( ! is_admin() ) {
if ( ! defined( 'MD_REAL_LANG' ) )
define( 'MD_REAL_LANG', $locale );
$locale = 'en_US';
}
return $locale;
}
add_filter( 'locale', 'md_set_en_us_locale', 1 );
/**
* Set real lang code.
*/
function md_set_real_locale( $locale ) {
if ( defined( 'MD_REAL_LANG' ) )
$locale = MD_REAL_LANG;
return $locale;
}
/**
* Load translations for admin bar.
*
* This loads only deafult (ie. core WP) translations,
* it's possible to load plugins & themes translations
* but that requeries more code.
*/
function md_load_translations() {
unload_textdomain( 'default' );
add_filter( 'locale', 'md_set_real_locale', 2 );
load_default_textdomain();
}
add_action( 'admin_bar_init', 'md_load_translations', 1 );
add_action( 'wp_before_admin_bar_render', 'md_load_translations', 1 );
/**
* Unload translations after admin bar
*/
function md_unload_translations() {
unload_textdomain( 'default' );
remove_filter( 'locale', 'md_set_real_locale', 2 );
load_default_textdomain();
}
add_action( 'wp_after_admin_bar_render', 'md_unload_translations', 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment