-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translator.php
140 lines (117 loc) · 3.96 KB
/
Translator.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
<?php
/**
* @version 0.1
* @author Sabino Papagna <[email protected]>
* @link https://github.com/bagbyte/translator-php
* @copyright Copyright 2015 Sabino Papagna
* @license GNU General Public License
* @package Translator-PHP
*/
class Translator {
const KEY_DELIMITER = '.';
const DEFAULT_LANGUAGE = 'en';
const LOCALE_PATH = './translations';
const FOLDER_SEPARATOR = '/';
const TRANSLATION_FILE_EXTENSION = '.yml';
const SESSION_LANGUAGE_PREFERENCE = 'translator_language_preference';
const LOCALE_GET_PARAMETER = 'locale';
private $language;
private $translations = array();
private $contexts = array();
private $supportedLanguages = array('en','it');
public function __construct($contexts = null) {
if (!isset($_SESSION)) {
session_start();
}
if (isset($_GET[self::LOCALE_GET_PARAMETER]) && !empty($_GET[self::LOCALE_GET_PARAMETER])) {
$this->language = $_GET[self::LOCALE_GET_PARAMETER];
}
elseif (isset($_SESSION[self::SESSION_LANGUAGE_PREFERENCE])) {
$this->language = $_SESSION[self::SESSION_LANGUAGE_PREFERENCE];
}
elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$this->language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
else {
$this->language = self::DEFAULT_LANGUAGE;
}
$_SESSION[self::SESSION_LANGUAGE_PREFERENCE] = $this->language;
if ($contexts != null) {
$this->contexts = $contexts;
$this->load($contexts);
}
}
public function load($contexts = array()) {
if (!is_array($contexts))
return;
if (!is_array($this->translations))
$this->translations = array();
foreach($contexts as $ctx) {
if (in_array("", $this->contexts))
continue;
$this->contexts[] = $ctx;
$localeFile = $this->getFilePath($this->language, $ctx);
$defaultLocaleFile = $this->getFilePath(self::DEFAULT_LANGUAGE, $ctx);
if (file_exists($localeFile))
$this->translations[$ctx] = Spyc::YAMLLoad($localeFile);
elseif (file_exists($defaultLocaleFile))
$this->translations[$ctx] = Spyc::YAMLLoad($defaultLocaleFile);
}
}
private function translation($context, $key, $print = true) {
if ((strlen($context) < 1) || (strlen($key) < 1) || (count($this->translations) < 1)) {
if ($print) {
echo $key;
return;
}
else
return $key;
}
if (!in_array($context, $this->contexts) || !array_key_exists($context, $this->translations)) {
if ($print) {
echo $key;
return;
}
else
return $key;
}
$output = $this->getValueFromKey($this->translations[$context], $key);
if ($print)
echo (($output == null) ? $key : $output);
else
return (($output == null) ? $key : $output);
}
public function getTranslation($context, $key) {
return $this->translation($context, $key, false);
}
public function translate($context, $key) {
return $this->translation($context, $key, true);
}
public function getCurrentLanguage() {
return $_SESSION[self::SESSION_LANGUAGE_PREFERENCE];
}
public function getAvailableLanguages() {
return $this->supportedLanguages;
}
private function getFilePath($locale, $context) {
return self::LOCALE_PATH . self::FOLDER_SEPARATOR . $locale . self::FOLDER_SEPARATOR . $context . self::TRANSLATION_FILE_EXTENSION;
}
private function getValueFromKey($array, $key) {
if (strlen($key) < 1)
return null;
$keys = explode(self::KEY_DELIMITER, $key);
if (count($keys) == 1) {
if (array_key_exists($key, $array))
return $array[$key];
return null;
}
else {
$currentKey = $keys[0];
if (!array_key_exists($currentKey, $array))
return null;
array_shift($keys);
return $this->getValueFromKey($array[$currentKey], implode(self::KEY_DELIMITER, $keys));
}
}
}
?>