/** * Load a language database * * - Leading and trailing whitespace will be eliminated */ private function _load_language($lang) { $this->_stringdb[$lang] = array(); $filename = "{$this->_library_filename}.{$lang}.txt"; if ($GLOBALS['midcom_config']['cache_module_memcache_backend'] != 'flatfile') { $stringtable = midcom::get('cache')->memcache->get('L10N', $filename); if (is_array($stringtable)) { $this->_stringdb[$lang] = $stringtable; return; } } if (!empty(midcom::get('componentloader')->manifests[$this->_component_name]->extends)) { $parent_l10n = new self(midcom::get('componentloader')->manifests[$this->_component_name]->extends, 'default'); $this->_stringdb[$lang] = $parent_l10n->get_stringdb($lang); } if (!file_exists($filename)) { return; } $data = file($filename); // get site-specific l10n $component_locale = midcom_helper_misc::get_snippet_content_graceful($GLOBALS['midcom_config']['midcom_sgconfig_basedir'] . "/" . $this->_component_name . "/l10n/" . $lang); if (!empty($component_locale)) { $data = array_merge($data, explode("\n", $component_locale)); } // Parse the Array $stringtable = array(); $version = ''; $language = ''; $instring = false; $string_data = ''; $string_key = ''; foreach ($data as $line => $string) { // Kill any excess whitespace first. $string = trim($string); if (!$instring) { // outside of a string value if ($string == '') { // Do nothing } else { if (substr($string, 0, 3) == '---') { // this is a command if (strlen($string) < 4) { $line++; // Array is 0-indexed throw new midcom_error("L10n DB SYNTAX ERROR: An incorrect command was detected at {$filename}:{$line}"); } $pos = strpos($string, ' '); if ($pos === false) { $command = substr($string, 3); } $command = substr($string, 3, $pos - 3); switch ($command) { case '#': // Skip break; case 'VERSION': if ($version != '') { $line++; // Array is 0-indexed throw new midcom_error("L10n DB SYNTAX ERROR: A second VERSION tag has been detected at {$filename}:{$line}"); } $version = substr($string, 11); break; case 'LANGUAGE': if ($language != '') { $line++; // Array is 0-indexed throw new midcom_error("L10n DB SYNTAX ERROR: A second LANGUAGE tag has been detected at {$filename}:{$line}"); } $language = substr($string, 12); break; case 'STRING': $string_data = ''; $string_key = substr($string, 10); $instring = true; break; default: $line++; // Array is 0-indexed throw new midcom_error("L10n DB SYNTAX ERROR: Unknown command '{$command}' at {$filename}:{$line}"); } } else { $line++; // Array is 0-indexed throw new midcom_error("L10n DB SYNTAX ERROR: Invalid line at {$filename}:{$line}"); } } } else { // Within a string value if ($string == '---STRINGEND') { $instring = false; $stringtable[$string_key] = $string_data; } else { if ($string_data == '') { $string_data .= $string; } else { $string_data .= "\n{$string}"; } } } } if ($instring) { throw new midcom_error("L10n DB SYNTAX ERROR: String constant exceeds end of file."); } if (version_compare($version, $this->_version, "<")) { throw new midcom_error("L10n DB ERROR: File format version of {$filename} is too old, no update available at the moment."); } if ($lang != $language) { throw new midcom_error("L10n DB ERROR: The DB language version {$language} did not match the requested {$lang}."); } ksort($stringtable, SORT_STRING); $this->_stringdb[$lang] = array_merge($this->_stringdb[$lang], $stringtable); if ($GLOBALS['midcom_config']['cache_module_memcache_backend'] != 'flatfile') { midcom::get('cache')->memcache->put('L10N', $filename, $this->_stringdb[$lang]); } }