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')。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,感谢 索孚方科 公司的大力支持以及相关研发人员的鼎力协助,让山寨版的CE语言可能比Pro版的更贴切好用一些,故本语言也可以说攀了个粗腿。对公司相关人员快捷高效的测试,表示感谢! 其次,注意事项: 兼容6.5.X; 一般注意事项,见http://down.51cto.com/data/275922 建议中文的sugar系统修改config.php配置文件,以获得更好使用(先备份好原文 件)。 config.php为sugar配置文件,建议更改部分。 第1处: 'default_currency_iso4217' => 'CNY', //修改默认货币代码为人民币 'default_currency_name' => 'Chinese Yuan', //修改默认货币名称为元,可在 系统管理->货币 下增加汇率和其他货币 第2处: 'default_language' => 'zh_cn', //修改默认登陆为中文 第3处: 'default_locale_name_format' => 's f l', //姓名称谓 第4处: 'default_charset' => 'EUC-CN', //导入导出为GB2312格式,避免中文乱码 第5处,可不变: 'upload_dir' => 'upload/', //保持此目录或者修改上传目录,括document的文件附件存放在此目录 'upload_maxsize' => 30000000, //保持或者修改此值,修改单个文件最大上传值(默认30M) =========================================================================== 本次语言解决的一些问题: 1.在IE下的Ajax问题(模块因语言问题显示null),详见《6.4.3中文模块列表不能正确显示?寻帮助》一文http://www.sugar360.cn/forum.php ... &extra=page=1 2.已解决:模块生成器->文件-> 可用子面板->默认,点开后无内容(而英文下无此问题); 3.中文下,日历的日期与星期几可能不对应的问题; 4.更新翻译,让其更加易懂。 5.相关翻译人员:木子飞飞 (rell336@126.com),无常(Huang.jecky@gmail.com),Cheli Zhao (cheli.zhao@srforce.com), Richard Qi (richard@srforce.com)。 6.若对翻译的一些文字有建议,可直接联系我:rell336@126.com 14:52 2014-7-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值