5 * Internationalization (i18n) module
7 * @author Jose A. Reyero, 2004
12 * Module initialization
14 * Get language from path if exists and Initialize i18n system
15 * May do a redirect from home page for not to get wrong versions in cache
19 global $i18n_langpath;
20 // Some functions only to be included when module is enabled
22 $path = _i18n_get_original_path();
23 $i18n_langpath = i18n_get_lang_prefix($path);
24 $lang = _i18n_get_lang();
26 if ($path == '') { // Main page
27 // Check for update script
28 if (isset($_GET['op'])) {
31 if( variable_get('cache',0) && $lang != i18n_default_language() ) {
32 // Redirect to main page in $lang
34 } elseif (variable_get('i18n_frontpage',0)){
35 $_GET['q'] = i18n_frontpage();
38 elseif ($lang == $path) { // When path is only language code
39 $_GET['q'] = variable_get('i18n_frontpage',0) ?
i18n_frontpage() : variable_get('site_frontpage','node');
41 elseif ($i18n_langpath) {
42 //search alias with and without lang and remove lang
43 $_GET['q'] = i18n_get_normal_path($path);
46 // Multi table, for backwards compatibility and experimentation
47 if (variable_get('i18n_multi' , 0)) {
48 _i18n_set_db_prefix(_i18n_get_lang());
51 // If not in bootstrap, include hooks
52 if(!_i18n_is_bootstrap()){
53 include
'modules/i18n/i18n.inc';
59 * Common module functions
63 * Implementation of hook_help().
65 function i18n_help($section = 'admin/help#i18n' ) {
67 case
'admin/help#i18n' :
69 <p>This module provides support for internationalization of Drupal sites:</p>
71 <li>Translation of the user interface for anonymous users (combined with locale)</li>
72 <li>Multi-language for content. Adds a language field for nodes and taxonomy vocabularies and terms</li>
73 <li>Basic translation management</li>
74 <li>Browser language detection</li>
75 <li>Keeps the language setting accross consecutive requests using URL rewriting</li>
76 <li>Provides a block for language selection and two theme functions: <i>i18n_flags</i> and <i>i18n_links</i></li>
77 <li>Support for long locale names</li>
79 <p><small>Module developed by Jose A. Reyero, <a href="http://www.reyero.net">www.reyero.net</a></small></p>' );
81 case
'admin/modules#description' :
82 $output = t('Enables multilingual content. <b>Requires locale module for interface translation</b>' );
89 * Implementation of hook_settings().
91 function i18n_settings() {
92 global $db_prefix_i18n;
93 global $i18n_languages;
95 $config_languages = is_array($i18n_languages) ?
implode(', ',$i18n_languages) : 'Not defined';
96 $output .
= form_radios(t('Supported languages'), 'i18n_supported_langs' , variable_get('i18n_supported_langs', 'locale'),
98 'locale' => t('Defined by the locale module'),
99 'i18n' => t('Defined in the configuration file (%config_languages)', array('%config_languages' => $config_languages))
100 ), t('Where to get the list of supported languages from' ));
101 $output .
= form_radios(t('Browser language detection'), 'i18n_browser', variable_get('i18n_browser', 0), array(t('Disabled'), t('Enabled' )));
102 $output .
= form_select(t('Front page'), 'i18n_frontpage', variable_get('i18n_frontpage', 0), array(t('Default'), t('Language dependent')), t(" If 'language dependent' is selected, default front page will be prepended with language code, i.e. 'en/node'"));
104 $output .= form_textfield(t('Language icons html tag'), 'i18n_flags', variable_get('i18n_flags', '<img class="i18n-flag" src="modules/i18n/flags/*.png" width="16" height="12" alt="Language Flag" />'), 70, 180,
105 t('HTML tag for flags. Asterisk \'*\' is a placeholder for language code. It should be something like <img class="i18n-flag" src="modules/i18n/flags/*.png" width="16" height="12"/>'));
107 $output .
= form_textfield(t('Language icons path'), 'i18n_flags_path', variable_get('i18n_flags_path', 'modules/i18n/flags/*.png'), 70, 180,
108 t('Path for language icons, relative to Drupal installation. \'*\' is a placeholder for language code.'));
109 $output .
= form_textfield(t('Language icons sixe'), 'i18n_flags_size', variable_get('i18n_flags_size', '16x12'), 10, 10,
110 t('Image size for language icons, in the form "width x height".'));
112 $output .
= t('<h2>Multilingual content</h2>' );
113 foreach (node_list() as
$node) {
114 $subform.
=form_checkbox(t($node), 'i18n_node_'.
$node, 1, variable_get('i18n_node_'.
$node, 0));
116 $output .
= form_group(t('Nodes' ), $subform, t('Select node types to be translated.' ));
118 $output .
= t('<h2>Advanced Features</h2>' );
119 $output .
= t('<p>These are intended only for advanced users. Some changes to the database are required. Please, do read the INSTALL.txt and README.txt files before enabling these options</p>' );
122 // Language dependent tables
123 if (is_array($db_prefix_i18n)) {
125 $text = '<strong>'.
t('Current language dependent tables are: ').
implode(', ', array_keys($db_prefix_i18n)).
'</strong>' ;
127 $text = t("Check the module's SETUP.txt file.");
129 $output .
= form_radios(t('Language dependent tables'), 'i18n_multi', variable_get('i18n_multi', 0), array(t('Disabled'), t('Enabled')), t('If enabled, different tables for each language will be used. They must be defined in the configuration file.') .
' ' .
$text);
139 * Get list of supported languages
141 function i18n_supported_languages() {
142 global $i18n_languages;
147 elseif (variable_get('i18n_supported_langs', 'locale') == 'locale') {
148 $languages = _i18n_locale_supported_languages();
151 elseif (is_array($i18n_languages)) {
152 return $languages = $i18n_languages;
160 * Returns default language
162 function i18n_default_language(){
163 return key(i18n_supported_languages());
166 // Get language from browser settings, but only if it is in the $i18n_languages array
167 function i18n_get_browser_lang() {
168 $languages = i18n_supported_languages();
169 $accept=explode(',',array_shift( explode(";",$_SERVER["HTTP_ACCEPT_LANGUAGE"])));
170 foreach ($accept as
$lang) {
171 $lang=substr($lang,0,2);
172 if ( !empty($lang) && array_key_exists($lang,$languages)) {
179 * Get language code from path.
182 * @param $trim true to remove language code from $path
184 function i18n_get_lang_prefix(&$path, $trim = false
) {
185 $maybelang = array_shift(explode('/',$path));
186 $languages = i18n_supported_languages();
187 if(array_key_exists($maybelang, $languages)){
189 $path = trim(substr($path, strlen($maybelang)),'/');
194 if (preg_match("/^\w\w($|\/.*)/", $path)) {
195 return substr($path, 0, 2);
201 * Language dependent front page
203 function i18n_frontpage() {
204 $path = _i18n_get_lang().
'/'.
variable_get('site_frontpage','node');
205 return i18n_get_normal_path($path);
209 * This function is similar to drupal_get_normal_path, but language-aware
210 * Also removes language from path
212 function i18n_get_normal_path($path) {
213 // First, check alias with lang, then without
214 if ($path != ($alias = drupal_lookup_path('alias', $path))) {
216 } elseif(i18n_get_lang_prefix($path, TRUE
)){
217 $alias = drupal_lookup_path('alias', $path);
218 if( $alias && $path != $alias) {
222 // We only get here when no alias is defined, with or without lang
227 * Gets language, checking in order:
231 * 3. Browser language
232 * 4. Default language
235 function _i18n_get_lang() {
236 global $user, $i18n_langpath;
239 //see if the language is already set.
244 $languages = i18n_supported_languages();
246 if ($i18n_langpath && array_key_exists($i18n_langpath,$languages)) {
247 $i18n_lang = $i18n_langpath;
249 elseif ($user->uid
&& $user->language
&& array_key_exists($user->language
,$languages)) {
250 $i18n_lang = $user->language
;
252 elseif (variable_get("i18n_browser",0) && $lang=i18n_get_browser_lang()) {
256 $i18n_lang=key($languages);
263 * Check whether we are in bootstrap mode
265 function _i18n_is_bootstrap(){
266 return !function_exists('drupal_get_headers');
270 * Sets db_prefix to given language
272 function _i18n_set_db_prefix($lang) {
273 global $db_prefix, $db_prefix_i18n;
274 if (is_array($db_prefix_i18n)) {
275 $db_prefix = array_merge($db_prefix, str_replace('**', $lang, $db_prefix_i18n));
280 * To get the original path.
281 * Cannot use $_GET["q"] cause it may have been already changed
283 function _i18n_get_original_path() {
284 return isset($_REQUEST["q"]) ?
trim($_REQUEST["q"],"/") : '';
289 * Returns list of enabled languages from locale module
291 * * Some code borrowed from locale module
293 function _i18n_locale_supported_languages() {
295 $result = db_query('SELECT locale, name FROM {locales_meta} WHERE enabled = 1 ORDER BY isdefault DESC, name ASC');
296 while ($row = db_fetch_object($result)) {
297 $enabled[$row->locale
] = $row->name
;
303 * Emulates drupal_goto, it may not be loaded yet
305 function _i18n_goto($lang){
306 if(!function_exists('drupal_goto')){
307 require_once
'./includes/common.inc';