2023-04-23 08:37:35 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
error_reporting(E_ALL);
|
2023-04-26 03:09:44 +00:00
|
|
|
function error_handler($errno, $errstr, $errfile, $errline) {
|
|
|
|
echo "$errstr at $errfile:$errline\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
set_error_handler('error_handler');
|
2023-04-23 08:37:35 +00:00
|
|
|
|
|
|
|
$english_path = 'en-US-new'; // New English locale
|
2023-04-26 03:09:44 +00:00
|
|
|
$locale_path = 'locales'; // Source locales from run script
|
2023-04-23 08:37:35 +00:00
|
|
|
$content_locale_path = 'content-locales'; // Existing content locales from previous version
|
|
|
|
$output_dir = 'output/locale'; // Directory to save merged locales
|
|
|
|
$content_output_dir = 'output/content'; // Directory to save merged content locales
|
|
|
|
$use_content_input_dir = false;
|
|
|
|
$use_content_output_dir = true; // set to true for XPI, false for BZ
|
|
|
|
$localeCodeInOutputXML = true; // set to true for XPI, false for BZ
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
$locale_re = '/([a-z]{2})(\-[A-Z]{2})?/';
|
2023-05-10 10:20:06 +00:00
|
|
|
$locale_file_re = '/^[a-z].+\.(dtd|properties)$/';
|
2023-04-26 03:09:44 +00:00
|
|
|
|
|
|
|
// Zotero files
|
|
|
|
$english_files = array_filter(
|
|
|
|
scandir($english_path),
|
|
|
|
fn ($path) => preg_match($locale_file_re, $path)
|
|
|
|
);
|
|
|
|
// Mozilla files
|
|
|
|
$english_mozilla_files = array_filter(
|
|
|
|
scandir($english_path . "/mozilla"),
|
|
|
|
fn ($path) => preg_match($locale_file_re, $path)
|
|
|
|
);
|
|
|
|
$english_mozilla_files = array_map(fn ($path) => 'mozilla/' . $path, $english_mozilla_files);
|
|
|
|
$all_english_files = array_merge($english_files, $english_mozilla_files);
|
2023-04-23 08:37:35 +00:00
|
|
|
|
|
|
|
// Make output directories for each locale
|
2023-04-26 03:09:44 +00:00
|
|
|
$locales = array_filter(scandir($locale_path), fn ($path) => preg_match($locale_re, $path));
|
2023-04-23 08:37:35 +00:00
|
|
|
foreach ($locales as $locale) {
|
2023-04-26 03:09:44 +00:00
|
|
|
preg_match($locale_re, $locale, $matches);
|
|
|
|
if (!$matches) {
|
2023-04-23 08:37:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$dir = $output_dir . '/' . $locale . '/zotero/';
|
|
|
|
@mkdir($dir, 0775, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make content output directory for CSL files
|
|
|
|
if ($use_content_output_dir) {
|
|
|
|
@mkdir("$content_output_dir/csl/", 0775, true);
|
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
foreach ($all_english_files as $file) {
|
|
|
|
echo "Processing $file\n";
|
2023-04-23 08:37:35 +00:00
|
|
|
|
|
|
|
$extension = substr(strrchr($file, '.'), 1);
|
|
|
|
|
|
|
|
foreach ($locales as $locale) {
|
|
|
|
preg_match('/([a-z]{2})(\-[A-Z]{2})?/', $locale, $matches);
|
|
|
|
if (!isset($matches[1])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$locale = $matches[1];
|
|
|
|
if (!empty($matches[2])) {
|
|
|
|
$locale .= $matches[2];
|
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
$locale_source_file = "$locale_path/$locale/zotero/$file";
|
|
|
|
$output_locale_dir = "$output_dir/$locale/zotero/";
|
|
|
|
|
|
|
|
if (!file_exists($output_locale_dir . '/mozilla')) {
|
|
|
|
mkdir($output_locale_dir . '/mozilla', 0755);
|
|
|
|
}
|
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
if ($file == 'locales.xml') {
|
|
|
|
if (strlen($locale) == 2) {
|
|
|
|
$csl_locale = $locale . '-' . strtoupper($locale);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$csl_locale = $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($use_content_input_dir) {
|
|
|
|
$locale_source_file = "$content_locale_path/csl/locales-$csl_locale.xml";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($use_content_output_dir) {
|
2023-04-26 03:09:44 +00:00
|
|
|
$output_locale_dir = "$content_output_dir/csl/";
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($localeCodeInOutputXML) {
|
|
|
|
$save_file = "locales-$csl_locale.xml";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$save_file = "locales.xml";
|
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
echo "Saving {$output_locale_dir}{$save_file}\n";
|
2023-04-23 08:37:35 +00:00
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
$string = generate_csl_locale(
|
|
|
|
"$english_path/$file",
|
|
|
|
$locale_source_file,
|
|
|
|
$locale
|
|
|
|
);
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-04-26 03:09:44 +00:00
|
|
|
echo "Saving {$output_locale_dir}{$file}\n";
|
2023-04-23 08:37:35 +00:00
|
|
|
|
|
|
|
$save_file = $file;
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
$string = generate_locale(
|
|
|
|
$extension,
|
|
|
|
"$english_path/$file",
|
|
|
|
$locale_source_file,
|
2023-05-10 10:20:06 +00:00
|
|
|
$locale,
|
|
|
|
// Preserve whitespace in Mozilla files
|
|
|
|
str_contains($file, 'mozilla')
|
2023-04-26 03:09:44 +00:00
|
|
|
);
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can't handle this file, so bail
|
|
|
|
if (!$string) {
|
2023-04-26 03:09:44 +00:00
|
|
|
throw new Exception("Error generating file");
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
file_put_contents($output_locale_dir . $save_file, $string);
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
function parse_strings($type, $file, $locale = null) {
|
2023-04-23 08:37:35 +00:00
|
|
|
if (!file_exists($file)) {
|
2023-04-26 03:09:44 +00:00
|
|
|
$msg = "Source file $file doesn't exist";
|
|
|
|
if ($locale) {
|
|
|
|
if (strpos($file, 'updates.dtd') !== false) {
|
|
|
|
$missing_locales = ['br', 'en-GB', 'gl-ES', 'hu-HU', 'mn-MN'];
|
|
|
|
if (in_array($locale, $missing_locales)) {
|
|
|
|
echo $msg . "\n";
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Exception($msg);
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$pairs = array();
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'dtd':
|
2023-04-26 03:09:44 +00:00
|
|
|
// 1: space after ENTITY
|
|
|
|
// 2: key
|
|
|
|
// 3: space after key
|
|
|
|
// 4: string
|
2023-05-10 10:20:06 +00:00
|
|
|
// 5: space before >
|
|
|
|
// 6: newlines
|
|
|
|
$regex = '/<!ENTITY(\s*)([^\s]*)(\s*)"([^"]*)"(\s*)> *(\n*)(\n|$)/s';
|
2023-04-23 08:37:35 +00:00
|
|
|
break;
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
case 'properties':
|
2023-05-10 10:20:06 +00:00
|
|
|
$regex = '/([^\s]*)\s*= *([^\n]*)(\s*)(\n|$)/s';
|
2023-04-23 08:37:35 +00:00
|
|
|
break;
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
default:
|
2023-04-26 03:09:44 +00:00
|
|
|
throw new Exception("Unsupported extension .$type");
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
preg_match_all($regex, file_get_contents($file), $matches, PREG_SET_ORDER);
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
if ($type == 'dtd') {
|
|
|
|
$pairs[$match[2]] = $match;
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
2023-05-10 10:20:06 +00:00
|
|
|
else if ($type == 'properties') {
|
|
|
|
$pairs[$match[1]] = $match;
|
|
|
|
}
|
2023-04-23 08:37:35 +00:00
|
|
|
else {
|
2023-05-10 10:20:06 +00:00
|
|
|
throw new Exception("Unsupported");
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
return $pairs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-05-10 10:20:06 +00:00
|
|
|
function generate_locale($type, $english_file, $locale_file, $locale, $preserveWhitespace = false) {
|
2023-04-23 08:37:35 +00:00
|
|
|
$output = '';
|
2023-04-26 03:09:44 +00:00
|
|
|
|
|
|
|
// Keep copyright block at top of Mozilla files
|
2023-05-10 10:20:06 +00:00
|
|
|
preg_match('/^(<!--.+?-->|# This Source Code.+?2.0\/\.)\s+/s', file_get_contents($locale_file), $matches);
|
2023-04-26 03:09:44 +00:00
|
|
|
if ($matches) {
|
|
|
|
$output .= $matches[0];
|
|
|
|
}
|
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
$english_pairs = parse_strings($type, $english_file);
|
|
|
|
if (!$english_pairs) {
|
2023-04-26 03:09:44 +00:00
|
|
|
throw new Exception("No English pairs!");
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
$locale_pairs = parse_strings($type, $locale_file, $locale);
|
2023-04-23 08:37:35 +00:00
|
|
|
|
2023-04-26 03:09:44 +00:00
|
|
|
foreach ($english_pairs as $key => $val) {
|
2023-04-23 08:37:35 +00:00
|
|
|
if (!$val) {
|
|
|
|
if ($output != '') {
|
|
|
|
$output .= "\n";
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-10 10:20:06 +00:00
|
|
|
$english_val = $english_pairs[$key];
|
|
|
|
$locale_val = empty($locale_pairs[$key]) ? $english_val : $locale_pairs[$key];
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
switch ($type) {
|
|
|
|
case 'dtd':
|
2023-05-10 10:20:06 +00:00
|
|
|
// Don't replace string with space, only truly empty string, or else we mess up
|
|
|
|
// zotero.merge.of in Estonian, which apparently doesn't exist
|
|
|
|
if (empty($locale_val[4])) {
|
|
|
|
$locale_val = $english_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep spacing between components
|
|
|
|
if ($preserveWhitespace) {
|
|
|
|
$prefix = '<!ENTITY' . $locale_val[1];
|
|
|
|
$middle = $locale_val[3];
|
|
|
|
$string = '"' . $locale_val[4] . '"';
|
|
|
|
$suffix = $locale_val[5] . ">" . $english_val[6];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$prefix = '<!ENTITY ';
|
|
|
|
$middle = " ";
|
|
|
|
$string = '"' . $locale_val[4];
|
|
|
|
$suffix = '">' . $english_val[6];
|
|
|
|
}
|
2023-04-23 08:37:35 +00:00
|
|
|
break;
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
case 'properties':
|
2023-05-10 10:20:06 +00:00
|
|
|
// If empty value, use English
|
|
|
|
if (empty(trim($locale_val[2]))) {
|
|
|
|
$locale_val = $english_val;
|
|
|
|
}
|
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
$prefix = '';
|
|
|
|
$middle = '=';
|
2023-05-10 10:20:06 +00:00
|
|
|
$string = $locale_val[2];
|
|
|
|
$suffix = $english_val[3];
|
2023-04-23 08:37:35 +00:00
|
|
|
break;
|
2023-04-26 03:09:44 +00:00
|
|
|
|
2023-04-23 08:37:35 +00:00
|
|
|
default:
|
|
|
|
echo "Unsupported extension $type\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-10 10:20:06 +00:00
|
|
|
$output .= $prefix . $key . $middle . $string . $suffix . "\n";
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 10:20:06 +00:00
|
|
|
return trim($output) . "\n";
|
2023-04-23 08:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function generate_csl_locale($english_file, $locale_file, $locale) {
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
$english_str = file_get_contents($english_file);
|
|
|
|
$english_sxe = new SimpleXMLElement($english_str);
|
|
|
|
|
|
|
|
$str = file_get_contents($locale_file);
|
|
|
|
if (!$str) {
|
|
|
|
echo "Locale version of locales.xml not found\n";
|
|
|
|
return $english_str;
|
|
|
|
}
|
|
|
|
$locale_sxe = new SimpleXMLElement($str);
|
|
|
|
|
|
|
|
$xw = new XMLWriter();
|
|
|
|
$xw->openMemory();
|
|
|
|
$xw->startDocument('1.0', 'UTF-8');
|
|
|
|
$xw->startElement ('terms');
|
|
|
|
$xw->writeAttribute('xmlns', 'http://purl.org/net/xbiblio/csl');
|
|
|
|
$xw->startElement('locale');
|
|
|
|
$xw->writeAttribute('xml:lang', substr($locale, 0, 2));
|
|
|
|
|
|
|
|
$locale_sxe->registerXPathNamespace('csl', 'http://purl.org/net/xbiblio/csl');
|
|
|
|
|
|
|
|
foreach ($english_sxe->locale->term as $term) {
|
|
|
|
$name = $term->attributes()->name;
|
|
|
|
$form = $term->attributes()->form;
|
|
|
|
|
|
|
|
if ($form) {
|
|
|
|
$node = $locale_sxe->xpath("//csl:term[@name='$name' and @form='$form']");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$node = $locale_sxe->xpath("//csl:term[@name='$name' and not(@form)]");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($node[0])) {
|
|
|
|
$node = $node[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$node = $term;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xw->startElement('term');
|
|
|
|
$xw->writeAttribute('name', $name);
|
|
|
|
if ($form) {
|
|
|
|
$xw->writeAttribute('form', $form);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeOf($term->children()) > 0) {
|
|
|
|
$xw->startElement('single');
|
|
|
|
$xw->text($node->single ? $node->single : $term->single);
|
|
|
|
$xw->endElement();
|
|
|
|
$xw->startElement('multiple');
|
|
|
|
$xw->text($node->multiple ? $node->multiple : $term->multiple);
|
|
|
|
$xw->endElement();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If original had children and we don't any longer, use English
|
|
|
|
if (sizeOf($node[0]->children()) > 0) {
|
|
|
|
$xw->text($term);
|
|
|
|
}
|
|
|
|
// Otherwise use the locale string
|
|
|
|
else {
|
|
|
|
$xw->text($node[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$xw->endElement(); // </term>
|
|
|
|
}
|
|
|
|
|
|
|
|
$xw->endElement(); // </locale>
|
|
|
|
$xw->endElement(); // </terms>
|
|
|
|
$str = $xw->outputMemory(true);
|
|
|
|
|
|
|
|
$doc = new DOMDocument('1.0');
|
|
|
|
$doc->formatOutput = true;
|
|
|
|
$doc->loadXML($str);
|
|
|
|
return $doc->saveXML();
|
|
|
|
}
|
|
|
|
?>
|