<!DCOTYPE html>
<html>
<head>
<title>SugarCRM旧语言包移植到新语言包工具</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style>
textarea.ta{width:100%;height:50px;border:1px solid;padding:5px 10px;}
</style>
</head>
<body>
<form method="POST" οnsubmit="return predo(this);">
旧版中文语言数组<br/>
<textarea name="zh" title="格式:array(key=>val[,key=>val])" class="ta" wrap="off" ></textarea><br/>
新版英文语言数组<br/>
<textarea name="en" title="格式:array(key=>val[,key=>val])" class="ta" wrap="off" ></textarea><br/>
<button type="submit" title="处理流程:">开始处理</button><input type="checkbox" title="框中填写的代码如果是js的对象请勾上,php会自动转换成php的array" name="json" />js对象
</form>
<script>
var typed = function( obj ) {
if ( obj == null ) {
return String( obj );
}
var class2type = {};
var core_toString = class2type.toString;
var t="Boolean Number String Function Array Date RegExp Object Error".split(" ");
for (var fi = 0;fi < t.length;fi++) class2type[ "[object " + t[fi] + "]" ] = t[fi].toLowerCase();
return typeof obj === "object" || typeof obj === "function" ?
class2type[ core_toString.call(obj) ] || "object" :
typeof obj;
};
var quote = function (str) {
var encode = /["\\\x00-\x1f]|[^\x1f-\x7f]/g,
meta = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
if (str.match(encode)) {
return '"' + str.replace(encode, function (a) {
var c = meta[a];
if (typeof c === 'string') {
return c;
}
c = a.charCodeAt();
return '\\u' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
}) + '"';
}
return '"' + str + '"';
};
var var2json = function (o) {
if (o === null) {
return 'null';
}
var pairs, k, name, val,
type = typed(o);
if (type === 'undefined') {
return undefined;
}
// Also covers instantiated Number and Boolean objects,
// which are typeof 'object' but thanks to $.type, we
// catch them here. I don't know whether it is right
// or wrong that instantiated primitives are not
// exported to JSON as an {"object":..}.
// We choose this path because that's what the browsers did.
if (type === 'number' || type === 'boolean') {
return String(o);
}
if (type === 'string') {
return quote(o);
}
if (typeof o === 'function') {
return o.toString();
}
if (type === 'date') {
var month = o.getUTCMonth() + 1,
day = o.getUTCDate(),
year = o.getUTCFullYear(),
hours = o.getUTCHours(),
minutes = o.getUTCMinutes(),
seconds = o.getUTCSeconds(),
milli = o.getUTCMilliseconds();
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
if (hours < 10) {
hours = '0' + hours;
}
if (minutes < 10) {
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
if (milli < 100) {
milli = '0' + milli;
}
if (milli < 10) {
milli = '0' + milli;
}
return '"' + year + '-' + month + '-' + day + 'T' +
hours + ':' + minutes + ':' + seconds +
'.' + milli + 'Z"';
}
pairs = [];
if ('array' == typed(o)) {
for (k = 0; k < o.length; k++) {
pairs.push(var2json(o[k]) || 'null');
}
return '[' + pairs.join(',') + ']';
}
// Any other object (plain object, RegExp, ..)
// Need to do typeof instead of $.type, because we also
// want to catch non-plain objects.
var hasOwn = Object.prototype.hasOwnProperty;
if (typeof o === 'object') {
for (k in o) {
// Only include own properties,
// Filter out inherited prototypes
if (hasOwn.call(o, k)) {
// Keys must be numerical or string. Skip others
type = typeof k;
if (type === 'number') {
name = '"' + k + '"';
} else if (type === 'string') {
name = quote(k);
} else {
continue;
}
type = typeof o[k];
// Invalid values like these return undefined
// from toJSON, however those object members
// shouldn't be included in the JSON string at all.
if (type !== 'function' && type !== 'undefined') {
val = var2json(o[k]);
pairs.push(name + ':' + val);
}
}
}
return '{' + pairs.join(',') + '}';
}
}
var json2var = function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
if ( data === null ) {
return data;
}
if ( typeof data === "string" ) {
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = data.replace(/^\s+|\s+$/g, '');
if ( data ) {
var rvalidchars = /^[\],:{}\s]*$/,
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
.replace( rvalidtokens, "]" )
.replace( rvalidbraces, "")) ) {
return ( new Function( "return " + data ) )();
}
}
}
alert( "Invalid JSON: " + data );
};
function predo(f) {
var zh = f.elements.zh.value;
var en = f.elements.en.value;
var json = f.elements.json.checked;
if (/^\s*$/.test(zh)) return alert('请输入旧版本中文语言数组代码'),false;
if (/^\s*$/.test(en)) return alert('请输入新版本英文语言数组代码'),false;
if (json) {
try {
zh = ( new Function( "return " + zh) )();
f.elements.zh.value = var2json(zh);
} catch (e) {
return alert('旧版本中文语言数组代码是错误的js代码:' +(e.message||e)),false;
}
try {
en = ( new Function( "return " + en) )();
f.elements.en.value = var2json(en);
} catch (e) {
return alert('新版本英文语言数组代码是错误的js代码:' +(e.message||e)),false;
}
var is_array = function(i){return 'array' == typed(i);};
var is_object = function(i){return 'object' == typed(i);};
var isset = function(i){return 'undefined' != typed(i);};
var is_string = function(i){return 'string' == typed(i);};
var FOR = function(o,func){
if ('array' == typed(o)) for (var fi = 0;fi < o.length;fi++) func.call(o[fi],fi,o[fi]);
else if ('object' == typed(o)) for (var fi in o) func.call(o[fi],fi,o[fi]);
else func.call(o,0, o);
};
var arrayFor = function($zh,$en){//递归处理语言数组,存在相同主键就移植到en数组中,没有保留en原有英文
var $rea = is_array($en) ? [] : {};
FOR ($en, function($k,$v){
if (is_string($v)) {
$rea[$k] = isset($zh[$k])? $zh[$k] : $v;
} else if (is_array($v) ) {
$rea[$k] = isset($zh[$k]) && is_array($zh[$k]) ?arrayFor($zh[$k],$v) :$v;
} else if (is_object($v) ) {
$rea[$k] = isset($zh[$k]) && is_object($zh[$k]) ?arrayFor($zh[$k],$v) :$v;
} else {
$rea[$k] = isset($zh[$k]) && !is_array($zh[$k])? $zh[$k] : $v;
}
});
return $rea;
}
var out = var2json(arrayFor(zh,en));
document.getElementById('out').innerHTML = '处理后数组<br/><textarea wrap="off" class="ta">'+(out ? out.replace(/[<>]/g, function($0){return '<' == $0 ? '<' :'>';}) : '') +'</textarea><br/>';
return false;//json,直接使用js处理
}
//return false;
};
</script>
<div id="out">
<?php
define('sugarEntry', 1);
function userErrorHandler() {
$e = func_get_args();
echo '<pre style="color:red;"><br/>----------运行出错---------:<br/>'.print_r($e, 1).'<br/>----------运行出错---------<br/></pre>';
}
set_error_handler("userErrorHandler");
set_exception_handler("userErrorHandler");
function shutdown() {
$a=error_get_last();
if($a != null) echo '<pre style="color:red;"><br/>++++++低级错误+++++<br/>'.print_r($a, 1).'<br/>++++++低级错误+++++<br/></pre>';
}
register_shutdown_function('shutdown');//如果使用了exit将不运行此脚本
//语言包数组处理
if (!empty($_POST)) {
$zh = $_POST['zh'];
$en = $_POST['en'];
$json = isset($_POST['json']);
if ($json) {
$zh = json_decode($zh, 1);
$en = json_decode($en, 1);
} else {
eval('$zh = '.$zh.';');
eval('$en = '.$en.';');
}
if (!is_array($zh) || !is_array($en)) {
echo '数组文件非法内容:'.$_POST['zh'].'='.gettype($zh).';'.$_POST['en'].'='.gettype($en);
} else {
$newEn = arrayFor($zh,$en);
echo '处理后数组<br/><textarea wrap="off" class="ta">'.htmlspecialchars ($json?json_encode($newEn) :var_export($newEn,1)).'</textarea><br/>' ;
}
}
function arrayFor($zh,$en){//递归处理语言数组,存在相同主键就移植到en数组中,没有保留en原有英文
$rea = array();
foreach ($en as $k => $v) {
if (is_array($v)) {
$rea[$k] = isset($zh[$k]) && is_array($zh[$k]) ?arrayFor($zh[$k],$v) :$v;
} else {
$rea[$k] = isset($zh[$k]) && !is_array($zh[$k])? $zh[$k] : $v;
}
}
return $rea;
}
?>
</div>
</body>
</html>
php的array处理
结果
array (
'a' => '是a',
'b' => '是b',
'c' => '是c',
'd' =>
array (
'dd' => 'kkk',
'ddd' => 'is ddd',
),
'e' => 'is e',
)