-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.php
180 lines (168 loc) · 5.5 KB
/
translate.php
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
<?php
/*
the directory in which the dictionaries reside in
standard is 'lang'
*/
$dict_dir=$_SERVER['DOCUMENT_ROOT'].'/lang';
/*
language to use by default
NULL or unset means to use the language provided by the browser
be careful though: if an unsupported language is supplied by the browser all calls to the translating function will return an empty string or even an error
can't be null if $strict is set to true
*/
$default='de';
$use_lang='de';
/*
wheter the translator module should be set up strict, meaning it stays with the default language
if set on false your users might get a better user experience
because the page is presented in their preferred language automatically, if this language is supported
a get parameter called lang overrides the default and browser defined language, if not in strict mode
*/
$strict=false;
$dictionary_setup_done=false;
dictionary_setup($strict);
function dictionary_setup($strict){
global $dict_dir;
global $use_lang;
global $dictionary;
global $dictionary_setup_done;
$dictionary=array();
if(!@file_exists($dict_dir.DIRECTORY_SEPARATOR.'dictionary')){
echo '<!-- Translate: Datei "'.$dict_dir.DIRECTORY_SEPARATOR.'dictionary" nicht gefunden -->';
return;
}
$template=file($dict_dir.DIRECTORY_SEPARATOR.'dictionary',FILE_IGNORE_NEW_LINES);
$dir=scandir($dict_dir);
foreach($dir as $f){
if(!is_file($dict_dir.DIRECTORY_SEPARATOR.$f)||$f=='dictionary'||strpos($f,'.')!==FALSE) continue;
$dictionary_file=file($dict_dir.DIRECTORY_SEPARATOR.$f,FILE_IGNORE_NEW_LINES);
for($i=0;$i<count($dictionary_file);$i++){
$key=$template[$i];
$dictionary[$f][$key]=$dictionary_file[$i];
}
}
if(!$strict){
if(isset($_GET['lang'])){
$lang=strtolower(substr($_GET['lang'],0,2));
if(language_supported($lang)){
$use_lang=$lang;
}
}elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
//selects the users preferred language (from browser settings)
$langs=preg_split("[,;]",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $l){
if($l[0]=='q'&&$l[1]=='='){
// this is a quality code; skip it
continue;
}else if(language_supported($l)){
$use_lang=$l;
break;
}
}
}
}else{
if($use_lang==null){
die('multilang error: strict mode, but no language provided');
}
}
$dictionary_setup_done=true;
}
function language_supported($lang){
global $dictionary;
global $dictionary_setup_done;
global $dict_dir;
if($dictionary_setup_done){
return array_key_exists($lang,$dictionary);
}else{
return file_exists($dict_dir.DIRECTORY_SEPARATOR.$lang);
}
}
/*
the actual translating function
*/
function __($translate,$lang=null){
global $use_lang;
global $dictionary;
if(empty($lang)){
$lang=$use_lang;
}
return $dictionary[$lang][$translate];
}
function language_switcher(){
global $dictionary;
$i=0;
foreach(array_keys($dictionary) as $lang){
$i++;
$language=$dictionary[$lang]['language'];
echo "<a href=\"/$lang/\">$language</a>";
// the first grid column always has to be a spacer
if($i==5){
$i=0;
echo "<span> </span>";
}
}
}
function language_supported_for_chunk($lang,$chunk){
global $dict_dir;
$path=$dict_dir.DIRECTORY_SEPARATOR.'chunk-translations'.DIRECTORY_SEPARATOR.$chunk.DIRECTORY_SEPARATOR.$lang;
return file_exists($path)&&is_file($path);
}
function __chunk($chunk,$lang=null){
global $default;
global $use_lang;
global $dict_dir;
if(empty($lang)){
$lang=$use_lang;
}
while(strlen($lang)>2&&!language_supported_for_chunk($lang,$chunk)){
$lang=substr($lang,0,strrpos($lang,'-'));
}
if(!language_supported_for_chunk($lang,$chunk)){
$lang=$default;
}
$path=$dict_dir.DIRECTORY_SEPARATOR.'chunk-translations'.DIRECTORY_SEPARATOR.$chunk.DIRECTORY_SEPARATOR.$lang;
if(is_file($path)){
return file_get_contents($path);
}else{
// an error occured or default translation is missing
return 'Das hätte nicht passieren dürfen: Es ist ein Fehler im Übersetzungsmodul aufgetreten.';
}
}
function __blog_prev($name,$col_width='12',$lang=null){
global $default;
global $use_lang;
global $dict_dir;
$chunk="blog-${name}-prev";
if(empty($lang)){
$lang=$use_lang;
}
while(strlen($lang)>2&&!language_supported_for_chunk($lang,$chunk)){
$lang=substr($lang,0,strrpos($lang,'-'));
}
if(!language_supported_for_chunk($lang,$chunk)){
$lang=$default;
}
$path=$dict_dir.DIRECTORY_SEPARATOR.'chunk-translations'.DIRECTORY_SEPARATOR.$chunk.DIRECTORY_SEPARATOR.$lang;
if(is_file($path)){
$translation=file($path);
$data=json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/blog/entries.json'),true);
foreach($data as $key=>$value){
if($value['title']==$name){
$data=$data[$key];
break;
}
}
$html="<div class=\"col-md-$col_width\"><div class=\"fh5co-blog animate-box\">";
$html.="<a href=\"/$use_lang/blog/$name\"><img class=\"img-responsive\" src=\"".$data['thumbnail']."\" alt=\"".$translation[0]."\"/></a>";
$html.='<div class="blog-text">';
$html.="<span class=\"posted_on\">".$data['pubDate']."</span>";
$html.="<span class=\"comment\"><i class=\"icon-pencil\"></i> ".$translation[1]."</span>";
$html.="<h3><a href=\"/$use_lang/blog/$name\">".$translation[2]."</a></h3>";
$html.='<p>'.implode(array_slice($translation,3)).'</p>';
$html.='</div></div></div>';
return $html;
}else{
// an error occured or default translation is missing
return 'An error occured. We\'re sorry for the inconvenience.';
}
}