多语言切换小例子,很实用

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LanguageTest
{
    class Program
    {
        static void Main(string[] args)
        {
            bool stop = false;
            while(!stop)
            {
                Console.WriteLine("请输入系统语种,如 en, cn");
                var lang = Console.ReadLine();
                SetCulture(lang);
                Console.WriteLine(Resource.String1);
                Console.WriteLine("是否退出? y=退出 n=继续");
                stop = Console.ReadLine() == "y";
            }
        }

        public static void SetCulture(string languageId)
        {
            //语言转换
            if (languageId == "cn")
                languageId = "zh-cn";
            else if (languageId == "jp")
                languageId = "ja-jp";
            else if (languageId == "hant")
                languageId = "zh-hant";
            else
                languageId = "en-us";

            var info = new CultureInfo(languageId);
            Thread.CurrentThread.CurrentCulture = info;
            Thread.CurrentThread.CurrentUICulture = info;
        }
    }
}

注意一点,资源文件必须要有一个默认语言的资源文件,如上面的Resource.resx,不能都写上语言名称,必须留一个不写语言名称的。

posted on 2019-05-10 17:16  itjeff  阅读(204)  评论(0编辑  收藏  举报

导航