diff options
author | chris <chris@teacherscpd.co.uk> | 2005-09-08 02:02:25 +0200 |
---|---|---|
committer | chris <chris@teacherscpd.co.uk> | 2005-09-08 02:02:25 +0200 |
commit | b3a7251e1e9afb880d733cd6dfa82f8c46fcb1e3 (patch) | |
tree | 1ff8836a6f30c7feb95ee76a90cc13c3daefea04 | |
parent | e82e35266a7ac6c07c616136e5f5a6cf27432730 (diff) | |
download | rpg-b3a7251e1e9afb880d733cd6dfa82f8c46fcb1e3.tar.gz rpg-b3a7251e1e9afb880d733cd6dfa82f8c46fcb1e3.tar.bz2 |
add localisation methods to syntax plugin base class
darcs-hash:20050908000225-50fdc-abc9e505bc9510e309ae061b995bbfbfd1a97b01.gz
-rw-r--r-- | lib/plugins/syntax.php | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/lib/plugins/syntax.php b/lib/plugins/syntax.php index b8b496b25..027c85c51 100644 --- a/lib/plugins/syntax.php +++ b/lib/plugins/syntax.php @@ -17,7 +17,9 @@ require_once(DOKU_INC.'inc/parser/parser.php'); class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode { var $allowedModesSetup = false; - + var $localised = false; // set to true by setupLocale() after loading language dependent strings + var $lang = array(); // array to hold language dependent strings, best accessed via ->getLang() + /** * General Info * @@ -117,7 +119,7 @@ class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode { } /** - * There should be no need to override this function + * There should be no need to override these functions */ function accepts($mode) { @@ -136,5 +138,76 @@ class DokuWiki_Syntax_Plugin extends Doku_Parser_Mode { return parent::accepts($mode); } + // plugin introspection methods + // extract from class name, format = <plugin type>_plugin_<name>[_<component name>] + function getPluginType() { list($t) = explode('_', get_class($this), 2); return $t; } + function getPluginName() { list($t, $p, $n) = explode('_', get_class($this), 4); return $n; } + function getPluginComponent() { list($t, $p, $n, $c) = explode('_', get_class($this), 4); return (isset($c)?$c:''); } + + // localisation methods + /** + * getLang($id) + * + * use this function to access plugin language strings + * to try to minimise unnecessary loading of the strings when the plugin doesn't require them + * e.g. when info plugin is querying plugins for information about themselves. + * + * @param $id id of the string to be retrieved + * @return string string in appropriate language or english if not available + */ + function getLang($id) { + if (!$this->localised) $this->setupLocale(); + + return (isset($this->lang[$id]) ? $this->lang[$id] : ''); + } + + /** + * locale_xhtml($id) + * + * retrieve a language dependent wiki page and pass to xhtml renderer for display + * plugin equivalent of p_locale_xhtml() + * + * @param $id id of language dependent wiki page + * @return string parsed contents of the wiki page in xhtml format + */ + function locale_xhtml($id) { + return p_cached_xhtml($this->localFN($id)); + } + + /** + * localFN($id) + * prepends appropriate path for a language dependent filename + * plugin equivalent of localFN() + */ + function localFN($id) { + global $conf; + $plugin = $this->getPluginName(); + $file = DOKU_PLUGIN.$plugin.'/lang/'.$conf['lang'].'/'.$id.'.txt'; + if(!@file_exists($file)){ + //fall back to english + $file = DOKU_PLUGIN.$plugin.'/lang/en/'.$id.'.txt'; + } + return $file; + } + + /** + * setupLocale() + * reads all the plugins language dependent strings into $this->lang + * this function is automatically called by getLang() + */ + function setupLocale() { + if ($this->localised) return; + + global $conf; // definitely don't invoke "global $lang" + $path = DOKU_PLUGIN.$this->getPluginName().'/lang/'; + + // don't include once, in case several plugin components require the same language file + @include($path.'en/lang.php'); + if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php'); + + $this->lang = $lang; + $this->localised = true; + } + } //Setup VIM: ex: et ts=4 enc=utf-8 : |