Completed
Push — master ( 10fc3d...cbc6e7 )
by Richard
14s
created

LegacyCodes::getLocaleCode()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 5
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xoops\Core\Locale;
13
14
/**
15
 * Xoops\Core\Locale\LegacyCodes - locale code to/from XOOPS legacy language directory names
16
 *
17
 * XOOPS 2.5.x and earlier used language defines for translations. These defines were stored in
18
 * in a directory structure in this form, where modulename is the module's "dirname" and
19
 * languagename is a lowercase english name for the language, i.e. "spanish" or "french":
20
 *
21
 *   modules/modulename/language/languagename/
22
 *   modules/modulename/language/languagename/admin.php
23
 *   modules/modulename/language/languagename/blocks.php
24
 *   modules/modulename/language/languagename/main.php
25
 *   modules/modulename/language/languagename/modinfo.php
26
 *   modules/modulename/language/languagename/help/help.html
27
 *   modules/modulename/language/languagename/mail_template/*.tpl
28
 *
29
 * This class provides a translation from a local code, such as "fr_FR," to the legacy
30
 * language name, so that language files in legacy modules can be loaded, if needed.
31
 *
32
 * @category  Xoops\Core\Locale\LegacyCodes
33
 * @package   Xoops
34
 * @author    Richard Griffith <[email protected]>
35
 * @copyright 2015 XOOPS Project (http://xoops.org)/
36
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
37
 * @link      http://xoops.org
38
 */
39
class LegacyCodes
40
{
41
    private static $rawCodes = array(
42
        ['ar_SA', 'ar',      'ar-Arab-SA', ['arabic']],
43
        ['bg_BG', 'bg',      'bg-Cyrl-BG', ['bulgarian']],
44
        ['cs_CZ', 'cs',      'cs-Latn-CZ', ['czech']],
45
        ['da_DK', 'da',      'da-Latn-DK', ['danish']],
46
        ['de_DE', 'de',      'de-Latn-DE', ['german']],
47
        ['el_GR', 'el',      'el-Grek-GR', ['greek']],
48
        ['en_US', 'en',      'en-Latn-US', ['english']],
49
        ['es_ES', 'es',      'es-Latn-ES', ['spanish']],
50
        ['fa_IR', 'fa',      'fa-Arab-IR', ['persian']],
51
        ['fr_FR', 'fr',      'fr-Latn-FR', ['french']],
52
        ['hr_HR', 'hr',      'hr-Latn-HR', ['croatian']],
53
        ['hu_HU', 'hu',      'hu-Latn-HU', ['hungarian']],
54
        ['it_IT', 'it',      'it-Latn-IT', ['italian']],
55
        ['ja_JP', 'ja',      'ja-Jpan-JP', ['japanese']],
56
        ['ko_KR', 'ko',      'ko-Kore-KR', ['korean']],
57
        ['ms_MY', 'ms',      'ms-Latn-MY', ['malaysian']],
58
        ['nl_NL', 'nl',      'nl-Latn-NL', ['dutch']],
59
        ['no_NO', 'no',      'no-Latn-NO', ['norwegian']],
60
        ['pl_PL', 'pl',      'pl-Latn-PL', ['polish']],
61
        ['pt_BR', 'pt',      'pt-Latn-BR', ['portuguesebr', 'brazilian']],
62
        ['pt_PT', 'pt_PT',   'pt-Latn-PT', ['portuguese']],
63
        ['ru_RU', 'ru',      'ru-Cyrl-RU', ['russian']],
64
        ['sk_SK', 'sk',      'sk-Latn-SK', ['slovak']],
65
        ['sl_SI', 'sl',      'sl-Latn-SI', ['slovenian']],
66
        ['sv_SE', 'sv',      'sv-Latn-SE', ['swedish']],
67
        ['th_TH', 'th',      'th-Thai-TH', ['thai']],
68
        ['tr_TR', 'tr',      'tr-Latn-TR', ['turkish']],
69
        ['vi_VN', 'vi',      'vi-Latn-VN', ['vietnamese']],
70
        ['zh_CN', 'zh_Hans', 'zh-Hans-CN', ['schinese']],
71
        ['zh_TW', 'zh_Hant', 'zh-Hant-TW', ['tchinese', 'chinese_zh']],
72
    );
73
74
    private static $namesByCode = null;
75
    private static $codesByName = null;
76
77
    /**
78
     * Get legacy language directory name for a locale code
79
     * @param string $localeCode locale code
80
     * @return string[] array of possible language directory names, empty if no mapping exists
81
     */
82 16
    public static function getLegacyName($localeCode)
83
    {
84 16
        if (empty(self::$namesByCode)) {
85 1
            foreach (self::$rawCodes as $codeDef) {
86 1
                list($locale, $shortLocale, $fullLocale, $languages) = $codeDef;
87 1
                self::$namesByCode[$locale] = $languages;
88 1
                self::$namesByCode[$shortLocale] = $languages;
89 1
                self::$namesByCode[$fullLocale] = $languages;
90
            }
91
        }
92
93 16
        if (isset(self::$namesByCode[$localeCode])) {
94 15
            return self::$namesByCode[$localeCode];
95
        }
96
97 1
        $langOnly = substr($localeCode, 0, 2);
98 1
        if (isset(self::$namesByCode[$langOnly])) {
99
            return self::$namesByCode[$langOnly];
100
        }
101
102 1
        return array();
103
    }
104
105
    /**
106
     * Get locale code representing a legacy language directory name
107
     * @param string $languageDir legacy language directory name
108
     * @return string|null locale code or null if no mapping exists
109
     */
110 1
    public static function getLocaleCode($languageDir)
111
    {
112 1
        if (empty(self::$codesByName)) {
113 1
            foreach (self::$rawCodes as $codeDef) {
114 1
                list($locale, $shortLocale, $fullLocale, $languages) = $codeDef;
115 1
                foreach ($languages as $language) {
116 1
                    self::$codesByName[$language] = $fullLocale;
117
                }
118
            }
119
        }
120
121 1
        if (isset(self::$codesByName[$languageDir])) {
122 1
            return self::$codesByName[$languageDir];
123
        }
124
125 1
        return null;
126
    }
127
}
128