1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Arthur Schiwon <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OC\L10N; |
26
|
|
|
|
27
|
|
|
use OCP\IConfig; |
28
|
|
|
use OCP\IUser; |
29
|
|
|
use OCP\L10N\ILanguageIterator; |
30
|
|
|
|
31
|
|
|
class LanguageIterator implements ILanguageIterator { |
32
|
|
|
private $i = 0; |
33
|
|
|
/** @var IConfig */ |
34
|
|
|
private $config; |
35
|
|
|
/** @var IUser */ |
36
|
|
|
private $user; |
37
|
|
|
|
38
|
|
|
public function __construct(IUser $user, IConfig $config) { |
39
|
|
|
$this->config = $config; |
40
|
|
|
$this->user = $user; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Rewind the Iterator to the first element |
45
|
|
|
*/ |
46
|
|
|
public function rewind() { |
47
|
|
|
$this->i = 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the current element |
52
|
|
|
* |
53
|
|
|
* @since 14.0.0 |
54
|
|
|
*/ |
55
|
|
|
public function current(): string { |
56
|
|
|
switch($this->i) { |
57
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
58
|
|
|
case 0: |
|
|
|
|
59
|
|
|
$forcedLang = $this->config->getSystemValue('force_language', false); |
60
|
|
|
if(is_string($forcedLang)) { |
61
|
|
|
return $forcedLang; |
62
|
|
|
} |
63
|
|
|
$this->next(); |
64
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
65
|
|
View Code Duplication |
case 1: |
|
|
|
|
66
|
|
|
$forcedLang = $this->config->getSystemValue('force_language', false); |
67
|
|
|
if(is_string($forcedLang) |
68
|
|
|
&& ($truncated = $this->getTruncatedLanguage($forcedLang)) !== $forcedLang |
69
|
|
|
) { |
70
|
|
|
return $truncated; |
71
|
|
|
} |
72
|
|
|
$this->next(); |
73
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
74
|
|
|
case 2: |
|
|
|
|
75
|
|
|
$userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null); |
76
|
|
|
if(is_string($userLang)) { |
77
|
|
|
return $userLang; |
78
|
|
|
} |
79
|
|
|
$this->next(); |
80
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
81
|
|
View Code Duplication |
case 3: |
|
|
|
|
82
|
|
|
$userLang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', null); |
83
|
|
|
if(is_string($userLang) |
84
|
|
|
&& ($truncated = $this->getTruncatedLanguage($userLang)) !== $userLang |
85
|
|
|
) { |
86
|
|
|
return $truncated; |
87
|
|
|
} |
88
|
|
|
$this->next(); |
89
|
|
|
case 4: |
90
|
|
|
return $this->config->getSystemValue('default_language', 'en'); |
91
|
|
|
/** @noinspection PhpMissingBreakStatementInspection */ |
92
|
|
View Code Duplication |
case 5: |
|
|
|
|
93
|
|
|
$defaultLang = $this->config->getSystemValue('default_language', 'en'); |
94
|
|
|
if(($truncated = $this->getTruncatedLanguage($defaultLang)) !== $defaultLang) { |
95
|
|
|
return $truncated; |
96
|
|
|
} |
97
|
|
|
$this->next(); |
98
|
|
|
default: |
99
|
|
|
return 'en'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Move forward to next element |
105
|
|
|
* |
106
|
|
|
* @since 14.0.0 |
107
|
|
|
*/ |
108
|
|
|
public function next() { |
109
|
|
|
++$this->i; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return the key of the current element |
114
|
|
|
* |
115
|
|
|
* @since 14.0.0 |
116
|
|
|
*/ |
117
|
|
|
public function key(): int { |
118
|
|
|
return $this->i; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Checks if current position is valid |
123
|
|
|
* |
124
|
|
|
* @since 14.0.0 |
125
|
|
|
*/ |
126
|
|
|
public function valid(): bool { |
127
|
|
|
return $this->i <= 6; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getTruncatedLanguage(string $lang):string { |
131
|
|
|
$pos = strpos($lang, '_'); |
132
|
|
|
if($pos !== false) { |
133
|
|
|
$lang = substr($lang, 0, $pos); |
134
|
|
|
} |
135
|
|
|
return $lang; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|