Skip to content
Snippets Groups Projects
Commit 8c5800c7 authored by Demian Katz's avatar Demian Katz
Browse files

Utility to normalize language files.

parent 18733cec
No related merge requests found
<?php
/**
* Class to consistently format ExtendedIni language files.
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Translator
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
namespace VuFind\I18n;
use Zend\I18n\Translator\TextDomain;
/**
* Class to consistently format ExtendedIni language files.
*
* @category VuFind2
* @package Translator
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
class ExtendedIniNormalizer
{
/**
* Normalize a directory on disk.
*
* @param string $dir Directory to normalize.
*
* @return void
*/
public function normalizeDirectory($dir)
{
$dir = rtrim($dir, '/');
$handle = opendir($dir);
while ($file = readdir($handle)) {
if (substr($file, -4) == '.ini') {
$this->normalizeFile($dir . '/' . $file);
}
}
closedir($handle);
}
/**
* Normalize a file on disk.
*
* @param string $file Filename.
*
* @return void
*/
public function normalizeFile($file)
{
$reader = new Translator\Loader\ExtendedIniReader();
// Reading and rewriting the file by itself will eliminate all comments;
// we should extract comments separately and then recombine the parts.
$fileArray = file($file);
// Strip off UTF-8 BOM if necessary.
$bom = html_entity_decode('&#xFEFF;', ENT_NOQUOTES, 'UTF-8');
$fileArray[0] = str_replace($bom, '', $fileArray[0]);
$comments = $this->extractComments($fileArray);
$strings = $this->formatAsString($reader->getTextDomain($fileArray, false));
file_put_contents($file, $comments . $strings);
}
/**
* Normalize a TextDomain or array to a string that can be written to file.
*
* @param array|TextDomain $input Language values to format.
*
* @return string
*/
public function formatAsString($input)
{
// This is easier to work with as an associative array:
$input = (array)$input;
// Perform a case-insensitive sort:
ksort($input, SORT_FLAG_CASE | SORT_STRING);
// Format the lines:
$output = '';
foreach ($input as $key => $value) {
$output .= "$key = \"$value\"\n";
}
return trim($output);
}
/**
* Extract comments from an array of lines read from a file.
*
* @param array $contents Contents to scan for comments.
*
* @return string
*/
public function extractComments($contents)
{
$comments = '';
foreach ($contents as $line) {
if (substr(trim($line), 0, 1) == ';') {
$comments .= $line;
}
}
return $comments;
}
}
\ No newline at end of file
......@@ -78,6 +78,7 @@ class Module implements \Zend\ModuleManager\Feature\ConsoleUsageProviderInterfac
'harvest merge-marc' => 'MARC merge tool',
'import import-xsl' => 'XSLT importer',
'import webcrawl' => 'Web crawler',
'language normalize' => 'Normalize a directory of language files',
'util createHierarchyTrees' => 'Cache populator for hierarchies',
'util cssBuilder' => 'LESS compiler',
'util deletes' => 'Tool for deleting Solr records',
......
......@@ -6,6 +6,7 @@ $config = array(
'invokables' => array(
'harvest' => 'VuFindConsole\Controller\HarvestController',
'import' => 'VuFindConsole\Controller\ImportController',
'language' => 'VuFindConsole\Controller\LanguageController',
'util' => 'VuFindConsole\Controller\UtilController',
),
),
......
<?php
/**
* CLI Controller Module (language tools)
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Controller
* @author Chris Hallberg <challber@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/vufind2:building_a_controller Wiki
*/
namespace VuFindConsole\Controller;
use Zend\Console\Console;
/**
* This controller handles various command-line tools for dealing with language files
*
* @category VuFind2
* @package Controller
* @author Chris Hallberg <challber@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/vufind2:building_a_controller Wiki
*/
class LanguageController extends AbstractBase
{
/**
* Normalizer
*
* @return \Zend\Console\Response
*/
public function normalizeAction()
{
// Display help message if parameters missing:
$argv = $this->consoleOpts->getRemainingArgs();
if (!isset($argv[0])) {
Console::writeLine(
"Usage: {$_SERVER['argv'][0]} [target]"
);
Console::writeLine("\ttarget - a file or directory to normalize");
return $this->getFailureResponse();
}
$normalizer = new \VuFind\I18n\ExtendedIniNormalizer();
$target = $argv[0];
if (is_dir($target)) {
$normalizer->normalizeDirectory($target);
} else if (is_file($target)) {
$normalizer->normalizeFile($target);
} else {
Console::writeLine("{$target} does not exist.");
return $this->getFailureResponse();
}
return $this->getSuccessResponse();
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment