사용하는 언어에 언어코드가 없을 경우 다른 언어에서 찾아 가져오기

번역되지않은 문장이 있는 경우 달랑 언어코드만을 출력하지말고, 다른 언어로 대체하기 (영어 우선)
This commit is contained in:
conory 2016-03-02 23:53:49 +09:00
parent 929a694ee1
commit 5f563af984

View file

@ -16,6 +16,7 @@ class Lang
* Configuration. * Configuration.
*/ */
protected $_language; protected $_language;
protected $_other_lang = array();
protected $_loaded_directories = array(); protected $_loaded_directories = array();
protected $_loaded_plugins = array(); protected $_loaded_plugins = array();
protected $_search_priority = array(); protected $_search_priority = array();
@ -85,6 +86,7 @@ class Lang
* Load translations from a directory. * Load translations from a directory.
* *
* @param string $dir * @param string $dir
* @param string $plugin_name
* @return bool * @return bool
*/ */
public function loadDirectory($dir, $plugin_name = null) public function loadDirectory($dir, $plugin_name = null)
@ -97,34 +99,34 @@ class Lang
return true; return true;
} }
// Look for language files. $default_lang = array('en', 'ko');
if (file_exists($dir . '/' . $this->_language . '.php')) if (!in_array($this->_language, $default_lang))
{ {
$filename = $dir . '/' . $this->_language . '.php'; $default_lang[] = $this->_language;
} }
elseif (($hyphen = strpos($this->_language, '-')) !== false)
foreach ($default_lang as $val)
{ {
if (file_exists($dir . '/' . substr($this->_language, 0, $hyphen) . '.php')) $lang = $this->getPluginLang($dir, $val);
if (empty($lang))
{ {
$filename = $dir . '/' . substr($this->_language, 0, $hyphen) . '.php'; continue;
}
if ($this->_language === $val)
{
$this->_loaded_plugins[$plugin_name] = $lang;
}
else
{
$this->_other_lang[$plugin_name][$val] = $lang;
} }
}
elseif (file_exists("$dir/lang.xml"))
{
$filename = Compat\LangParser::compileXMLtoPHP("$dir/lang.xml", $this->_language === 'ja' ? 'jp' : $this->_language);
}
elseif (file_exists($dir . '/' . ($this->_language === 'ja' ? 'jp' : $this->_language) . '.lang.php'))
{
$filename = $dir . '/' . ($this->_language === 'ja' ? 'jp' : $this->_language) . '.lang.php';
} }
// Load the language file. // Load the language file.
if ($filename) if (isset($this->_loaded_plugins[$plugin_name]))
{ {
$lang = new \stdClass;
include $filename;
$this->_loaded_directories[$dir] = true; $this->_loaded_directories[$dir] = true;
$this->_loaded_plugins[$plugin_name] = $lang;
array_unshift($this->_search_priority, $plugin_name); array_unshift($this->_search_priority, $plugin_name);
return true; return true;
} }
@ -136,6 +138,48 @@ class Lang
} }
} }
/**
* get the language file from plugin.
*
* @param string $dir
* @param string $language
* @return object
*/
public function getPluginLang($dir, $language = null)
{
if (!$language)
{
$language = $this->_language;
}
if (file_exists($dir . '/' . $language . '.php'))
{
$filename = $dir . '/' . $language . '.php';
}
elseif (($hyphen = strpos($language, '-')) !== false && file_exists($dir . '/' . substr($language, 0, $hyphen) . '.php'))
{
$filename = $dir . '/' . substr($language, 0, $hyphen) . '.php';
}
elseif (file_exists("$dir/lang.xml"))
{
$filename = Compat\LangParser::compileXMLtoPHP("$dir/lang.xml", $language === 'ja' ? 'jp' : $language);
}
elseif (file_exists($dir . '/' . ($language === 'ja' ? 'jp' : $language) . '.lang.php'))
{
$filename = $dir . '/' . ($language === 'ja' ? 'jp' : $language) . '.lang.php';
}
if (!$filename)
{
return new \stdClass;
}
$lang = new \stdClass;
include $filename;
return $lang;
}
/** /**
* Get the list of supported languages. * Get the list of supported languages.
* *
@ -179,10 +223,15 @@ class Lang
{ {
return $this->_loaded_plugins[$plugin_name]->{$key}; return $this->_loaded_plugins[$plugin_name]->{$key};
} }
else foreach ($this->_other_lang[$plugin_name] as $lang)
{ {
return $key; if (isset($lang->{$key}))
{
return $lang->{$key};
}
} }
return $key;
} }
// Search custom translations first. // Search custom translations first.
@ -213,6 +262,18 @@ class Lang
} }
} }
} }
// Search other language.
foreach ($this->_other_lang as $plugin_name => $val)
{
foreach ($this->_other_lang[$plugin_name] as $lang)
{
if (isset($lang->{$key}))
{
return $lang->{$key};
}
}
}
// If no translation is found, return the key. // If no translation is found, return the key.
return $key; return $key;