SugarCRM源码分析之国际化语言


        本篇主要分析下SugarCRM中的语言包源码。

./include/entryPoints.php
SugarApplication::preLoadLanguages();

/**
 * Load only bare minimum of language that can be done before user init and MVC stuff
 */
static function preLoadLanguages()
{
    if (!empty($_SESSION['authenticated_user_language'])) {
        $GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
    } else {

        // 加载配种文件中的默认语言zh_ch
        $GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
    }

    // 记录日志,若要执行,需把配置文件中的level改为debug
    $GLOBALS['log']->debug('current_language is: ' . $GLOBALS['current_language']);

    // set module and application string arrays based upon selected language
    // 获取语言包
    $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
}

function return_application_language($language)
{

    // $app_strings、$app_list_strings存放的都是语言包的译文
    // 首次进来都为null
    global $app_strings, $app_list_strings, $sugar_config;

    // app_strings.zh_cn
    $cache_key = 'app_strings.'.$language;

    // Check for cached value
    // 第一次没有
    $cache_entry = sugar_cache_retrieve($cache_key);
    if (!empty($cache_entry)) {
        return $cache_entry;
    }

    $temp_app_strings = $app_strings;
    $temp_app_list_strings = $app_list_strings;
    $default_language = !empty($sugar_config['default_language']) ? $sugar_config['default_language'] : $language;

    $langs = array();
    if ($language != 'en_us') {
        $langs[] = 'en_us';
    }
    if ($default_language != 'en_us' && $language != $default_language) {
        $langs[] = $default_language;
    }

    /*
    Array
    (
        [0] => en_us
        [1] => zh_CN
    )
    */
    $langs[] = $language;

    $app_strings_array = array();
    foreach ($langs as $lang) {
        $app_list_strings = array();
        foreach(SugarAutoLoader::existing(
                "include/language/$lang.lang.php",
                "include/language/$lang.lang.override.php",
                "include/language/$lang.lang.php.override",
                "custom/application/Ext/Language/$lang.lang.ext.php",
                "custom/include/language/$lang.lang.php"
        ) as $file) {
            include $file;
            $GLOBALS['log']->info("Found language file: $file");
        }

        $app_strings_array[] = $app_strings;
    }

    // 把二维的$app_strings_array数组变为一维$app_strings的
    $app_strings = array();
    foreach ($app_strings_array as $app_strings_item) {
        $app_strings = sugarLangArrayMerge($app_strings, $app_strings_item);
    }

    if (!isset($app_strings)) {
        $GLOBALS['log']->fatal("Unable to load the application language strings");

        return null;
    }

    // If we are in debug mode for translating, turn on the prefix now!
    if (!empty($sugar_config['translation_string_prefix'])) {
        foreach ($app_strings as $entry_key=>$entry_value) {
            $app_strings[$entry_key] = $language.' '.$entry_value;
        }
    }
    if (isset($_SESSION['show_deleted'])) {
        $app_strings['LBL_DELETE_BUTTON'] = $app_strings['LBL_UNDELETE_BUTTON'];
        $app_strings['LBL_DELETE_BUTTON_LABEL'] = $app_strings['LBL_UNDELETE_BUTTON_LABEL'];
        $app_strings['LBL_DELETE_BUTTON_TITLE'] = $app_strings['LBL_UNDELETE_BUTTON_TITLE'];
        $app_strings['LBL_DELETE'] = $app_strings['LBL_UNDELETE'];
    }

    // 返回热键,若是没有HTTP_USER_AGENT的话alt+
    $app_strings['LBL_ALT_HOT_KEY'] = get_alt_hot_key();

    $return_value = $app_strings;
    $app_strings = $temp_app_strings;
    $app_list_strings = $temp_app_list_strings;

    // 放入缓存
    sugar_cache_put($cache_key, $return_value);

    return $return_value;
}

function sugarLangArrayMerge($gimp, $dom)
{
    if (is_array($gimp) && is_array($dom)) {
        foreach ($dom as $domKey => $domVal) {
            if (isset($gimp[$domKey])) {
                if (is_array($domVal)) {
                    $tempArr = array();
                    foreach ( $domVal as $domArrKey => $domArrVal )
                        if (!empty($domArrVal)) // SP-1818: should not overwrite if string is empty
                            $tempArr[$domArrKey] = $domArrVal;
                    foreach ( $gimp[$domKey] as $gimpArrKey => $gimpArrVal )
                        if ( !isset($tempArr[$gimpArrKey]) )
                            $tempArr[$gimpArrKey] = $gimpArrVal;
                    $domVal = $tempArr;
                }
                if (!empty($domVal)) { // SP-1818: should not overwrite if string is empty
                    $gimp[$domKey] = $domVal;
                }
            } else {
                $gimp[$domKey] = $domVal;
            }
        }
    }
    // if the passed value for gimp isn't an array, then return the $dom
    elseif (is_array($dom)) {
        return $dom;
    }

    return $gimp;
}

function get_alt_hot_key()
{
    $ua = '';
    if ( isset($_SERVER['HTTP_USER_AGENT']) )
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    $isMac = strpos($ua, 'mac') !== false;
    $isLinux = strpos($ua, 'linux') !== false;

    if (!$isMac && !$isLinux && strpos($ua, 'mozilla') !== false) {
       if (preg_match('/firefox\/(\d)?\./', $ua, $matches)) {
             return $matches[1] < 2 ? 'Alt+' : 'Alt+Shift+';
       }
    }

    return $isMac ? 'Ctrl+' : 'Alt+';
}

        代码分析完后,了解了./include/language存放的是SugarCRM通用的语言配置,也就是下载了SugarCRM之后就有的,如果想配置个性化的,那么需要在./custome/inclde/language里配置,前提是在配置文件里加上本地默认的语言选项('default_language' => 'zh_CN')。

你可能感兴趣的:(SugarCRM源码分析之国际化语言)