[feature/extension-manager] Support for loading language files from extensions

The referenced extension needs to be explicitly specified in an add_lang_ext()
call.

PHPBB3-10323
This commit is contained in:
Nils Adermann 2011-10-14 00:44:48 +02:00
parent 724f40f0f4
commit 639e3b9f17
4 changed files with 76 additions and 18 deletions

View file

@ -268,7 +268,7 @@ class phpbb_extension_finder
* @param bool $is_dir Whether the found items should be directories * @param bool $is_dir Whether the found items should be directories
* @return array An array of paths to found items * @return array An array of paths to found items
*/ */
protected function find($cache = true, $is_dir = false) public function find($cache = true, $is_dir = false)
{ {
$this->query['is_dir'] = $is_dir; $this->query['is_dir'] = $is_dir;
$query = md5(serialize($this->query)); $query = md5(serialize($this->query));

View file

@ -89,11 +89,12 @@ class phpbb_extension_manager
* Generates the path to an extension * Generates the path to an extension
* *
* @param string $name The name of the extension * @param string $name The name of the extension
* @param bool $phpbb_relative Whether the path should be relative to phpbb root
* @return string Path to an extension * @return string Path to an extension
*/ */
public function get_extension_path($name) public function get_extension_path($name, $phpbb_relative = false)
{ {
return 'ext/' . basename($name) . '/'; return (($phpbb_relative) ? $this->phpbb_root_path : '') . 'ext/' . basename($name) . '/';
} }
/** /**

View file

@ -865,7 +865,7 @@ class p_master
function add_mod_info($module_class) function add_mod_info($module_class)
{ {
global $user, $phpEx; global $user, $phpEx;
/*
if (file_exists($user->lang_path . $user->lang_name . '/mods')) if (file_exists($user->lang_path . $user->lang_name . '/mods'))
{ {
$add_files = array(); $add_files = array();
@ -888,6 +888,23 @@ class p_master
{ {
$user->add_lang($add_files); $user->add_lang($add_files);
} }
}*/
global $phpbb_extension_manager;
$finder = $phpbb_extension_manager->get_finder();
$lang_files = $finder
->prefix('info_' . strtolower($module_class) . '_')
->suffix(".$phpEx")
->directory('/language/' . $user->lang_name)
->default_path('language/' . $user->lang_name . '/mods/')
->default_directory('')
->find();
foreach ($lang_files as $lang_file => $ext_name)
{
$user->add_lang_ext($ext_name, $lang_file);
} }
} }

View file

@ -1901,6 +1901,7 @@ class user extends session
* @param mixed $lang_set specifies the language entries to include * @param mixed $lang_set specifies the language entries to include
* @param bool $use_db internal variable for recursion, do not use * @param bool $use_db internal variable for recursion, do not use
* @param bool $use_help internal variable for recursion, do not use * @param bool $use_help internal variable for recursion, do not use
* @param string $ext_name The extension to load language from, or empty for global files
* *
* Examples: * Examples:
* <code> * <code>
@ -1911,7 +1912,7 @@ class user extends session
* $lang_set = array('help' => 'faq', 'db' => array('help:faq', 'posting')) * $lang_set = array('help' => 'faq', 'db' => array('help:faq', 'posting'))
* </code> * </code>
*/ */
function add_lang($lang_set, $use_db = false, $use_help = false) function add_lang($lang_set, $use_db = false, $use_help = false, $ext_name = '')
{ {
global $phpEx; global $phpEx;
@ -1925,36 +1926,54 @@ class user extends session
if ($key == 'db') if ($key == 'db')
{ {
$this->add_lang($lang_file, true, $use_help); $this->add_lang($lang_file, true, $use_help, $ext_name);
} }
else if ($key == 'help') else if ($key == 'help')
{ {
$this->add_lang($lang_file, $use_db, true); $this->add_lang($lang_file, $use_db, true, $ext_name);
} }
else if (!is_array($lang_file)) else if (!is_array($lang_file))
{ {
$this->set_lang($this->lang, $this->help, $lang_file, $use_db, $use_help); $this->set_lang($this->lang, $this->help, $lang_file, $use_db, $use_help, $ext_name);
} }
else else
{ {
$this->add_lang($lang_file, $use_db, $use_help); $this->add_lang($lang_file, $use_db, $use_help, $ext_name);
} }
} }
unset($lang_set); unset($lang_set);
} }
else if ($lang_set) else if ($lang_set)
{ {
$this->set_lang($this->lang, $this->help, $lang_set, $use_db, $use_help); $this->set_lang($this->lang, $this->help, $lang_set, $use_db, $use_help, $ext_name);
} }
} }
/**
* Add Language Items from an extension - use_db and use_help are assigned where needed (only use them to force inclusion)
*
* @param string $ext_name The extension to load language from, or empty for global files
* @param mixed $lang_set specifies the language entries to include
* @param bool $use_db internal variable for recursion, do not use
* @param bool $use_help internal variable for recursion, do not use
*/
function add_lang_ext($ext_name, $lang_set, $use_db = false, $use_help = false)
{
if ($ext_name === '/')
{
$ext_name = '';
}
$this->add_lang($lang_set, $use_db, $use_help, $ext_name);
}
/** /**
* Set language entry (called by add_lang) * Set language entry (called by add_lang)
* @access private * @access private
*/ */
function set_lang(&$lang, &$help, $lang_file, $use_db = false, $use_help = false) function set_lang(&$lang, &$help, $lang_file, $use_db = false, $use_help = false, $ext_name = '')
{ {
global $phpEx; global $phpbb_root_path, $phpEx;
// Make sure the language name is set (if the user setup did not happen it is not set) // Make sure the language name is set (if the user setup did not happen it is not set)
if (!$this->lang_name) if (!$this->lang_name)
@ -1970,11 +1989,32 @@ class user extends session
{ {
if ($use_help && strpos($lang_file, '/') !== false) if ($use_help && strpos($lang_file, '/') !== false)
{ {
$language_filename = $this->lang_path . $this->lang_name . '/' . substr($lang_file, 0, stripos($lang_file, '/') + 1) . 'help_' . substr($lang_file, stripos($lang_file, '/') + 1) . '.' . $phpEx; $filename = dirname($lang_file) . '/help_' . basename($lang_file);
} }
else else
{ {
$language_filename = $this->lang_path . $this->lang_name . '/' . (($use_help) ? 'help_' : '') . $lang_file . '.' . $phpEx; $filename = (($use_help) ? 'help_' : '') . $lang_file;
}
if ($ext_name)
{
global $phpbb_extension_manager;
$ext_path = $phpbb_extension_manager->get_extension_path($ext_name, true);
$lang_path = $ext_path . 'language/';
}
else
{
$lang_path = $this->lang_path;
}
if (strpos($phpbb_root_path . $filename, $lang_path . $this->lang_name . '/') === 0)
{
$language_filename = $phpbb_root_path . $filename;
}
else
{
$language_filename = $lang_path . $this->lang_name . '/' . $filename . '.' . $phpEx;
} }
if (!file_exists($language_filename)) if (!file_exists($language_filename))
@ -1984,24 +2024,24 @@ class user extends session
if ($this->lang_name == 'en') if ($this->lang_name == 'en')
{ {
// The user's selected language is missing the file, the board default's language is missing the file, and the file doesn't exist in /en. // The user's selected language is missing the file, the board default's language is missing the file, and the file doesn't exist in /en.
$language_filename = str_replace($this->lang_path . 'en', $this->lang_path . $this->data['user_lang'], $language_filename); $language_filename = str_replace($lang_path . 'en', $lang_path . $this->data['user_lang'], $language_filename);
trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR); trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR);
} }
else if ($this->lang_name == basename($config['default_lang'])) else if ($this->lang_name == basename($config['default_lang']))
{ {
// Fall back to the English Language // Fall back to the English Language
$this->lang_name = 'en'; $this->lang_name = 'en';
$this->set_lang($lang, $help, $lang_file, $use_db, $use_help); $this->set_lang($lang, $help, $lang_file, $use_db, $use_help, $ext_name);
} }
else if ($this->lang_name == $this->data['user_lang']) else if ($this->lang_name == $this->data['user_lang'])
{ {
// Fall back to the board default language // Fall back to the board default language
$this->lang_name = basename($config['default_lang']); $this->lang_name = basename($config['default_lang']);
$this->set_lang($lang, $help, $lang_file, $use_db, $use_help); $this->set_lang($lang, $help, $lang_file, $use_db, $use_help, $ext_name);
} }
// Reset the lang name // Reset the lang name
$this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']); $this->lang_name = (file_exists($lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']);
return; return;
} }