Skip to content

Commit

Permalink
moved language loading to the loader
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed May 12, 2018
1 parent 077c27b commit 5675a07
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 64 deletions.
59 changes: 1 addition & 58 deletions lib/plugins/config/admin.php
Expand Up @@ -213,67 +213,10 @@ function _setting_natural_comparison($a, $b) {
* @param bool $prompts
*/
public function setupLocale($prompts = false) {

parent::setupLocale();
if(!$prompts || $this->_localised_prompts) return;

$this->_setup_localised_plugin_prompts();
$this->configuration->getLangs();
$this->_localised_prompts = true;

}

/**
* @return bool
* @fixme this should be done by the loader
*/
protected function _setup_localised_plugin_prompts() {
global $conf;

$langfile = '/lang/' . $conf['lang'] . '/settings.php';
$enlangfile = '/lang/en/settings.php';

if($dh = opendir(DOKU_PLUGIN)) {
while(false !== ($plugin = readdir($dh))) {
if($plugin == '.' || $plugin == '..' || $plugin == 'tmp' || $plugin == 'config') continue;
if(is_file(DOKU_PLUGIN . $plugin)) continue;

if(file_exists(DOKU_PLUGIN . $plugin . $enlangfile)) {
$lang = array();
@include(DOKU_PLUGIN . $plugin . $enlangfile);
if($conf['lang'] != 'en') @include(DOKU_PLUGIN . $plugin . $langfile);
foreach($lang as $key => $value) {
$this->lang['plugin' . Configuration::KEYMARKER . $plugin . Configuration::KEYMARKER . $key] = $value;
}
}

// fill in the plugin name if missing (should exist for plugins with settings)
if(!isset($this->lang['plugin' . Configuration::KEYMARKER . $plugin . Configuration::KEYMARKER . 'plugin_settings_name'])) {
$this->lang['plugin' . Configuration::KEYMARKER . $plugin . Configuration::KEYMARKER . 'plugin_settings_name'] =
ucwords(str_replace('_', ' ', $plugin));
}
}
closedir($dh);
}

// the same for the active template
$tpl = $conf['template'];

if(file_exists(tpl_incdir() . $enlangfile)) {
$lang = array();
@include(tpl_incdir() . $enlangfile);
if($conf['lang'] != 'en') @include(tpl_incdir() . $langfile);
foreach($lang as $key => $value) {
$this->lang['tpl' . Configuration::KEYMARKER . $tpl . Configuration::KEYMARKER . $key] = $value;
}
}

// fill in the template name if missing (should exist for templates with settings)
if(!isset($this->lang['tpl' . Configuration::KEYMARKER . $tpl . Configuration::KEYMARKER . 'tpl_settings_name'])) {
$this->lang['tpl' . Configuration::KEYMARKER . $tpl . Configuration::KEYMARKER . 'tpl_settings_name'] =
ucwords(str_replace('_', ' ', $tpl));
}

return true;
}

/**
Expand Down
23 changes: 17 additions & 6 deletions lib/plugins/config/core/Configuration.php
Expand Up @@ -3,7 +3,7 @@
namespace dokuwiki\plugin\config\core;

/**
* Holds all the current settings
* Holds all the current settings and proxies the Loader and Writer
*
* @author Chris Smith <chris@jalakai.co.uk>
* @author Ben Coburn <btcoburn@silicodon.net>
Expand Down Expand Up @@ -32,20 +32,22 @@ class Configuration {
/** @var bool have the settings been changed since loading from disk? */
protected $changed = false;

/** @var Loader */
protected $loader;
/** @var Writer */
protected $writer;

/**
* ConfigSettings constructor.
*/
public function __construct() {
$loader = new Loader(new ConfigParser());
$this->loader = new Loader(new ConfigParser());
$this->writer = new Writer();

$this->metadata = $loader->loadMeta();
$this->default = $loader->loadDefaults();
$this->local = $loader->loadLocal();
$this->protected = $loader->loadProtected();
$this->metadata = $this->loader->loadMeta();
$this->default = $this->loader->loadDefaults();
$this->local = $this->loader->loadLocal();
$this->protected = $this->loader->loadProtected();

$this->initSettings();
}
Expand Down Expand Up @@ -133,6 +135,15 @@ public function touch() {
$this->writer->touch();
}

/**
* Load the extension language strings
*
* @return array
*/
public function getLangs() {
return $this->loader->loadLangs();
}

/**
* Initalizes the $settings and $undefined properties
*/
Expand Down
69 changes: 69 additions & 0 deletions lib/plugins/config/core/Loader.php
Expand Up @@ -102,6 +102,41 @@ public function loadDefaults() {
return $conf;
}

/**
* Reads the language strings
*
* Only reads extensions, main one is loaded the usual way
*
* @return array
*/
public function loadLangs() {
$lang = array();

// plugins
foreach($this->plugins as $plugin) {
array_merge(
$lang,
$this->loadExtensionLang(
DOKU_PLUGIN . $plugin . '/',
'plugin',
$plugin
)
);
}

// current template
array_merge(
$lang,
$this->loadExtensionConf(
tpl_incdir() . '/',
'tpl',
$this->template
)
);

return $lang;
}

/**
* Read the local settings
*
Expand Down Expand Up @@ -192,4 +227,38 @@ protected function loadExtensionConf($file, $type, $extname) {

return $data;
}

/**
* Read the language file of an extension
*
* @param string $dir directory of the extension
* @param string $type should be 'plugin' or 'tpl'
* @param string $extname name of the extension
* @return array
*/
protected function loadExtensionLang($dir, $type, $extname) {
global $conf;
$ll = $conf['lang'];
$prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;

// include files
$lang = array();
if(file_exists($dir . 'lang/en/settings.php')) {
include $dir . 'lang/en/settings.php';
}
if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
include $dir . 'lang/' . $ll . '/settings.php';
}

// set up correct keys
$strings = array();
foreach($lang as $key => $val) {
$strings[$prefix . $key] = $val;
}

// add fieldset key
$strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $type));

return $strings;
}
}

0 comments on commit 5675a07

Please sign in to comment.