iT邦幫忙

0

MVC 使用 App_GlobalResources 多國語系的問題?

Luke 2018-07-18 10:02:197613 瀏覽

請問要如何使用 Resources
試不出來?
Controller

public class HomeController : Controller
{
	public ActionResult Index()
	{
		string resourceName ="Home";//Home.resx
		// HttpContext.GetGlobalResourceObject(resourceName, name, culture) as string;
		ResourceManager RM = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
		string name = RM.GetString("Name");
		return View();
	}
}

Index.cshtml

@{
    ViewBag.Title = "Home Demo";
}
<div class="jumbotron">
    <h1>I18N</h1>
    <p class="lead">@* <%Resource.Name %> *@ </p>
</div>

https://ithelp.ithome.com.tw/upload/images/20180718/20096781FzX1wEeSJT.png

這也有設定如何讓 App_GlobalResources 裡的全域資源檔變成公開類別
開啟「屬性」視窗可以發現在這個資源檔的「自訂工具」屬性為 GlobalResourceProxyGenerator

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
Luke iT邦研究生 5 級 ‧ 2018-07-18 11:49:11 檢舉

這篇看過,不過他是用 WebForm.aspx, WebForm.aspx.cs
我寫的是 index.cshtml 沒有 index.cs 檔案?
也無法用<asp:Label ID="txtContent" Text="???"/>
/images/emoticon/emoticon06.gif

Luke iT邦研究生 5 級 ‧ 2018-07-18 13:39:09 檢舉

/images/emoticon/emoticon07.gif

0
暐翰
iT邦大師 1 級 ‧ 2018-07-18 11:34:44

先上結果圖:

舉例:

建立一個Resource1.resc裡面建立一個TEST字串
接著建立兩個多國語言Resource1.zh-TW.resc

Resource1.en-US.resc

觀念:

Resource1.resc建立的時候會建立Resource1.cs檔案
想取得Resource裡面的值可以呼叫GetString(string name)方法
想要多國語言的時候可以呼叫多載GetString(string name, CultureInfo culture)
只要指定想要的國家語言 > 建立CultureInfo > 系統就會自動去找對應
Resource1.語言.resc檔案

DEMO:

static void Main(string[] args)
{
    var Test_zh_TW = Resource1.ResourceManager
        .GetString("TEST", CultureInfo.GetCultureInfo("zh-TW"));//output:測試
    var Test_defalut = Resource1.ResourceManager
        .GetString("TEST");//output:測試 ,因為我預設是zh-TW
    var Test_enUS = Resource1.ResourceManager
        .GetString("TEST", CultureInfo.GetCultureInfo("en-US"));//output:TEST 
}

附註
實際生產環境需要把用到的多國語言CultureInfo用實體Dictionary變數保存起來,取cookie方式取得client端的語言,並帶入key取得CultureInfo
接著帶入getstring的CultureInfo參數裡面。

並且建議打包成一個工具方式來使用。

簡單舉例:

    class Program
    {
        const string
            zh_tw = "zh-TW",
            en_US = "en-US";

        readonly static Dictionary<string, CultureInfo> UseCultureInfoDictionary = new Dictionary<string, CultureInfo>()
        {
            { zh_tw, CultureInfo.GetCultureInfo(zh_tw) },
            { en_US, CultureInfo.GetCultureInfo(en_US) }
        };

        enum LangugeEnum
        {
            zh_tw = 1,
            en_US = 2
        }

        static string getResource1Value(string key, LangugeEnum langugeenum)
        {
            var languekey = string.Empty;
            switch (langugeenum)
            {
                case LangugeEnum.zh_tw:
                    languekey = zh_tw;
                    break;
                case LangugeEnum.en_US:
                    languekey = en_US;
                    break;
                default:
                    return Resource1.ResourceManager.GetString(key);
            }
            return Resource1.ResourceManager.GetString(key, UseCultureInfoDictionary[languekey]);
        }

        static string getResource1Value(string key, string langugekey)
        {
            return Resource1.ResourceManager.GetString(key, UseCultureInfoDictionary[langugekey]);
        }

        static void Main(string[] args)
        {
            //取預設值方式
            var Test_defalut = Resource1.ResourceManager.GetString("TEST");//output:測試 ,因為我預設是zh-TW

            //使用工具方式+enum
            var Test_zh_TW = getResource1Value("TEST", LangugeEnum.zh_tw);//output:測試
            var Test_enUS = getResource1Value("TEST", LangugeEnum.en_US);//output:TEST 

            //使用string key方式,可以用在cookie取值帶參數
            var Test2_zh_TW = getResource1Value("TEST", "zh-TW");//output:測試
            var Test2_enUS = getResource1Value("TEST", "en-US");//output:TEST 
        }
    }
0
小魚
iT邦大師 1 級 ‧ 2018-07-18 12:38:38

我沒有用原本的方式,
我是自己寫多語系.
用兩個enum下去做,
一個enum是語系的代號,
一個是文字的代號.

當然這是寫死的,
不過一般來說也只能寫死,
不大可能讓使用者自己去改.

我要發表回答

立即登入回答