Commit e3b8e4c0 authored by Nigel McNie's avatar Nigel McNie
Browse files

Allow users to specify paths on the filesystem to search for language packs.

If a user has checked out a language pack repository, they can add the path of it to a new configuration variable, 'langpacksearchpaths', which is an array of paths to search.

Helpfiles are not handled yet.

This can be tested by checking out the en_AR.utf8 language pack and adding it to langpacksearchpaths. You should get an option to use language 'Avast!'.
parent 5f0d1dc9
...@@ -302,11 +302,11 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo ...@@ -302,11 +302,11 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
} }
// Define the locations of language strings for this section // Define the locations of language strings for this section
$docroot = get_config('docroot'); $langstringroot = get_language_root();
$locations = array(); $langdirectory = ''; // The directory in which the language file for this string should ideally reside, if the language has implemented it
if (false === strpos($section, '.')) { if (false === strpos($section, '.')) {
$locations[] = $docroot . 'lang/'; $langdirectory = 'lang/';
} }
else { else {
$extras = plugin_types(); // more later.. $extras = plugin_types(); // more later..
...@@ -316,26 +316,23 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo ...@@ -316,26 +316,23 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
if ($tocheck == 'blocktype' && if ($tocheck == 'blocktype' &&
strpos($pluginname, '/') !== false) { // it belongs to an artefact plugin strpos($pluginname, '/') !== false) { // it belongs to an artefact plugin
$bits = explode('/', $pluginname); $bits = explode('/', $pluginname);
$locations[] = $docroot . 'artefact/' . $bits[0] . '/blocktype/' . $bits[1] . '/lang/'; $langdirectory = 'artefact/' . $bits[0] . '/blocktype/' . $bits[1] . '/lang/';
$section = 'blocktype.' . $bits[1]; $section = 'blocktype.' . $bits[1];
} }
else { else {
$locations[] = $docroot . $tocheck . '/' . $pluginname . '/lang/'; $langdirectory = $tocheck . '/' . $pluginname . '/lang/';
} }
} }
} }
} }
// First check all the normal locations for the string in the current language // First check all the normal locations for the string in the current language
foreach ($locations as $location) { $langfile = $langstringroot . $langdirectory . $lang . '/' . $section . '.php';
//if local directory not found, or particular string does not exist in local direcotry
$langfile = $location . $lang . '/' . $section . '.php';
if (is_readable($langfile)) { if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) { if ($result = get_string_from_file($identifier, $langfile)) {
return $replacefunc($result, $variables); return $replacefunc($result, $variables);
} }
} }
}
// If the preferred language was English (utf8) we can abort now // If the preferred language was English (utf8) we can abort now
// saving some checks beacuse it's the only "root" lang // saving some checks beacuse it's the only "root" lang
...@@ -344,11 +341,10 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo ...@@ -344,11 +341,10 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
} }
// Is a parent language defined? If so, try to find this string in a parent language file // Is a parent language defined? If so, try to find this string in a parent language file
foreach ($locations as $location) { $langfile = $langstringroot . 'lang/' . $lang . '/langconfig.php';
$langfile = $location . $lang . '/langconfig.php';
if (is_readable($langfile)) { if (is_readable($langfile)) {
if ($parentlang = get_string_from_file('parentlanguage', $langfile)) { if ($parentlang = get_string_from_file('parentlanguage', $langfile)) {
$langfile = $location . $parentlang . '/' . $section . '.php'; $langfile = get_language_root($parentlang) . 'lang/' . $parentlang . '/' . $section . '.php';
if (is_readable($langfile)) { if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) { if ($result = get_string_from_file($identifier, $langfile)) {
return $replacefunc($result, $variables); return $replacefunc($result, $variables);
...@@ -356,18 +352,14 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo ...@@ -356,18 +352,14 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
} }
} }
} }
}
/// Our only remaining option is to try English /// Our only remaining option is to try English
foreach ($locations as $location) { $langfile = get_config('docroot') . $langdirectory . 'en.utf8/' . $section . '.php';
//if local_en not found, or string not found in local_en
$langfile = $location . 'en.utf8/' . $section . '.php';
if (is_readable($langfile)) { if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) { if ($result = get_string_from_file($identifier, $langfile)) {
return $replacefunc($result, $variables); return $replacefunc($result, $variables);
} }
} }
}
return '[[' . $identifier . '/' . $section . ']]'; // Last resort return '[[' . $identifier . '/' . $section . ']]'; // Last resort
} }
...@@ -379,7 +371,9 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo ...@@ -379,7 +371,9 @@ function get_string_location($identifier, $section, $variables, $replacefunc='fo
*/ */
function get_languages() { function get_languages() {
$langs = array(); $langs = array();
$langbase = get_config('docroot') . 'lang/';
foreach (language_get_searchpaths() as $searchpath) {
$langbase = $searchpath . '/lang/';
if (!$langdir = opendir($langbase)) { if (!$langdir = opendir($langbase)) {
throw new SystemException('Unable to read language directory '.$langbase); throw new SystemException('Unable to read language directory '.$langbase);
} }
...@@ -392,6 +386,8 @@ function get_languages() { ...@@ -392,6 +386,8 @@ function get_languages() {
} }
} }
closedir($langdir); closedir($langdir);
}
return $langs; return $langs;
} }
...@@ -399,6 +395,57 @@ function language_installed($lang) { ...@@ -399,6 +395,57 @@ function language_installed($lang) {
return is_readable(get_config('docroot') . 'lang/' . $lang . '/langconfig.php'); return is_readable(get_config('docroot') . 'lang/' . $lang . '/langconfig.php');
} }
/**
* Returns a list of directories in which to search for language packs.
*
* This is influenced by the configuration variable 'langpacksearchpaths'
*/
function language_get_searchpaths() {
static $searchpaths = array();
if (!$searchpaths) {
$searchpaths = array(get_config('docroot'));
$langpacksearchpaths = get_config('langpacksearchpaths');
foreach ($langpacksearchpaths as $path) {
$searchpaths[] = (substr($path, -1) == '/') ? $path : "$path/";
}
}
return $searchpaths;
}
/**
* Get the directory in which the specified language pack resides.
*
* Defaults to getting the directory for the current_language() - i.e. the
* language the user is using
*
* Returns null if the language can't be found
*
* @param string $language The language to look for
*/
function get_language_root($language=null) {
static $language_root_cache = array();
if (!isset($language_root_cache[$language])) {
if ($language == null) {
$language = current_language();
}
foreach (language_get_searchpaths() as $path) {
if (is_dir("$path/lang/$language")) {
return $language_root_cache[$language] = $path;
}
}
// Oh noes, can't be found
$language_root_cache[$language] = null;
}
return $language_root_cache[$language];
}
/** /**
* Return a list of available themes * Return a list of available themes
* Need to add the theme names sometime; for now use get_string(). * Need to add the theme names sometime; for now use get_string().
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment