Beispiel #1
0
        public BaseController(IServiceProvider serviceProvider, IHostingEnvironment hostingEnvironment)
        {
            _locker = new object();
            _languageReceiveAdapter = serviceProvider.GetService <ILanguageReceiveAdapter>();

            _hostingEnvironment = hostingEnvironment;
            // 在这里,我默认语言文件为{语言标识符}.mo,如en-US.mo,依此类推
            _contentRootPath = _hostingEnvironment.WebRootPath + @"\mofile";
            _languageManager = _languageReceiveAdapter.GetLanguageManager();

            // 检测语言管理器是否初始化
            if (_languageManager == null)
            {
                lock (_locker)
                {
                    if (_languageManager == null)
                    {
                        List <SingleLanguage> singleLanguages = CreateSingleLanguageList(_contentRootPath);
                        _languageReceiveAdapter.InitLanguageManager(singleLanguages);

                        _languageManager = _languageReceiveAdapter.GetLanguageManager();
                        _languageManager.SetDefaultLanguage("zh-CN");
                    }
                }
            }

            _currentLanguage = _languageManager.DefaultLanguage;
        }
Beispiel #2
0
        public void MultiLanguageTest()
        {
            string webRootPath = _hostingEnvironment;

            var langUSFilePath = webRootPath + @"\en-US.mo";
            var langCNFilePath = webRootPath + @"\zh-CN.mo";

            string cultureCode = "en-US";
            string msgid       = "仅自选时用户地址不能为空";

            List <string> parms = new List <string>();

            parms.Add("test1");
            parms.Add("test2");

            SingleLanguage langEn = new SingleLanguage("en-US", langUSFilePath, null);
            SingleLanguage langZh = new SingleLanguage("zh-CN", langCNFilePath, null);

            List <SingleLanguage> langs = new List <SingleLanguage>();

            langs.Add(langEn);
            langs.Add(langZh);

            _adpter.InitLanguageManager(langs);

            var manager = _adpter.GetLanguageManager();
            var result  = manager.GetTranslate(cultureCode, msgid);

            msgid = "paramsErrorResult";


            result = manager.GetTranslate(cultureCode, msgid, parms);



            Assert.Equal(result.Result, "Incorrect Parameter 'test1'.");

            List <string> cultureCodes = new List <string>();
            List <string> Msgid        = new List <string>();

            cultureCodes.Add("zh-CN");
            cultureCodes.Add("en-US");

            Msgid.Add("propertyManageName");
            Msgid.Add("propertyManageArr1");
            Msgid.Add("propertyManageArr2");


            var resultPartLoad = manager.GetLanguageShowDataParts(cultureCodes, Msgid, false);

            Assert.Equal(resultPartLoad.Count.ToString(), "2");
        }