forked from josereyero/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.module
238 lines (221 loc) · 7.78 KB
/
i18n.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
// $Id: i18n.module,v 1.41.2.49.2.1 2010/09/14 11:30:30 jareyero Exp $
/**
* @file
* Internationalization (i18n) module.
*
* This module extends multilingual support being the base module for the i18n package.
* - Multilingual variables
* - Extended languages for nodes
* - Extended language API
*
* @author Jose A. Reyero, 2004
*/
// All multilingual options disabled
define('I18N_LANGUAGE_DISABLED', 0);
// Language list will include all enabled languages
define('I18N_LANGUAGE_ENABLED', 1);
// Language list will include also disabled languages
define('I18N_LANGUAGE_EXTENDED', 4);
// Disabled languages will be hidden when possible
define('I18N_LANGUAGE_HIDDEN', 8);
// All defined languages will be allowed but hidden when possible
define('I18N_LANGUAGE_EXTENDED_NOT_DISPLAYED', I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN);
// No multilingual options
define('I18N_MODE_NONE', 0);
// Localizable object. Run through the localization system
define('I18N_MODE_LOCALIZE', 1);
// Predefined language for this object and all related ones.
define('I18N_MODE_LANGUAGE', 2);
// Multilingual objects, translatable but not localizable.
define('I18N_MODE_TRANSLATE', 4);
/**
* Implements hook_boot()
*/
function i18n_boot() {
// Just make sure the module is loaded for boot and the API is available.
}
/**
* Get global language object, make sure it is initialized
*
* @param $language
* Language code or language object to convert to valid language object
*/
function i18n_language($language = NULL) {
if ($language) {
if (is_object($language)) {
return $language;
}
else {
$list = language_list();
return isset($list[$language]) ? $list[$language] : i18n_language();
}
}
else {
if (empty($GLOBALS['language'])) {
// We don't have language yet, initialize the language system and retry
drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
}
return $GLOBALS['language'];
}
}
/**
* Get full language list
*
* @todo See about creating a permission for seeing disabled languages
*/
function i18n_language_list($field = 'name', $mode = NULL) {
$mode = isset($mode) ? $mode : variable_get('i18n_language_list', I18N_LANGUAGE_ENABLED);
return locale_language_list($field, I18N_LANGUAGE_EXTENDED & $mode);
}
/**
* Get language name for any defined (enabled or not) language
*
* @see locale_language_list()
*/
function i18n_language_name($lang) {
$list = &drupal_static(__FUNCTION__);
if (!isset($list)) {
$list = locale_language_list('name', TRUE);
}
if (!$lang || $lang === LANGUAGE_NONE) {
return t('Undefined');
}
elseif (isset($list[$lang])) {
return $list[$lang];
}
else {
return t('Unknown');
}
}
/**
* Get valid language code for current page
*/
function i18n_langcode($langcode = NULL) {
return $langcode && $langcode !== LANGUAGE_NONE ? $langcode : i18n_language()->language;
}
/**
* Implements hook_help().
*/
function i18n_help($path = 'admin/help#i18n', $arg) {
switch ($path) {
case 'admin/help#i18n' :
$output = '<p>'. t('This module improves support for multilingual content in Drupal sites:') .'</p>';
$output .= '<ul>';
$output .= '<li>'. t('Shows content depending on page language.') .'</li>';
$output .= '<li>'. t('Handles multilingual variables.') .'</li>';
$output .= '<li>'. t('Extended language option for chosen content types. For these content types transations will be allowed for all defined languages, not only for enabled ones.') .'</li>';
$output .= '<li>'. t('Provides a block for language selection and two theme functions: <em>i18n_flags</em> and <em>i18n_links</em>.') .'</li>';
$output .= '</ul>';
$output .= '<p>'. t('This is the base module for several others adding different features:') .'</p>';
$output .= '<ul>';
$output .= '<li>'. t('Multilingual menu items.') .'</li>';
$output .= '<li>'. t('Multilingual taxonomy adds a language field for taxonomy vocabularies and terms.') .'</li>';
$output .= '</ul>';
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) .'</p>';
return $output;
case 'admin/config/language/i18n':
$output = '<ul>';
$output .= '<li>'. t('To enable multilingual support for specific content types go to <a href="@configure_content_types">configure content types</a>.', array('@configure_content_types' => url('admin/structure/types'))) .'</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_menu().
*/
function i18n_menu() {
$items['admin/config/regional/i18n'] = array(
'title' => 'Multilingual settings',
'description' => 'Configure extended options for multilingual content and translations.',
'page callback' => 'drupal_get_form',
'page arguments' => array('i18n_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'weight' => 10,
);
$items['admin/config/regional/i18n/configure'] = array(
'title' => 'Multilingual system',
'description' => 'Configure extended options for multilingual content and translations.',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('i18n_admin_settings'),
//'access arguments' => array('administer site configuration'),
'file' => 'i18n.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Simple i18n API
*/
/**
* Switch select Mode on off if enabled
*/
function i18n_select($value = NULL) {
static $mode;
if (isset($value)) {
$mode = $value;
}
return $mode;
}
/**
* Get language properties.
*
* @param $code
* Language code.
* @param $property
* It may be 'name', 'native', 'ltr'...
*/
function i18n_language_property($code, $property) {
$languages = language_list();
return isset($languages[$code]->$property) ? $languages[$code]->$property : NULL;
}
/**
* Implements hook_preprocess_html().
*/
function i18n_preprocess_html(&$variables) {
global $language;
$variables['classes_array'][] = 'i18n-' . $language->language;
}
/**
* Translate or update user defined string. Entry point for i18n_string API if enabled.
*
* @param $name
* Textgroup and context glued with ':'.
* @param $default
* String in default language. Default language may or may not be English.
* @param $options
* An associative array of additional options, with the following keys:
* - 'langcode' (defaults to the current language) The language code to translate to a language other than what is used to display the page.
* - 'filter' Formatting or filtering callback to apply to the translated string only
* - 'callback' Callback to apply to the result (both to translated or untranslated string
* - 'update' (defaults to FALSE) Whether to update source table
* - 'translate' (defaults to TRUE) Whether to return a translation
*
* @return $string
* Translated string, $string if not found
*/
function i18n_string($name, $string, $options = array()) {
$options += array('translate' => TRUE, 'update' => FALSE);
if ($options['update']) {
$result = function_exists('i18n_string_update') ? i18n_string_update($name, $string, $options) : FALSE;
}
if ($options['translate']) {
$result = function_exists('i18n_string_translate') ? i18n_string_translate($name, $string, $options) : $string;
}
return $result;
}
/**
* Get language from context.
*
* Depending on the page content we may need to use a different language for some operations.
*/
function i18n_context_language() {
// Get language from the first module that provides it
foreach (module_implements('i18n_context_language') as $module) {
if ($language = module_invoke($module, 'i18n_context_language')) {
return $language;
}
}
return i18n_language();
}