forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.php
More file actions
executable file
·184 lines (171 loc) · 5.09 KB
/
translator.php
File metadata and controls
executable file
·184 lines (171 loc) · 5.09 KB
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
<?php
/**
* Translator
*
* PHP version 5
*
* Copyright (C) Ere Maijala 2017.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
require_once 'sessionfuncs.php';
/**
* Translator
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
class Translator
{
/**
* Storage for translations
*
* @var array
*/
public static $translations = [];
/**
* Any active languages for domains
*
* @var array
*/
public static $activeLanguages = [];
/**
* Translate a string
*
* @param string $str String to translate
* @param array $placeholders Any key/value pairs to replace in the translation
* @param string $default Optional default value if translation doesn't
* exist
*
* @return string
*/
public static function translate($str, $placeholders = [], $default = null)
{
$domain = 'default';
$p = strpos($str, '::');
if (false !== $p) {
$domain = substr($str, 0, $p);
$str = substr($str, $p + 2);
}
if (empty($str)) {
return $str;
}
if (!isset(self::$translations[$domain])) {
self::loadTranslations($domain);
}
if (isset(self::$translations[$domain][$str])) {
$str = self::$translations[$domain][$str];
} elseif (null !== $default) {
$str = $default;
}
if ($placeholders) {
$str = str_replace(
array_keys($placeholders), array_values($placeholders), $str
);
}
return $str;
}
/**
* Get active language for a domain
*
* @param string $domain Translation domain
*
* @return string
*/
public static function getActiveLanguage($domain)
{
return isset(self::$activeLanguages[$domain])
? self::$activeLanguages[$domain]
: '';
}
/**
* Set active language for a domain
*
* @param string $domain Translation domain
* @param string $language Language
*
* @return void
*/
public static function setActiveLanguage($domain, $language)
{
if ('en' === $language) {
$language = 'en-US';
} elseif ('fi' === $language) {
$language = 'fi-FI';
} elseif ('sv' === $language) {
$language = 'sv-FI';
}
self::$activeLanguages[$domain] = $language;
unset(self::$translations[$domain]);
}
/**
* Load translations for a domain
*
* @param string $domain Translation domain
*
* @return void
*/
protected static function loadTranslations($domain)
{
$file = 'fi-FI';
if (!session_id()) {
session_start();
}
if (!empty(self::$activeLanguages[$domain])) {
$file = self::$activeLanguages[$domain];
} elseif ('default' !== $domain
&& !empty(self::$activeLanguages['non-default'])
) {
$file = self::$activeLanguages['non-default'];
} elseif (isset($_SESSION['sesLANG'])) {
$file = $_SESSION['sesLANG'];
} elseif (defined('_UI_LANGUAGE_')) {
$file = _UI_LANGUAGE_;
}
if ('default' !== $domain) {
$file = $domain . '_' . $file;
}
if (!file_exists("lang/$file.ini")) {
if ('default' !== $domain) {
$file = $domain . '_fi-FI';
} else {
$file = 'fi-FI';
}
}
self::$translations[$domain] = file_exists("lang/$file.ini")
? parse_ini_file("lang/$file.ini") : [];
if (file_exists("lang/$file.local.ini")) {
self::$translations[$domain] = array_merge(
self::$translations[$domain],
parse_ini_file("lang/$file.local.ini")
);
}
if (_CHARSET_ != 'UTF-8') {
foreach (self::$translations[$domain] as $key => &$tr) {
if (is_string($tr)) {
$tr = utf8_decode($tr);
}
}
}
}
}