Skip to content

Commit

Permalink
pkp#4779 Move to using XLIFF files for translation (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed May 28, 2019
1 parent e9fc644 commit a11b224
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
14 changes: 3 additions & 11 deletions classes/i18n/LocaleFile.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
26 changes: 13 additions & 13 deletions classes/i18n/PKPLocale.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
);
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
86 changes: 86 additions & 0 deletions tools/xmlToXliff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* @file tools/poToCountries.php
*
* Copyright (c) 2014-2019 Simon Fraser University
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class poToCountries
* @ingroup tools
*
* @brief CLI tool to convert a .PO file for ISO3166 into the countries.xml format
* supported by the PKP suite. These .po files can be sourced from e.g.:
* https://packages.debian.org/source/sid/iso-codes
*/

require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');

class poToCountries extends CommandLineTool {
/** @var string Name of source XML locale file */
protected $xmlFile;

/** @var string Name of target XLIFF file */
protected $xliffFile;

/**
* Constructor
*/
function __construct($argv = array()) {
parent::__construct($argv);

array_shift($argv); // Shift the tool name off the top

$this->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();


0 comments on commit a11b224

Please sign in to comment.