#!/usr/bin/env php \s*$|'; break; case 'properties': $regex = '|^(?:#\s*)?([^\s]*)\s*=\s*(.*)$|'; break; default: echo "Unsupported extension $type\n"; return false; } foreach ($lines as $line) { preg_match($regex, $line, $matches); if (!empty($matches[0])) { $pairs[$matches[1]] = $matches[2]; } else { array_push($pairs, NULL); } } return $pairs; } function generate_locale($type, $english_file, $locale_file) { GLOBAL $missing_mode, $matching_are_missing; $output = ''; $english_pairs = parse_strings($type, $english_file); if (!$english_pairs) { return false; } $locale_pairs = parse_strings($type, $locale_file); foreach ($english_pairs as $key=>$val) { if (!$val) { if ($output != '') { $output .= "\n"; } continue; } switch ($type) { case 'dtd': $prefix = ''; break; case 'properties': $prefix = ''; $middle = '='; $suffix = ''; break; default: echo "Unsupported extension $type\n"; return false; } // If missing mode is 2, skip strings not available in this locale if (empty($locale_pairs[$key]) && $missing_mode == 2) { continue; } $output .= $prefix; $missing = empty($locale_pairs[$key]) || ($matching_are_missing && $english_pairs[$key] === $locale_pairs[$key]); // Use existing locale string if we have it if (!$missing) { $output .= $key . $middle . $locale_pairs[$key]; } // Default missing strings to English else if ($missing_mode == 0) { $output .= $key . $middle . $english_pairs[$key]; } // Leave missing strings blank else if ($missing_mode == 1) { $output .= $key . $middle; } $output .= $suffix; $output .= "\n"; } return $output; } 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(); // } $xw->endElement(); // $xw->endElement(); // $str = $xw->outputMemory(true); $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $doc->loadXML($str); return $doc->saveXML(); } ?>