From a11b224bbd206344023564413122208456841e16 Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Tue, 28 May 2019 16:00:42 -0700 Subject: [PATCH] pkp/pkp-lib#4779 Move to using XLIFF files for translation (work in progress) --- classes/i18n/LocaleFile.inc.php | 14 ++---- classes/i18n/PKPLocale.inc.php | 26 +++++----- composer.json | 3 +- tools/xmlToXliff.php | 86 +++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 tools/xmlToXliff.php diff --git a/classes/i18n/LocaleFile.inc.php b/classes/i18n/LocaleFile.inc.php index 1f8e8daeee2..33fafeec310 100644 --- a/classes/i18n/LocaleFile.inc.php +++ b/classes/i18n/LocaleFile.inc.php @@ -119,18 +119,10 @@ function translate($key, $params = array(), $locale = null) { */ static function &load($filename) { $localeData = array(); - - // Reload localization XML file - $xmlDao = new XMLDAO(); - $data = $xmlDao->parseStruct($filename, array('message')); - - // Build array with ($key => $string) - if (isset($data['message'])) { - foreach ($data['message'] as $messageData) { - $localeData[$messageData['attributes']['key']] = $messageData['value']; - } + $translations = Gettext\Translations::fromXliffFile($filename); + foreach ($translations as $translation) { + $localeData[$translation->getOriginal()] = $translation->getTranslation(); } - return $localeData; } diff --git a/classes/i18n/PKPLocale.inc.php b/classes/i18n/PKPLocale.inc.php index 55d9d69bb8b..86073cfba93 100644 --- a/classes/i18n/PKPLocale.inc.php +++ b/classes/i18n/PKPLocale.inc.php @@ -136,7 +136,7 @@ static function initialize($request) { } } - AppLocale::registerLocaleFile($locale, "lib/pkp/locale/$locale/common.xml"); + AppLocale::registerLocaleFile($locale, "lib/pkp/locale/$locale/common.xliff"); // Set site time zone // Starting from PHP 5.3.0 PHP will throw an E_WARNING if the default @@ -185,18 +185,18 @@ static function makeComponentMap($locale) { $baseDir = "lib/pkp/locale/$locale/"; return array( - LOCALE_COMPONENT_PKP_COMMON => $baseDir . 'common.xml', - LOCALE_COMPONENT_PKP_ADMIN => $baseDir . 'admin.xml', - LOCALE_COMPONENT_PKP_INSTALLER => $baseDir . 'installer.xml', - LOCALE_COMPONENT_PKP_MANAGER => $baseDir . 'manager.xml', - LOCALE_COMPONENT_PKP_READER => $baseDir . 'reader.xml', - LOCALE_COMPONENT_PKP_SUBMISSION => $baseDir . 'submission.xml', - LOCALE_COMPONENT_PKP_EDITOR => $baseDir . 'editor.xml', - LOCALE_COMPONENT_PKP_REVIEWER => $baseDir . 'reviewer.xml', - LOCALE_COMPONENT_PKP_USER => $baseDir . 'user.xml', - LOCALE_COMPONENT_PKP_GRID => $baseDir . 'grid.xml', - LOCALE_COMPONENT_PKP_DEFAULT => $baseDir . 'default.xml', - LOCALE_COMPONENT_PKP_API => $baseDir . 'api.xml', + LOCALE_COMPONENT_PKP_COMMON => $baseDir . 'common.xliff', + LOCALE_COMPONENT_PKP_ADMIN => $baseDir . 'admin.xliff', + LOCALE_COMPONENT_PKP_INSTALLER => $baseDir . 'installer.xliff', + LOCALE_COMPONENT_PKP_MANAGER => $baseDir . 'manager.xliff', + LOCALE_COMPONENT_PKP_READER => $baseDir . 'reader.xliff', + LOCALE_COMPONENT_PKP_SUBMISSION => $baseDir . 'submission.xliff', + LOCALE_COMPONENT_PKP_EDITOR => $baseDir . 'editor.xliff', + LOCALE_COMPONENT_PKP_REVIEWER => $baseDir . 'reviewer.xliff', + LOCALE_COMPONENT_PKP_USER => $baseDir . 'user.xliff', + LOCALE_COMPONENT_PKP_GRID => $baseDir . 'grid.xliff', + LOCALE_COMPONENT_PKP_DEFAULT => $baseDir . 'default.xliff', + LOCALE_COMPONENT_PKP_API => $baseDir . 'api.xliff', ); } diff --git a/composer.json b/composer.json index a98d12ca000..c063107bbc0 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "illuminate/validation": "^5.5.41", "firebase/php-jwt": "5.*", "danielstjules/stringy": "3.*", - "adodb/adodb-php": "dev-v5.20.14-mods" + "adodb/adodb-php": "dev-v5.20.14-mods", + "gettext/gettext": "^4.6.2" }, "require-dev": { "facebook/webdriver": "~1.6", diff --git a/tools/xmlToXliff.php b/tools/xmlToXliff.php new file mode 100644 index 00000000000..4b47583830f --- /dev/null +++ b/tools/xmlToXliff.php @@ -0,0 +1,86 @@ +xmlFile = array_shift($argv); + $this->xliffFile = array_shift($argv); + + if (empty($this->xmlFile) || !file_exists($this->xmlFile)) { + $this->usage(); + exit(1); + } + + if (empty($this->xliffFile)) { + $this->usage(); + exit(2); + } + } + + /** + * Print command usage information. + */ + function usage() { + echo "Script to convert XML locale file to XLIFF format\n" + . "Usage: {$this->scriptName} path/to/input-locale-file.xml path/to/output-xliff-file.xliff\n"; + } + + /** + * Rebuild the search index for all articles in all journals. + */ + function execute() { + $localeData = array(); + $xmlDao = new XMLDAO(); + $data = $xmlDao->parseStruct($this->xmlFile, array('message')); + + // Build array with ($key => $string) + if (isset($data['message'])) { + foreach ($data['message'] as $messageData) { + $localeData[$messageData['attributes']['key']] = $messageData['value']; + } + } + + $translations = new \Gettext\Translations(); + foreach ($localeData as $key => $translation) { + $translationObject = new \Gettext\Translation('', $key); + $translationObject->setTranslation($translation); + $translations->append($translationObject); + } + + $translations->toXliffFile($this->xliffFile); + } +} + +$tool = new poToCountries(isset($argv) ? $argv : array()); +$tool->execute(); + +