/
CoreTranslationLoader.php
79 lines (73 loc) · 2.58 KB
/
CoreTranslationLoader.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
<?php
namespace Concrete\Core\Localization\Translator\Adapter\Zend\Translation\Loader\Gettext;
use Concrete\Core\Localization\Translator\Translation\Loader\AbstractTranslationLoader;
use Concrete\Core\Localization\Translator\TranslatorAdapterInterface;
/**
* Translation loader that loads the concrete5 core translations for the Zend
* translation adapter.
*
* @author Antti Hukkanen <antti.hukkanen@mainiotech.fi>
*/
class CoreTranslationLoader extends AbstractTranslationLoader
{
/**
* {@inheritdoc}
*/
public function loadTranslations(TranslatorAdapterInterface $translatorAdapter)
{
$locale = $translatorAdapter->getLocale();
$languageFile = $this->locateLanguageFile($locale);
if ($languageFile !== null) {
$translator = $translatorAdapter->getTranslator();
$translator->addTranslationFile('gettext', $languageFile);
}
}
/**
* Get the full path to the file containing the localized strings for a specific locale.
*
* @param string $localeID The ID of the locale
*
* @return string|null Returns the full path of the file if it exists, null otherwise
*/
private function locateLanguageFile($localeID)
{
$localeIDAlternatives = $this->getLocaleIDAlternatives($localeID);
$result = null;
foreach ($localeIDAlternatives as $localeIDAlternative) {
$languageFile = $this->getAppLanguageFilePath($localeIDAlternative);
if (!is_file($languageFile)) {
$languageFile = $this->getCoreLanguageFilePath($localeIDAlternative);
if (!is_file($languageFile)) {
$languageFile = null;
}
}
if ($languageFile !== null) {
$result = $languageFile;
break;
}
}
return $result;
}
/**
* Get the full path to the file containing the localized strings for a specific locale (in the application directory).
*
* @param string $localeID The ID of the locale
*
* @return string
*/
private function getAppLanguageFilePath($localeID)
{
return DIR_LANGUAGES . '/' . $localeID . '/LC_MESSAGES/messages.mo';
}
/**
* Get the full path to the file containing the localized strings for a specific locale (in the concrete directory).
*
* @param string $localeID The ID of the locale
*
* @return string
*/
private function getCoreLanguageFilePath($localeID)
{
return DIR_LANGUAGES_CORE . '/' . $localeID . '/LC_MESSAGES/messages.mo';
}
}