Overriding language for a Drupal backend
Someone in my DPC Drupal tutorial brought up the topic of multi-lingual sites, and asked how to run the backend of the site in English, when the frontend is in Dutch...
Text in Drupal should run through the t() function, which handles translation. t() checks the global $language variable to choose the target language ($language may be set from user preferences, from a path parameter, from content-negotiation...it will be set automatically).
The solution is to implement an early bootstrap hook - hook_init - to change the language.
Assuming a custom Drupal module called 'backend_language':
<?php /** * Implementation of hook_init */ function backend_language_init() { // get our target backend language from a variable config: $backend_language = variable_get('backend_language', FALSE); // check if the current language is different if(_backend_language_is_backend() && $GLOBALS['language']->language != $backend_language) { // check that our desired language exists $languages_available = language_list(); if(array_key_exists($backend_language, $languages_available)) { // override language to use our preferred language $GLOBALS['language'] = $languages[$backend_language]; } } } /** * Check whether the user is vising a backend-page. */ function _backend_language_is_backend() { // use the same rules that govern the 'admin theme' setting at admin/settings/admin // check that the first path parameter is 'admin', or (if the "Use administration theme for content editing" checkbox is checked, if it's a node add/edit form). // see system.module:534 return (arg(0) == 'admin' || (variable_get('node_admin_theme', '0') && arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit'))); }
- Start bootstrap
- Switch to English
- Generate admin menu
- Switch to previously-used language
- Continue to render page
- backend_language module: -10
- admin_menu: -9
- backend_language_restore module: -8
<?php /** * Implementation of hook_init */ function backend_language_init() { // get our target backend language from a variable config: $backend_language = variable_get('backend_language', FALSE); // check if the current language is different if(_backend_language_is_backend() && $GLOBALS['language']->language != $backend_language) { // check that our desired language exists $languages_available = language_list(); if(array_key_exists($backend_language, $languages_available)) { // remember the original language $GLOBALS['language_original'] = $GLOBALS['language'] // override language to use our preferred language $GLOBALS['language'] = $languages[$backend_language]; } } }
<?php /** * Implementation of hook_init */ function backend_language_restore_init() { if(isset($GLOBALS['original_language'])) { $GLOBALS['language'] = $GLOBALS['original_language']; unset($GLOBALS['original_language']); } }


Comments
Carsten (not verified)
Tue, 05/31/2011 - 07:53
Permalink
Thanks for sharing this idea!
There's a little typo inside backend_language_init() - $GLOBAL['.... is missing the trailing letter S.
marcus
Wed, 08/24/2011 - 10:57
Permalink
Thanks Carsten - fixed!
Add new comment