summaryrefslogtreecommitdiffstats
path: root/i18n.module
blob: 4894474d6ab887bcbf94429a48fe147a2cfcdb55 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
 * Internationalization (i18n) module
 *
 * @author Jose A. Reyero, 2004
 */

/**
 *	Common module functions
 */

// This runs after user has ben initialized and path may be already translated

function i18n_init(){
	global $i18n_langpath;
	
	// Quick fix, check if called from bootstrap.inc
	if(!function_exists("drupal_get_path_map")) return 0;
	
	if($lang=i18n_get_lang()){
		i18n_set_lang($lang);
	}else{
  	$lang= i18n_get_default_lang();
	}
	
  if(variable_get("i18n_interface",0)) i18n_set_default_lang($lang);
  
  if(variable_get("i18n_content",0)){
  	$original=i18n_get_original_path();
  	if(!$i18n_langpath && $trpath=i18n_get_normal_path("$lang/$original")){
  		$_GET["q"]=$trpath;
  	}elseif(!$original || $i18n_langpath==$original){
			$_GET["q"]=drupal_get_normal_path(i18n_front_page($lang));
  	}
  }
}
function i18n_front_page($lang){
	return variable_get("i18n_front_page",0) ? $lang.'/'.variable_get('site_frontpage','node') : variable_get('site_frontpage','node');
}
function i18n_help($section = "admin/help#i18n") {
  switch ($section) {
  	case "admin/help#i18n":
  		$output = t("
  			<p>This module provides support for internationalization of Drupal sites in various ways:</p>
  			<ul><li>Translation of the user interface for registered and anonymous users</li>
  			<li>Multi-language for content, combined with url aliasing. For this feature to be used, you must enable it in the module configuration and then use url aliasing to keep pages in various languages. I.e. 'en/mypage', 'es/mypage' should be English and Spanish versions of the same page</li>
  			<li>Detection of the brower language</li>
  			<li>Keeps the language settings accross consecutive requests, using a number of methods: URL rewriting, sessions, cookies</li>
  			<li>Provides a block for language selection and two theme functions: <i>i18n_flags</i> and <i>i18n_links</i></li></ul>
  			<p>For url rewriting you need to have the file <b>i18n.inc</b> in the <i>includes</i> folder and add the following line to your configuration file:</p>
  			<pre>
  			include 'includes/i18n.inc';
  			</pre>
  			<p>You can try different settings to have only content translation, interface translation or both.</p>
  			<p><small>Module developed by <a href=\"http://freelance.reyero.net\">freelance.reyero.net</a></small></p>");
  		break;
    case "admin/system/modules#description":
      $output = t("Supports site internationalization (i18n).");
      break;
  }
  return $output;
}

function i18n_settings(){
	$output .= form_radios(t("Interface translation"),"i18n_interface",variable_get("i18n_interface",0),array(t("Disabled"),t("Enabled")),t("If disabled, uses Drupal's default. If enabled, translates the interface to selected language"));
	$output .= form_radios(t("Content translation"),"i18n_content",variable_get("i18n_content",0),array(t("Disabled"),t("Enabled")),t("If enabled, prepends language code to url and searches for translated content"));
	$output .= form_select(t("Front page"),"i18n_front_page",variable_get("i18n_front_page",0),array(t("Default"),t("Language dependant")),t("If language dependant and <i>Content translation</i> is enabled, default front page will be prepended with language code, i.e. 'en/node'"));
	$output .=form_radios(t("Keep Language"),"i18n_keep",variable_get("i18n_keep",'url'),
	array(	""=>t("Disabled"),
					"url" => t("URL rewriting"),
					"session" => t("Session"),
					"cookie" => t("Cookie")
	),t("A language code independent of user's language will be kept accross requests using the selected method"));
	$output .= form_radios(t("Detect browser language"),"i18n_browser",variable_get("i18n_browser",0),array(t("Disabled"),t("Enabled")));
	$output .= form_textfield(t("Flags directory"),"i18n_flags",variable_get("i18n_flags","misc/flags/*.gif"),70,180,t("Path for flags. Asterisk '*' is a placeholder for language code. This is only needed when you want a language selector block"));

	return $output;
}

function i18n_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Languages');
  }
  else {
  	$blocks['subject']=t('Languages');
  	$content = "<table><tr><td>";
  	$content .=  theme("i18n_links",1,1,"</td><td>","</td></tr><tr><td>");
  	$content .= "</td></tr></table>";
    $blocks['content'] =$content;
  }

  return $blocks;
}


/**
 * i18n api
 */
 
if(!function_exists("i18n_get_lang_prefix")){
function i18n_get_lang_prefix($path){
	global $languages;
	$split=split("/",$path);
	$maybelang=$split[0];
	if($languages[$maybelang]){
		return $maybelang;
	}
}
}
 
function i18n_get_lang(){
	global $i18n_lang;
	if($i18n_lang){
		return $i18n_lang;
	} elseif($lang=$_GET['i18n_lang']) {
		return $i18n_lang=$lang;
	} else {
		switch(variable_get('i18n_keep','')){
			case 'url': // URL
				$lang= i18n_get_lang_prefix(i18n_get_original_path());
				break;
			case 'session': // Session
				session_start();
				$lang = $_SESSION['i18n_lang'];
				break;
			case 'cookie': // Cookie
				$lang=$_COOKIE['i18n_lang'];
				break;
			default:
				$lang=$_GET['i18n_lang'];
		}
	}
	return $i18n_lang=$lang;
}

function i18n_set_lang($lang){
	global $i18n_lang;
	global $i18n_langpath;
	$i18n_lang=$lang;
	switch(variable_get('i18n_keep','')){
		case 'url': // URL
		case 'query': // Query string
			$i18n_langpath=$lang;
			break;
		case 'session': // Session
			session_start();
			$_SESSION["i18n_lang"]=$lang;
			break;
		case 'cookie': // Cookie
			setcookie('i18n_lang',$lang,NULL,'/');
			break;
	}
}

function i18n_get_default_lang(){
	global $user;
	global $languages;
	if(!$lang=$user->language){
  	if(variable_get('i18n_browser',0) && $lang=i18n_get_browser_lang()){
  		i18n_set_default_lang($lang);
  	} else {
  		$lang=key($languages);
  		reset($languages);
  	}
	}
	return $lang;	
}

// Set language as default rearranging $languages array
function i18n_set_default_lang($lang){
	global $languages;
	global $i18n_lang;
	$i18n_lang=$lang;
	if(variable_get("i18n_interface",0)){
		$deflang=key($languages);
		if($lang != $deflang){ 
			// Reorder $languages to set $lang as the first element...
			$languages=array_merge(array($lang => $languages[$lang]),$languages);
		} else { //Language is the default
				reset($languages);
		}
	}
}

// Function based on common.inc::drupal_get_normal_path, but returns nothing if mapping not found
function i18n_get_normal_path($path) {
  if (($map = drupal_get_path_map()) && isset($map[$path])) {
    return $map[$path];
  } 
}

// To get the original path. Cannot use $_GET["q"] cause it may have been already changed
function i18n_get_original_path(){
  $path=$_REQUEST["q"];
	return trim($path,"/");
}

// Get language from browser settings, but only if it is in the $languages array
function i18n_get_browser_lang(){
	global $languages;
	$accept=explode(",",array_shift( explode(";",$_SERVER["HTTP_ACCEPT_LANGUAGE"])));
	foreach($accept as $lang){
		$lang=substr($lang,0,2);
		if($languages[$lang]) return $lang;
	}
}

/**
 *	Theme functions
 */
 
/**
	* Returns language links with optional flags
	*
	* @param $flags an integer, 1 to use language flags
	* @param $names an integer, 1 to use language names
	* @param $delim1 delimiter to place between language name and flag
	* @param $delim2 delimiter to place between different languages
	*
	* @return a string containing the @a links output.
	*/
	
function theme_i18n_links($flags=1,$names=1,$delim1=" ",$delim2=" "){
	global $languages;
	global $i18n_lang;

	foreach($languages as $lang => $langname){
		$name= $names ? t($langname): ""; // Should be localized??
		$flag= $flags ? _i18n_flag($lang) :"";
		if($lang==$i18n_lang){
			$links[] = "<strong>$name</strong>$delim1$flag";
		}else {
			$links[] = i18n_l($name,$lang).$delim1.i18n_l($flag,$lang);
		}
	}
	$output =implode($delim2,$links);
	return $output;	
}

function theme_i18n_flags(){
	return theme_i18n_links(1,0);
}

function _i18n_flag($lang,$attribs=array()){
	if($path=variable_get("i18n_flags","misc/flags/*.gif")){
		$flag=str_replace("*",$lang,$path);
		return "<img class=\"i18n\" src=\"$flag\"".drupal_attributes($attribs)." />";
	}
}

// Creates links for different languages
function i18n_l($text, $lang , $url="",$attributes=array(),$query=NULL) {
	global $i18n_langpath;
	// If !url get from original request
	if(!$url) $url=i18n_get_original_path();
		// If url has lang_prefix, remove it
	if(i18n_get_lang_prefix($url)) $url=substr($url,3);
	
	if(variable_get('i18n_keep','')=='url'){
		$url="$lang/$url";
	} else {
		if(variable_get('i18n_content',0)) $url="$lang/$url";
		$query= $query ? $query.'&i18n_lang='.$lang : "i18n_lang=$lang";
	}
	// Save $i18n_langpath, and set to null to avoid url_rewriting
	$tmp=$i18n_langpath;
	$i18n_langpath="";
	$result= l($text,$url,$attributes,$query);
	// Restore $i18n_langpath
	$i18n_langpath=$tmp;	
	return $result;
}

?>