/
lang.php
135 lines (117 loc) · 3.18 KB
/
lang.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
<?php
require_once PUN_ROOT.'modules/gettext/gettext.php';
require_once PUN_ROOT.'modules/cache/cache.php';
class Flux_Lang
{
/**
* An array of translated strings
*
* @var array
*/
protected $translationTable = array();
/**
* The directory where language packs are located
*
* @var string
*/
protected $langDir = 'lang/';
/**
* The default language
*
* @var string
*/
protected $defaultLang = 'en';
/**
* The language to use
*
* @var string
*/
protected $lang = 'en';
/**
* Set the directory where language packs are located
*
* @param string $dir
* @return void
*/
public function setLanguageDirectory($dir)
{
// Remove any trailing slashes
$this->langDir = rtrim($dir, '/');
}
/**
* Set the default language
*
* @param array $lang
* @return void
*/
public function setDefaultLanguage($lang)
{
if (file_exists($this->langDir.'/'.$lang))
$this->defaultLang = $lang;
else
throw new Exception('Default language pack "'.$lang.'" does not exist.');
}
/**
* Set the language to use
*
* @param array $lang
* @return void
*/
public function setLanguage($lang)
{
if (file_exists($this->langDir.'/'.$lang))
$this->lang = $lang;
else
throw new Exception('Language pack "'.$lang.'" does not exist.');
}
/**
* Load a new file and add its translations to our elements
*
* Incomplete language packs will fall back to the default language
* for missing strings.
*
* This only loads Gettext files if cached files cannot be found.
*
* @param array $resource
* @return void
*/
public function load($resource)
{
$default_filename = $this->langDir.'/'.$this->defaultLang.'/'.$resource.'.mo';
$filename = $this->langDir.'/'.$this->lang.'/'.$resource.'.mo';
$cache = Cache::load('file', array('dir' => FORUM_CACHE_DIR), 'varexport');
// TODO: Handle Cache config globally. How?
// TODO: Slash allowed? - I'd rather use that than an underscore
$trans_cache = $cache->get($this->lang.'_'.$resource);
if ($trans_cache === Cache::NOT_FOUND)
{
$trans_cache = Gettext::parse($filename);
// If this is not the default language, load that, too
if ($this->defaultLang != $this->lang)
{
$def_trans_cache = $cache->get($this->defaultLang.'_'.$resource);
if ($def_trans_cache === Cache::NOT_FOUND)
{
$def_trans_cache = Gettext::parse($default_filename);
$cache->set($this->defaultLang.'_'.$resource, $def_trans_cache);
}
// TODO: How could we automatically regenerate these cache files when necessary? (E.g. imagine the default language files being replaced during a new release, the custom translations haven't caught up yet. How to handle that? etc.)
// Now use default language values as fallback
$trans_cache = array_merge($def_trans_cache, $trans_cache);
}
$cache->set($this->lang.'_'.$resource, $trans_cache);
}
// Store the loaded values for usage
$this->translationTable = array_merge($this->translationTable, $trans_cache);
}
/**
* Return the translation of the given string
*
* @param string $str
* @return string
*/
public function t($str)
{
return isset($this->translationTable[$str]) ? $this->translationTable[$str][0] : $str;
}
}