forked from langsci/customLocale
-
Notifications
You must be signed in to change notification settings - Fork 19
/
CustomLocaleHandler.php
60 lines (52 loc) · 2.12 KB
/
CustomLocaleHandler.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
<?php
/**
* @file CustomLocaleHandler.php
*
* Copyright (c) 2016-2022 Language Science Press
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CustomLocaleHandler
*/
namespace APP\plugins\generic\customLocale;
use APP\handler\Handler;
use Gettext\Translation;
use PKP\core\PKPRequest;
use PKP\i18n\translation\LocaleFile;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
class CustomLocaleHandler extends Handler
{
/**
* Print the custom locale changes.
*/
public function printCustomLocaleChanges(array $args, PKPRequest $request): void
{
$customLocalePath = CustomLocalePlugin::getStoragePath();
// Get all po-files in the custom locale directory
$directory = new RecursiveDirectoryIterator($customLocalePath);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.po$/i', RecursiveRegexIterator::GET_MATCH);
$files = array_keys(iterator_to_array($regex));
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="custom-locale.txt"');
// iterate through all customized files
foreach ($files as $localeFile) {
/** @var Translation[] */
$customTranslations = LocaleFile::loadTranslations($localeFile)->getTranslations();
if (!count($customTranslations)) {
continue;
}
$locale = explode('/', substr($localeFile, strlen($customLocalePath) + 1))[0];
$translator = CustomLocalePlugin::getTranslator($locale);
echo "{$locale}\n\n";
foreach ($customTranslations as $translation) {
$localeKey = $translation->getOriginal();
echo __('common.id') . ": {$localeKey}"
. "\n" . __('plugins.generic.customLocale.file.reference') . ": {$translator->getSingular($localeKey)}"
. "\n" . __('plugins.generic.customLocale.file.custom') . ": {$translation->getTranslation()}\n\n";
}
}
}
}