Created
December 9, 2012 22:27
-
-
Save dimadin/4247285 to your computer and use it in GitHub Desktop.
Load only admin bar translation on WordPress frontend
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
/** | |
* 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