Skip to content

Instantly share code, notes, and snippets.

@kijin
Created September 30, 2016 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijin/15e21a87c43715a63c69e03ebaa3c63b to your computer and use it in GitHub Desktop.
Save kijin/15e21a87c43715a63c69e03ebaa3c63b to your computer and use it in GitHub Desktop.
Convert XE lang files to XML format
<?php
/**
* XE 구버전의 PHP 언어파일이나 라이믹스의 PHP 언어파일을
* lang.xml 형식으로 변환하는 프로그램
*
* Copyright (c) 2016 Kijin Sung <kijin@kijinsung.com>
* License: MIT
*/
// 변환할 언어코드 목록
$lang_codes = array('ko', 'en');
// 변환할 파일들이 위치한 폴더명
$dir = __DIR__;
// 데이터를 로딩한다
$data = array();
foreach ($lang_codes as $lang_code)
{
if (file_exists($dir . '/' . $lang_code . '.php'))
{
$lang = new stdClass;
include $dir . '/' . $lang_code . '.php';
}
elseif (file_exists($dir . '/' . $lang_code . '.lang.php'))
{
$lang = new stdClass;
include $dir . '/' . $lang_code . '.lang.php';
}
else
{
continue;
}
foreach (get_object_vars($lang) as $key => $val)
{
$data[$key][$lang_code] = $val;
}
}
// 데이터가 없는 경우 종료한다
if (!count($data))
{
echo 'No lang data found in path: ' . $dir . PHP_EOL;
exit(1);
}
// XML 형식으로 기록한다
$output = array('<?xml version="1.0" encoding="UTF-8"?>', '<lang>');
foreach ($data as $key => $val)
{
$output[] = "\t" . '<item name="' . $key . '">';
foreach ($val as $lang_code => $lang_val)
{
$output[] = "\t\t" . '<value xml:lang="' . $lang_code . '"><![CDATA[' . $lang_val . ']]></value>';
}
$output[] = "\t" . '</item>';
}
$output[] = '</lang>';
$output[] = '';
// XML 파일을 저장한다
if (file_put_contents($dir . '/lang.xml', implode("\n", $output)))
{
echo 'Successfully converted to XML file: ' . $dir . '/lang.xml' . PHP_EOL;
exit(0);
}
else
{
echo 'Error writing XML file: ' . $dir . '/lang.xml' . PHP_EOL;
exit(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment