-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLang.php
More file actions
134 lines (107 loc) · 2.88 KB
/
Lang.php
File metadata and controls
134 lines (107 loc) · 2.88 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
<?php
namespace Undercloud;
use Undercloud\Lang\LangIterator;
use Undercloud\Lang\Storage;
use Undercloud\Lang\TranslateNotFound;
use Undercloud\Lang\Parser\DefaultParser;
require_once __DIR__ . '/Storage.php';
require_once __DIR__ . '/LangIterator.php';
require_once __DIR__ . '/TranslateNotFound.php';
require_once __DIR__ . '/Parser/AbstractParser.php';
require_once __DIR__ . '/Parser/DefaultParser.php';
require_once __DIR__ . '/Parser/JsonParser.php';
class Lang
{
private $options = array();
private $iterator;
private $storage;
public function __construct(array $options = array())
{
if (false == isset($options['accept'])) {
$options['accept'] = $this->getLangsHttp();
}
if (false == isset($options['avail'])) {
$options['avail'] = array();
}
$options['lang'] = array_values(
array_intersect(
$options['accept'],
$options['avail']
)
);
$this->options = $options;
$this->iterator = new LangIterator($options['lang']);
$this->storage = new Storage();
if (isset($options['root'])) {
$this->storage->setRoot($options['root']);
}
$this->storage->setParser(
isset($options['parser'])
? $options['parser']
: (new DefaultParser)
);
}
public function getLangsHttp(array $default = array('en'))
{
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$langs = preg_replace('~;q=[0-9].[0-9]~i', '', $langs);
$langs = strtolower($langs);
$langs = explode(',', $langs);
if (!$langs) {
return $default;
}
return $langs;
} else {
return $default;
}
}
public function getPrimaryLocale()
{
return reset($this->options['lang']);
}
public function getFallBackLocale()
{
return end($this->options['lang']);
}
public function assign($message, array $placeholders = array())
{
$index = 0;
foreach ($placeholders as $key => $value) {
$index++;
if (false == is_scalar($value)) {
if (is_array($value)) {
$placeholders[$key] = implode(', ', array_filter($value, function($item) {
return is_scalar($item);
}));
} else {
$placeholders[$key] = null;
}
}
$message = str_replace(':' . $key, '%' . $index . '$s', $message);
}
return vsprintf($message, array_values($placeholders));
}
public function __invoke($message, array $assoc = array())
{
return $this->get($message, $assoc);
}
public function get($message, array $assoc = array())
{
$current = $this->iterator->current();
$value = $this->storage->load($current, $message);
if ($value instanceof TranslateNotFound) {
$this->iterator->next();
if ($this->iterator->valid()) {
return $this->get($message, $assoc);
} else {
return;
}
} else {
$this->iterator->rewind();
$value = $this->assign($value, $assoc);
return $value;
}
}
}
?>