protected static function replace_callback($langblock)
 {
     global $CFG;
     static $parentcache;
     if (!isset($parentcache)) {
         $parentcache = array();
     }
     $mylang = current_language();
     if (!array_key_exists($mylang, $parentcache)) {
         $parentlang = get_parent_language($mylang);
         $parentcache[$mylang] = $parentlang;
     } else {
         $parentlang = $parentcache[$mylang];
     }
     $blocklang = trim(core_text::strtolower($langblock[1]));
     $blocktext = $langblock[2];
     if ($mylang === $blocklang || $parentlang === $blocklang) {
         return $blocktext;
     }
     return '';
 }
Example #2
0
function filter_multilang_impl($langblock)
{
    global $CFG;
    $mylang = current_language();
    static $parentcache;
    if (!isset($parentcache)) {
        $parentcache = array();
    }
    if (!array_key_exists($mylang, $parentcache)) {
        $parentlang = get_parent_language($mylang);
        $parentcache[$mylang] = $parentlang;
    } else {
        $parentlang = $parentcache[$mylang];
    }
    $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\\/(?:lang|span)>/is';
    if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
        //skip malformed blocks
        return $langblock[0];
    }
    $langlist = array();
    foreach ($rawlanglist[1] as $index => $lang) {
        $lang = str_replace('-', '_', strtolower($lang));
        // normalize languages
        $langlist[$lang] = $rawlanglist[2][$index];
    }
    if (array_key_exists($mylang, $langlist)) {
        return $langlist[$mylang];
    } else {
        if (array_key_exists($parentlang, $langlist)) {
            return $langlist[$parentlang];
        } else {
            $first = array_shift($langlist);
            return $first;
        }
    }
}
Example #3
0
    if (file_exists($CFG->dataroot . '/lang/' . $lang . '_utf8') or file_exists($CFG->dirroot . '/lang/' . $lang . '_utf8')) {
        $lang = $lang . '_utf8';
    } else {
        $lang = 'en_utf8';
    }
}
// load english defaults
$string = array();
foreach (get_langpack_locations('en_utf8') as $location) {
    if (!file_exists($location)) {
        continue;
    }
    include $location;
}
// find parent language
if ($parent = get_parent_language($lang)) {
    foreach (get_langpack_locations($parent) as $location) {
        if (!file_exists($location)) {
            continue;
        }
        include $location;
    }
}
// load wanted language
if ($lang !== 'en_utf8') {
    foreach (get_langpack_locations($lang) as $location) {
        if (!file_exists($location)) {
            continue;
        }
        include $location;
    }
Example #4
0
        }
    }
    if ($updated) {
        $notice_ok[] = get_string('langupdatecomplete', 'tool_langimport');
    } else {
        $notice_ok[] = get_string('nolangupdateneeded', 'tool_langimport');
    }
    unset($installer);
}
get_string_manager()->reset_caches();
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('langimport', 'tool_langimport'));
$installedlangs = get_string_manager()->get_list_of_translations(true);
$missingparents = array();
foreach ($installedlangs as $installedlang => $unused) {
    $parent = get_parent_language($installedlang);
    if (empty($parent)) {
        continue;
    }
    if (!isset($installedlangs[$parent])) {
        $missingparents[$installedlang] = $parent;
    }
}
$installer = new lang_installer();
if ($availablelangs = $installer->get_remote_list_of_languages()) {
    $remote = true;
} else {
    $remote = false;
    $availablelangs = array();
    echo $OUTPUT->box_start();
    print_string('remotelangnotavailable', 'tool_langimport', $CFG->dataroot . '/lang/');
 /**
  * Returns a parent language of the given installed language
  *
  * @param string $langcode
  * @return string parent language's code
  */
 protected function get_parent_language($langcode)
 {
     return get_parent_language($langcode);
 }
Example #6
0
/**
 * includes a language file or returns the content of a language file
 *
 * tries to load in descending order:
 *   param language, user language, default language
 *
 * @param string $filename
 * @param string $dirname
 * @param mixed options can contain
 *     @option string language - language to load
 *     @option bool return - if true the file content is returned
 *     @option bool no_fallback - if true do not load default language
 *     @option bool|string force_fallback - force pre-loading of another language
 *        default language if *true* or specified language
 *     @option bool local - if true load file from local directory
 * @return boolean|string
 */
function load_language($filename, $dirname = '', $options = array())
{
    global $user, $language_files;
    // keep trace of plugins loaded files for switch_lang_to() function
    if (!empty($dirname) && !empty($filename) && !@$options['return'] && !isset($language_files[$dirname][$filename])) {
        $language_files[$dirname][$filename] = $options;
    }
    if (!@$options['return']) {
        $filename .= '.php';
    }
    if (empty($dirname)) {
        $dirname = PHPWG_ROOT_PATH;
    }
    $dirname .= 'language/';
    $default_language = defined('PHPWG_INSTALLED') and !defined('UPGRADES_PATH') ? get_default_language() : PHPWG_DEFAULT_LANGUAGE;
    // construct list of potential languages
    $languages = array();
    if (!empty($options['language'])) {
        // explicit language
        $languages[] = $options['language'];
    }
    if (!empty($user['language'])) {
        // use language
        $languages[] = $user['language'];
    }
    if (($parent = get_parent_language()) != null) {
        // parent language
        // this is only for when the "child" language is missing
        $languages[] = $parent;
    }
    if (isset($options['force_fallback'])) {
        // fallback language
        // this is only for when the main language is missing
        if ($options['force_fallback'] === true) {
            $options['force_fallback'] = $default_language;
        }
        $languages[] = $options['force_fallback'];
    }
    if (!@$options['no_fallback']) {
        // default language
        $languages[] = $default_language;
    }
    $languages = array_unique($languages);
    // find first existing
    $source_file = '';
    $selected_language = '';
    foreach ($languages as $language) {
        $f = @$options['local'] ? $dirname . $language . '.' . $filename : $dirname . $language . '/' . $filename;
        if (file_exists($f)) {
            $selected_language = $language;
            $source_file = $f;
            break;
        }
    }
    if (!empty($source_file)) {
        if (!@$options['return']) {
            // load forced fallback
            if (isset($options['force_fallback']) && $options['force_fallback'] != $selected_language) {
                @(include str_replace($selected_language, $options['force_fallback'], $source_file));
            }
            // load language content
            @(include $source_file);
            $load_lang = @$lang;
            $load_lang_info = @$lang_info;
            // access already existing values
            global $lang, $lang_info;
            if (!isset($lang)) {
                $lang = array();
            }
            if (!isset($lang_info)) {
                $lang_info = array();
            }
            // load parent language content directly in global
            if (!empty($load_lang_info['parent'])) {
                $parent_language = $load_lang_info['parent'];
            } else {
                if (!empty($lang_info['parent'])) {
                    $parent_language = $lang_info['parent'];
                } else {
                    $parent_language = null;
                }
            }
            if (!empty($parent_language) && $parent_language != $selected_language) {
                @(include str_replace($selected_language, $parent_language, $source_file));
            }
            // merge contents
            $lang = array_merge($lang, (array) $load_lang);
            $lang_info = array_merge($lang_info, (array) $load_lang_info);
            return true;
        } else {
            $content = @file_get_contents($source_file);
            //Note: target charset is always utf-8 $content = convert_charset($content, 'utf-8', $target_charset);
            return $content;
        }
    }
    return false;
}
Example #7
0
}
//download lang pack with optional notification
if ($CFG->lang != 'en') {
    if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang . '.zip', 'languages.md5', 'lang')) {
        if ($cd->install() == COMPONENT_ERROR) {
            if ($cd->get_error() == 'remotedownloaderror') {
                $a = new stdClass();
                $a->url = 'http://download.moodle.org/langpack/2.0/' . $CFG->lang . '.zip';
                $a->dest = $CFG->dataroot . '/lang';
                cli_problem(get_string($cd->get_error(), 'error', $a));
            } else {
                cli_problem(get_string($cd->get_error(), 'error'));
            }
        } else {
            // install parent lang if defined
            if ($parentlang = get_parent_language()) {
                if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang . '.zip', 'languages.md5', 'lang')) {
                    $cd->install();
                }
            }
        }
    }
}
// switch the string_manager instance to stop using install/lang/
$CFG->early_install_lang = false;
$CFG->langotherroot = $CFG->dataroot . '/lang';
$CFG->langlocalroot = $CFG->dataroot . '/lang';
get_string_manager(true);
// make sure we are installing stable release or require a confirmation
if (isset($maturity)) {
    if ($maturity < MATURITY_STABLE and !$options['allow-unstable']) {
Example #8
0
/**
 * Try to upgrade the given language pack (or current language)
 * @global object
 */
function upgrade_language_pack($lang = '')
{
    global $CFG, $OUTPUT;
    if (empty($lang)) {
        $lang = current_language();
    }
    if ($lang == 'en_utf8') {
        return true;
        // Nothing to do
    }
    upgrade_started(false);
    echo $OUTPUT->heading(get_string('langimport', 'admin') . ': ' . $lang);
    @mkdir($CFG->dataroot . '/temp/');
    //make it in case it's a fresh install, it might not be there
    @mkdir($CFG->dataroot . '/lang/');
    require_once $CFG->libdir . '/componentlib.class.php';
    if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang . '.zip', 'languages.md5', 'lang')) {
        $status = $cd->install();
        //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
        if ($status == COMPONENT_INSTALLED) {
            @unlink($CFG->dataroot . '/cache/languages');
            if ($parentlang = get_parent_language($lang)) {
                if ($cd = new component_installer('http://download.moodle.org', 'lang16', $parentlang . '.zip', 'languages.md5', 'lang')) {
                    $cd->install();
                }
            }
            echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
        }
    }
    print_upgrade_separator();
}
Example #9
0
/**
 * Returns a list of currencies in the current language
 *
 * @global object
 * @global object
 * @return array
 */
function get_list_of_currencies()
{
    global $CFG, $USER;
    $lang = current_language();
    if (!file_exists($CFG->dataroot . '/lang/' . $lang . '/currencies.php')) {
        if ($parentlang = get_parent_language()) {
            if (file_exists($CFG->dataroot . '/lang/' . $parentlang . '/currencies.php')) {
                $lang = $parentlang;
            } else {
                $lang = 'en_utf8';
                // currencies.php must exist in this pack
            }
        } else {
            $lang = 'en_utf8';
            // currencies.php must exist in this pack
        }
    }
    if (file_exists($CFG->dataroot . '/lang/' . $lang . '/currencies.php')) {
        include_once $CFG->dataroot . '/lang/' . $lang . '/currencies.php';
    } else {
        //if en_utf8 is not installed in dataroot
        include_once $CFG->dirroot . '/lang/' . $lang . '/currencies.php';
    }
    if (!empty($string)) {
        asort($string);
    }
    return $string;
}
 public function installLangPack($pack)
 {
     global $CFG;
     require_once $CFG->libdir . '/adminlib.php';
     require_once $CFG->libdir . '/filelib.php';
     require_once $CFG->libdir . '/componentlib.class.php';
     $thisversion = '2.0';
     // TODO this information should be taken from version.php or similar source
     make_upload_directory('lang');
     if (is_array($pack)) {
         $packs = $pack;
     } else {
         $packs = array($pack);
     }
     foreach ($packs as $pack) {
         if ($cd = new component_installer('http://download.moodle.org', 'langpack/' . $thisversion, $pack . '.zip', 'languages.md5', 'lang')) {
             $status = $cd->install();
             switch ($status) {
                 case COMPONENT_ERROR:
                     if ($cd->get_error() == 'remotedownloaderror') {
                         $a = new stdClass();
                         $a->url = 'http://download.moodle.org/langpack/' . $thisversion . '/' . $pack . '.zip';
                         $a->dest = $CFG->dataroot . '/lang';
                         print_error($cd->get_error(), 'error', 'langimport.php', $a);
                     } else {
                         print_error($cd->get_error(), 'error', 'langimport.php');
                     }
                     break;
                 case COMPONENT_INSTALLED:
                     if ($parentlang = get_parent_language($pack)) {
                         // install also parent pack if specified
                         if ($cd = new component_installer('http://download.moodle.org', 'langpack/' . $thisversion, $parentlang . '.zip', 'languages.md5', 'lang')) {
                             $cd->install();
                         }
                     }
                     break;
             }
         } else {
             echo $OUTPUT->notification('Had an unspecified error with the component installer, sorry.');
         }
     }
 }