Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Created June 19, 2014 12:50
Show Gist options
  • Save Mark-H/79c92100aac1772f69f4 to your computer and use it in GitHub Desktop.
Save Mark-H/79c92100aac1772f69f4 to your computer and use it in GitHub Desktop.
compare.lexicon.php - A super useful script for people that want to update translations for MODX (or MODX Extras) and need to get a good at a glance view of the changes. Got it from the forums somewhere in the past, but can't find the original source now. It probably changed a bit over the years too.
<?php
$default = realpath(dirname(__FILE__).'/core/lexicon/en').'/';
$local = realpath(dirname(__FILE__).'/core/lexicon/nl').'/';
$showSame = false;
// find default files
$theFiles = array();
if($handle = opendir($default)) {
while(false !== ($file = readdir($handle))) {
if($file != '.' && $file != '..' && is_file($default.$file)) {
$theFiles[] = $file;
}
}
closedir($handle);
}
sort($theFiles);
// compare
$_lang = array();
foreach($theFiles as $filename) {
// load the local file
if (file_exists($local.$filename)) {
require_once($local.$filename);
$localLang = $_lang;
unset($_lang);
} else {
echo '<strong>'.$filename.'</strong><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">File '.$filename.' does not exist in translated file</span><br /><br />';
continue;
}
// load the default
require_once($default.$filename);
$defaultLang = $_lang;
unset($_lang);
echo '<strong>'.$filename.'</strong><br />';
// compare keys
foreach ($defaultLang as $key => $value) {
if (!isset($localLang[$key])) {
echo '&nbsp;&nbsp;&nbsp;&nbsp;';
echo 'The key <span style="color:red;">"'.$key.'"</span> <strong>doesn\'t exists</strong> in translated file<br />';
}
else if ($showSame && ($localLang[$key] == $value)) {
echo '&nbsp;&nbsp;&nbsp;&nbsp;';
echo '<span style="color:gray;">The value of key "'.$key.'" is the <strong>same as the default</strong> ';
echo '(<strong>'.strip_tags(substr($localLang[$key], 0, 40)).'...</strong> vs. <strong>'.strip_tags(substr($value, 0, 40)).'...</strong>)</span><br />';
}
}
foreach ($localLang as $key => $value) {
if(!isset($defaultLang[$key])) {
echo '&nbsp;&nbsp;&nbsp;&nbsp;';
echo 'The key "'.$key.'" exists in the translated file, but is <span style="color:blue;">removed by the default</span>.<br />';
}
}
echo '<br />';
}
?>
@Mark-H
Copy link
Author

Mark-H commented Jun 19, 2014

Simply add the file to the root of a MODX site, and update the $default and $local paths to point to the right directories. If you want to see when certain keys are the same in the default and local language (which can be an indication it needs to be translated still), set $showSame to true.

Open the file in the browser and you get a per-topic view of what is missing, the same or no longer used.

@Mark-H
Copy link
Author

Mark-H commented Jun 19, 2014

There's also http://forums.modx.com/thread/?thread=51022&page=1 which looks useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment