Completed
Push — master ( 8a6036...5a9909 )
by
unknown
11:22
created

LocaleHelper::getLanguageNameByCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Helper;
23
24
use OCP\L10N\IFactory;
25
26
class LocaleHelper {
27
	/**
28
	 * @var string[]
29
	 */
30
	private $commonLanguages = [
31
		'en',
32
		'es',
33
		'fr',
34
		'de',
35
		'de_DE',
36
		'ja',
37
		'ar',
38
		'ru',
39
		'nl',
40
		'it',
41
		'pt_BR',
42
		'pt_PT',
43
		'da',
44
		'fi_FI',
45
		'nb_NO',
46
		'sv',
47
		'tr',
48
		'zh_CN',
49
		'ko'
50
	];
51
52
	/**
53
	 * @var string[]
54
	 */
55
	private $languageCodes = [
56
		'el' => 'Ελληνικά',
57
		'en' => 'English',
58
		'fa' => 'فارسى',
59
		'fi_FI' => 'Suomi',
60
		'hi' => 'हिन्दी',
61
		'id' => 'Bahasa Indonesia',
62
		'lb' => 'Lëtzebuergesch',
63
		'ms_MY' => 'Bahasa Melayu',
64
		'nb_NO' => 'Norwegian Bokmål',
65
		'pt_BR' => 'Português brasileiro',
66
		'pt_PT' => 'Português',
67
		'ro' => 'română',
68
		'sr@latin' => 'Srpski',
69
		'sv' => 'Svenska',
70
		'hu_HU' => 'Magyar',
71
		'hr' => 'Hrvatski',
72
		'ar' => 'العربية',
73
		'lv' => 'Latviešu',
74
		'mk' => 'македонски',
75
		'uk' => 'Українська',
76
		'vi' => 'Tiếng Việt',
77
		'zh_TW' => '正體中文(臺灣)',
78
		'af_ZA' => 'Afrikaans',
79
		'bn_BD' => 'Bengali',
80
		'ta_LK' => 'தமிழ்',
81
		'zh_HK' => '繁體中文(香港)',
82
		'is' => 'Icelandic',
83
		'ka_GE' => 'Georgian for Georgia',
84
		'ku_IQ' => 'Kurdish Iraq',
85
		'si_LK' => 'Sinhala',
86
		'be' => 'Belarusian',
87
		'ka' => 'Kartuli (Georgian)',
88
		'my_MM' => 'Burmese - MYANMAR',
89
		'ur_PK' => 'Urdu (Pakistan)'
90
	];
91
92
	/**
93
	 * Get available languages split to the current, common and the rest
94
	 *
95
	 * @param IFactory $langFactory
96
	 * @param string $activeLangCode
97
	 *
98
	 * @return array
99
	 */
100
	public function getNormalizedLanguages(IFactory $langFactory, $activeLangCode) {
101
		$userLang = null;
102
		$commonLanguages = [];
103
		$languages = [];
104
105
		$availableCodes = $langFactory->findAvailableLanguages();
106
		foreach ($availableCodes as $languageCode) {
107
			$l = $langFactory->get('settings', $languageCode);
108
109
			// TRANSLATORS this is a self-name of your language for the language switcher
110
			$endonym = (string)$l->t('__language_name__');
111
			// Check if the language name is in the translation file
112
			// Fallback to hardcoded language name if it isn't
113
			$languageName = ($l->getLanguageCode() === $languageCode
114
				&& \substr($endonym, 0, 1) !== '_'
115
			) ? $endonym
116
				: $this->getLanguageNameByCode($languageCode);
117
118
			$ln = [
119
				'code' => $languageCode,
120
				'name' => $languageName
121
			];
122
			$languageWeight = $this->getCommonLanguageWeight($languageCode);
123
			if ($languageCode === $activeLangCode) {
124
				$userLang = $ln;
125
			} elseif ($languageWeight !== false) {
126
				$commonLanguages[$languageWeight] = $ln;
127
			} else {
128
				$languages[] = $ln;
129
			}
130
		}
131
132
		// if user language is not available but set somehow
133
		// show the actual code as name
134
		if ($userLang === null) {
135
			$userLang = [
136
				'code' => $activeLangCode,
137
				'name' => $activeLangCode,
138
			];
139
		}
140
141
		\ksort($commonLanguages);
142
		\usort($languages, [$this, 'compareLanguagesByName']);
143
144
		return [$userLang, $commonLanguages, $languages];
145
	}
146
147
	/**
148
	 * Get common language weight or false if language is not common
149
	 *
150
	 * @param string $languageCode
151
	 *
152
	 * @return false|int
153
	 */
154
	public function getCommonLanguageWeight($languageCode) {
155
		return \array_search($languageCode, $this->commonLanguages);
156
	}
157
158
	/**
159
	 * Get language name by code
160
	 *
161
	 * @param string $code
162
	 *
163
	 * @return string
164
	 */
165
	public function getLanguageNameByCode($code) {
166
		return (isset($this->languageCodes[$code]))
167
			? $this->languageCodes[$code]
168
			: $code;
169
	}
170
171
	/**
172
	 * Compare language arrays by name
173
	 * both arrays should have 'code' and 'name' keys
174
	 *
175
	 * @param string[] $a
176
	 * @param string[] $b
177
	 *
178
	 * @return int
179
	 */
180
	protected function compareLanguagesByName($a, $b) {
181 View Code Duplication
		if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
			// If a doesn't have a name, but b does, list b before a
183
			return 1;
184
		}
185 View Code Duplication
		if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
			// If a does have a name, but b doesn't, list a before b
187
			return -1;
188
		}
189
		// Otherwise compare the names
190
		return \strcmp($a['name'], $b['name']);
191
	}
192
}
193