Skip to content

Instantly share code, notes, and snippets.

@voduytuan
Last active November 6, 2016 13:19
Show Gist options
  • Save voduytuan/d6a0bb326ab403c9416ee1023e0f9a4d to your computer and use it in GitHub Desktop.
Save voduytuan/d6a0bb326ab403c9416ee1023e0f9a4d to your computer and use it in GitHub Desktop.
<?php
/**
* Download from: https://github.com/raulferras/PHP-po-parser/tree/master/src/Sepia
*/
include('./libs/Sepia/InterfaceHandler.php');
include('./libs/Sepia/FileHandler.php');
include('./libs/Sepia/StringHandler.php');
include('./libs/Sepia/PoParser.php');
/**
* Example config.json
{
"mainlanguage": "vn",
"sublanguages": ["en"],
"input": "./po",
"output": "../js/language"
}
*/
$confJson = file_get_contents('config.json');
$conf = json_decode($confJson, true);
//Check valid config format (JSON)
if (is_null($conf)) {
die('Config is invalid format. (JSON format required).');
}
//Check valid main language string and directory
if ($conf['mainlanguage'] == '' || !file_exists($conf['input'])
|| !file_exists($conf['input'] . DIRECTORY_SEPARATOR . $conf['mainlanguage'])) {
die('Main Language Directory is not found or not existed.');
}
//Check readable of main language
if (!is_readable($conf['input'] . DIRECTORY_SEPARATOR . $conf['mainlanguage'])) {
die('Main Language Directory is not readable.');
}
//Check sublanguage directory
if (is_array($conf['sublanguages']) && !empty($conf['sublanguages'])) {
foreach ($conf['sublanguages'] as $sublanguage) {
if (!file_exists($conf['input'] . DIRECTORY_SEPARATOR . $sublanguage)) {
die('Sub Language Directory is not found. ('.$sublanguage.')');
}
if (!is_readable($conf['input'] . DIRECTORY_SEPARATOR . $sublanguage)) {
die('Sub Language Directory is not readable. ('.$sublanguage.')');
}
}
}
//Check existed output directory
if ($conf['output'] == '' || !file_exists($conf['output'])) {
die('Output Directory is not set or not existed.');
}
//Check writable of output directory
if (!is_writable($conf['output'])) {
die('Output Directory is not writable.');
}
///////////////////////////////////////////////////
/// Everything is OK now
/// Start combine and convert
/// to JS object and save to output directory
$mainDirectory = $conf['input'] . DIRECTORY_SEPARATOR . $conf['mainlanguage'];
$mainFiles = scandir($mainDirectory);
$mainPoFiles = array();
foreach ($mainFiles as $mainFile) {
if ($mainFile != '.' && $mainFile != '..' && substr($mainFile, strrpos($mainFile, '.') + 1) == 'po') {
$mainPoFiles[] = $mainFile;
}
}
//Check if main po directory is empty
if (empty($mainPoFiles)) {
die('There is no PO file in main language.');
}
///////////////////////////////////////////////////
/// Start reading po contents
echo 'Processed files:' . PHP_EOL;
foreach ($mainPoFiles as $i => $mainPoFile) {
$poFilePath = $mainDirectory . DIRECTORY_SEPARATOR . $mainPoFile;
echo ($i + 1) . '> ' . $poFilePath . PHP_EOL;
if (!is_readable($poFilePath)) {
die('Can not read PO file "'.$poFilePath.'"');
}
//init data for current language file
$outputLangData = array();
$outputLangData[$conf['mainlanguage']] = array();
//parsing sub PO
$subPoDataByLang = array();
foreach ($conf['sublanguages'] as $sublanguage) {
//init for sublanguage
$outputLangData[$sublanguage] = array();
$subPoFilePath = $conf['input'] . DIRECTORY_SEPARATOR . $sublanguage . DIRECTORY_SEPARATOR . $mainPoFile;
if (!file_exists($subPoFilePath) || !is_readable($subPoFilePath)) {
die('Sub language PO file "'.$subPoFilePath.'" not found or not readable.');
}
//build data for sublanguage
$subPoDataByLang[$sublanguage] = array();
$fileHandler = new \Sepia\FileHandler($subPoFilePath);
$poParser = new \Sepia\PoParser($fileHandler);
$entries = $poParser->parse();
foreach ($entries as $entry) {
$subPoDataByLang[$sublanguage][$entry['msgid'][0]] = $entry['msgstr'][0];
}
}
//parsing main PO
$fileHandler = new \Sepia\FileHandler($poFilePath);
$poParser = new \Sepia\PoParser($fileHandler);
$entries = $poParser->parse();
foreach ($entries as $entry) {
$outputLangData[$conf['mainlanguage']][$entry['msgid'][0]] = $entry['msgstr'][0];
//process this entry for other sub languages
// make sure all sub language will have the same entry number (msgid) as main language
foreach ($conf['sublanguages'] as $sublanguage) {
if (array_key_exists($entry['msgid'][0], $subPoDataByLang[$sublanguage])) {
$outputLangData[$sublanguage][$entry['msgid'][0]] = $subPoDataByLang[$sublanguage][$entry['msgid'][0]];
} else {
$outputLangData[$sublanguage][$entry['msgid'][0]] = '';
}
}
}
$jsonData = json_encode($outputLangData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
//remove quotes from key after pretty
$jsonData = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $jsonData);
$content = getLanguageJsTemplate($poFilePath, $jsonData);
$outputFile = $conf['output'] . DIRECTORY_SEPARATOR . str_replace('.po', '.js', $mainPoFile);
file_put_contents($outputFile, $content);
}
function getLanguageJsTemplate($poFilePath, $content)
{
return "/** Content of this file is auto generated by PO converter from main file: {$poFilePath} */
define(['baselanguage'], function(BaseLanguage){
var lang = {$content};
return new BaseLanguage({lang: lang});
});
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment