Ã÷»ÔÕ¾/ÍøÕ¾½Ì³Ì/ÄÚÈÝ

.NETϵͳѧϰ----Globalization & Resources

ÍøÕ¾½Ì³Ì2024-02-13 ÔĶÁ
[ÕªÒª].NETϵͳѧϰ----Globalization & Resources l Ç°ÑÔl Á˽â×ÊÔ´Îļþl ´´½¨×ÊÔ´Îļþl ÔÚ³ÌÐòÖÐʹÓÃ×ÊÔ´Îļþl ×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Êðl ²Î¿¼ Ç°ÑÔ£º...
.NETϵͳѧϰ----Globalization & Resources



l Ç°ÑÔ

l Á˽â×ÊÔ´Îļþ

l ´´½¨×ÊÔ´Îļþ

l ÔÚ³ÌÐòÖÐʹÓÃ×ÊÔ´Îļþ

l ×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Êð

l ²Î¿¼



Ç°ÑÔ£º

ÔÚѧϰÈçºÎʹÓÃ.NET×ÊÔ´ÎļþÒÔ¼°ÈçºÎ¿ª·¢World-Ready³ÌÐò֮ǰ£¬ÎÒÃÇÏÈͨ¹ýÒ»¸öÀý×ÓÀ´¿´¿´ÎªÊ²Ã´ÒªÊ¹ÓÃ×ÊÔ´Îļþ£¬ÒÔ¼°Ê¹ÓÃËüµÄºÃ´¦¡£

¼ÙÉèÒªÔÚ³ÌÐòÖиù¾Ýµ±Ç°µÄCulutreÀ´ÉèÖÃFormµÄTitleºÍLogo£º

private void Form1_Load(object sender, System.EventArgs e) {

CultureInfo ci = new CultureInfo(Thread.CurrentThread.CurrentUICulture.ToString());

switch (ci.ToString().ToLower()) {

case "zh-cn": // ÖÐÎÄ°æ±¾

this.Text=FormTitle_ZH_CN;

imgLogo.Image = new Bitmap(Application.StartupPath + "/Logo_ZH_CN.jpg");

break;

case "en-us": // Ó¢ÎÄ°æ±¾

this.Text=FormTitle_EN_US;

imgLogo.Image = new Bitmap(Application.StartupPath + "/Logo_EN_US.jpg");

break;

default: // ĬÈÏ°æ±¾

this.Text=FormTitle_Neutral;

imgLogo.Image = new Bitmap(Application.StartupPath + "/Logo_Neutral.jpg");

break;

}

}

Õâ¶Î´úÂëÓÐÁ½¸öÎÊÌ⣺

Ê×ÏÈ£¬LogoÎļþÊDZ©Â¶¸øÓû§µÄ£¬¶øÇÒÊÇÒÔÆÕͨÎļþµÄ¸ñʽ´æ´¢µÄ£¬Õâµ¼ÖÂÆäËû³ÌÐò»òÊÇÓû§ºÜÈÝÒ×ÐÞ¸ÄÕâЩÎļþ£»½ÚÊ¡Ó²ÅÌ¿Õ¼äµÄÓû§»¹¿ÉÄÜ»áÑ¡Ôñɾ³ýËü£¬ÕâЩ¶¼¿ÉÄܻᵼÖÂÓ¦ÓóÌÐò³ö´í¡£È·±£Í¼Æ¬»òÈκÎÆäËûÎļþºÍ´úÂëÔÚÒ»ÆðµÄΨһµÄ°²È«·½Ê½Êǽ«Ëü×÷Ϊ×ÊÔ´ÎļþǶÈëÔÚ³ÌÐò¼¯Öв¢¼ÓÔØ¡£

Æä´Î£¬ÕâÊÇÒ»¸öWorld-Ready³ÌÐò£¬Èç¹ûÐèҪмÓÈëÒ»¸öеÄCulture£¬Äã¿ÉÄܲ»µÃ²»¸ü¸ÄÄãµÄÔ´´úÂ룬¼ÓÈëеÄcase£¬È»ºóÖØбàÒëÀ´ÊÊӦеÄCultureµÄÐèÒª£¬Õâ¶ÔÒ»¸öWorld-Ready³ÌÐòÀ´ËµÊDz»ÏÖʵµÄ¡£¿ª·¢World-Ready³ÌÐòºÜÖØÒªµÄÒ»µã¾ÍÊÇÒª±£Ö¤³ÌÐòµÄÂß¼­½çÃæºÍ×ÊÔ´½çÃæµÄ¸ôÀë¡£ÈκÎʱºò¼ÓÈëÒ»¸öеÄCulture×ÊÔ´£¬ÎÒÃǶ¼²»Ó¦¸ÃÖØбàÒëÔ´³ÌÐò£¬Ïà·´£¬ÎÒÃÇÖ»ÐèÒª°ÑеÄ×ÊÔ´Îļþ×¼±¸ºÃ£¬È»ºó·¢²¼¸øÓû§²¢²¿ÊðÔÚºÏÊʵÄĿ¼Ï¾ͿÉÒÔÁË¡£Ó¦ÓóÌÐòÓ¦¸ÃÄܹ»¸ù¾Ý²»Í¬µÄCultureÀ´×Ô¶¯Ñ°ÕÒºÏÊʵÄ×ÊÔ´¡£


±¾ÎĵÄÄ¿µÄ¾ÍÊÇͨ¹ýʵÀýÀ´°ïÖú¶ÁÕßÁ˽âʲôÊÇResources£¬ÒÔ¼°ÈçºÎʹÓÃResourcesÀ´Ïû³ýÉÏÃæËùÌáµ½µÄÁ½¸öÎÊÌâ¡£

È«ÎÄ·ÖΪËIJ¿·Ö£º

µÚÒ»²¿·ÖÊÇһЩºÍ×ÊÔ´Ïà¹ØµÄ¸ÅÄî¡£

µÚ¶þ²¿·ÖÊÇÒ»¸öʵÀý³ÌÐò£¨ResourceGenerator£©£¬ÓÃÀ´ËµÃ÷ÈçºÎ´´½¨×ÊÔ´Îļþ¡£

µÚÈý²¿·ÖÊÇÁíÍâÒ»¸öʵÀý³ÌÐò£¨WorldAPP£©£¬ÓÃÀ´ËµÃ÷ÈçºÎÔÚ³ÌÐòÖÐʹÓÃ×ÊÔ´Îļþ

µÚËIJ¿·ÖÊǹØÓÚ×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Êð¡£·Ö±ð½éÉÜ.NETÖÐ×ÊÔ´ÎļþµÄÃüÃû·½Ê½ºÍÈçºÎÔÚWorld-Ready³ÌÐòÖÐÅäÖÃ×ÊÔ´Îļþ¡£



µÚÒ»²¿·Ö ¸ÅÄî

ÏÈÀ´Á˽âһЩ¸ÅÄ

1. ʲôÊÇ×ÊÔ´Îļþ

¹ËÃû˼Ò壬×ÊÔ´Îļþµ±È»°üº¬µÄÈ«ÊÇ×ÊÔ´¡£²»¹ý£¬Ê²Ã´ÊÇ×ÊÔ´£¿ÕâÀïËùνµÄ×ÊÔ´¾ÍÊdzÌÐòÖпÉÀûÓõÄÈκÎÊý¾Ý£¬Æ©È磺×Ö·û´®¡¢Í¼Æ¬»òÈκζþ½øÖƸñʽµÄÊý¾Ý¡£Ò»¸ö×ÊÔ´Îļþ¿ÉÒÔÓжàÖÖÓïÑÔÎÄ»¯°æ±¾£¬±ÈÈ磬һ¸öCulture.resources Îļþ¿ÉÒÔÓÐÓ¢Óï°æ¡¢¼òÌåÖÐÎÄ°æÈÕÎÄ°æµÈ¡£ResourceManager¿ÉÒÔ×Ô¶¯¸ù¾ÝCultureºÍ×ÊÔ´ÎļþÃûÀ´È·Èϵ÷ÓÃÄĸö°æ±¾¡£Ö»²»¹ý²»Í¬µÄ×ÊÔ´°æ±¾ÐèÒªÔÚÎļþÃûÖмÓÈëÓïÑÔÎÄ»¯ÐÅÏ¢£¨.resourceÎļþÓÐÒ»Ì×ÑϸñµÄÃüÃû¹æ·¶£¬²Î¿¼µÚËIJ¿·Ö£º×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Ê𣩡£


2. ×ÊÔ´ÎļþµÄÀàÐÍ

System.ResourcesÃû³Æ¿Õ¼äÖ§³ÖÈýÖÖÀàÐ͵Ä×ÊÔ´£º

.txtÎļþ£¬Ö»ÄÜÓÐ×Ö·û´®×ÊÔ´¡£ÒòΪ²»Äܱ»Ç¶Èëµ½AssemblyÖУ¬ËùÒÔºÜÈÝÒ×±©Â¶£¬±»ÆäËû³ÌÐò»òÓû§Ð޸ġ£×î´óȱµãÊǽöÖ§³Ö×Ö·û´®×ÊÔ´£¬²»ÍƼöʹÓá£

.resxÎļþ£¬ÓÉXML×é³É£¬¿ÉÒÔ¼ÓÈëÈκÎ×ÊÔ´£¬°üÀ¨¶þ½øÖƸñʽµÄ¡£Í¬Ñù²»Äܱ»Ç¶Èëµ½AssemblyÖС£ÔÚSystem.Resources Ãû³Æ¿Õ¼äÖÐÓÐרÓöÁдµÄÀà¡£VS.NETÖд´½¨µÄÕâÖÖÎļþÒ²Êǽ«Æäת³É.resources ÎļþÈ»ºó¸ù¾ÝÉèÖý«ÆäǶÈëµ½AssemblyÖС£

.resourcesÎļþ£¬PE¸ñʽ£¬¿ÉÒÔ¼ÓÈëÈκÎ×ÊÔ´¡£ÊÇΨһ¿ÉÒÔ±»Ç¶Èëµ½AssemblyµÄÎļþ£¬ÔÚSystem.ResourcesÃû³Æ¿Õ¼äÖÐÓÐרÓöÁдµÄÀࣨResourceManager£©¡£


3. µ÷ÓÃ×ÊÔ´ÎļþµÄ¼¸ÖÖ·½·¨

ResourceManager¿ÉÒÔ¸ù¾Ý²»Í¬µÄUICultureÉèÖ÷µ»Ø²»Í¬µÄ±¾µØ×ÊÔ´£¬²»Í¬CultureµÄ×ÊÔ´ÎļþÓÐÒ»Ì×ÑϸñµÄÃüÃû¹æÔò£¬Ö»Óа´ÕÕÕâ¸ö¹æÔòÃüÃû£¬CRL²Å¿ÉÒÔ¸ù¾ÝCultureÕÒµ½Õâ¸ö±¾µØ×ÊÔ´¡£PS£ºÒòΪÕâ¸öºÜÖØÒª£¬ËùÒÔ²ÅÒ»ÔÙ³öÏÖJ¡£²Î¿¼µÚËIJ¿·Ö£º×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Êð£©

.txt Îļþ£º

²»¿ÉÒÔÖ±½Óµ÷Ó㬵ÃÏȽ«Æäת»»³É .resources Îļþ²ÅÄÜʹÓá£

.resx Îļþ£º

¿ÉÒÔÓÃResXResourceReaderÀ´¶ÁÈ¡£¬µ«ÊÇÕâÖÖ·½·¨²»Ö±¹ÛÒ²²»°²È«£¬²»ÍƼöÖ±½Óµ÷ÓÃ.resxÎļþ¡£ÕýÈ·µÄ·½·¨Êǽ«Æäת»»³É.resourcesÎļþ£¬È»ºóÓÃResourceManager¶ÁÈ¡¡£×¢Ò⣬Èç¹ûÊÇÔÚVS.NETÖÐÌí¼ÓµÄ.resxÎļþ£¬ÄÇôËüÃÇ×Ô¶¯±»ÉèΪ Embedded Resource£¬È»ºó±»×ª³É.resourcesÎļþºóǶÈëµ½AssemblyÖС£

.resources Îļþ£º

·Ö³ÉÁ½ÖÖÇé¿ö£º

¡¤ ±»Ç¶Èë»ò±àÒë³ÉÎÀÐdzÌÐò¼¯£¨Satellite Assembly£©£º

ÓÃResourceManagerµÄ¸÷ÖÖconstructorÀ´»ñµÃÔÚAssemblyÖеÄ×ÊÔ´¡£

¡¤ µ¥¶ÀÎļþ£¬Ã»Óб»±àÒë»òǶÈëµ½AssemblyÖУº

¿ÉÒÔÓÃResourceManager.CreateFileBasedResourceManagerÀ´»ñµÃ×ÊÔ´¼¯£¨ResourceSet£©£¬¾ÍÊÇËùÓеÄ×ÊÔ´¡£

ÌØÊâÇé¿ö£º

»¹ÓÐÒ»ÖÖÌØÊâÇé¿ö£¬ÄǾÍÊǵ±ÄãÖ±½ÓǶÈëÒ»×ÊԴʱ£¬Ò²¾ÍÊÇ˵£¬²»Í¨¹ýÒ»¸ö×ÊÔ´Îļþ£¨.resources£©¶øÖ±½Ó½«Ò»×ÊÔ´£¨Object£©Ç¶Èëµ½ Assembly ÖС£Õâ¿ÉÒÔͨ¹ýAL.exe£¨Assembly Linker£©µÄ²ÎÊý/embed:<object>°Ñ×ÊԴǶÈëÔÚAssemblyÖС£ÔÚÕâÖÖÇé¿öÏÂResourceManager¾ÍûÓÐÓÃÁË£¬ÒòΪËüÖ»ÄÜ»ñÈ¡.resources×ÊÔ´Îļþ£¨ÔÚ»ò²»ÔÚAssemblyÖУ©¡£

µ÷ÓÃÕâÀàÖ±½ÓǶÈëÔÚAssemblyÖеÄ×ÊÔ´£¬ÎÒÃǾÍÐèÒªÀûÓÃReflectionµÄһЩÌØÐÔÀ´Íê³É¡£ÔÚSystem.Reflection.AssemblyÀàÖÐÓÐһЩÏà¹Øº¯Êý¿ÉÒÔ°ïÖúÎÒÃÇÄõ½ÕâЩ×ÊÔ´¡£Í¨¹ýAssembly.GetManifestResourceNames¿ÉÒÔÄõ½ËùÓеÄ×ÊÔ´µÄÃû×Ö£¬È»ºóÎÒÃǾͿÉÒÔͨ¹ýAssembly.GetManifestResourceStream£¨<object_name>£©Õâ¸öº¯ÊýÄõ½¶ÔÓ¦µÄ×ÊÔ´²¢ÒÔstreamµÄ·½Ê½·µ»Ø£¬È»ºóÎÒÃÇ¿ÉÒÔ½«Õâ¸östreamת³ÉÔÚ.NETÖпÉÓõĶÔÏó¡£±ÈÈ磬Èç¹ûǶÈë×ÊÔ´ÊÇһͼƬ£¬ÄÇôÎÒÃÇ¿ÉÒÔÀûÓÃNew Bitmap£¨Stream£©µÄconstructor»ñµÃÕâ¸öͼƬ×ÊÔ´µÄBitmap¶ÔÏó¡£



µÚ¶þ²¿·Ö ´´½¨×ÊÔ´Îļþ

´´½¨×ÊÔ´ÎļþÓÐÁ½ÖÖ·½Ê½£¬Ò»ÖÖÊÇʹÓÃ.NET SDK×Ô´øµÄresgen¹¤¾ßÀ´´´½¨£¬ÁíÍâÒ»ÖÖÊÇ×Ô¼ºÐ´codeÀ´´´½¨¡£·Ö±ðÀ´½éÉÜ£º

1. Resgen£º

Õâ¸ö¹¤¾ßÊÇ.NET×Ô´øµÄ£¬Ëü¿ÉÒÔ°Ñ.txt£¬.resX£¬×ª»»Îª.resourcesÎļþ¡£.resourcesÎļþÊÇÒÔÒ»ÖÖÒÔ¼ü-Öµ·½Ê½¶ÔÓ¦´æ´¢µÄXML¸ñʽÎļþ£¬Ã¿Ò»¸ö¼ü<data>¶ÔÓ¦Ò»¸öÖµ<value>£¬Õâ¸ö<value>¿ÉÒÔÊÇÈκεĶþ½øÖƸñʽ¡£Èç¹ûÊǸñʽΪ£¨¼ü=Öµ£©¶ÔÓ¦µÃ.txtÎļþ£¬resgen»á×Ô¶¯Éú³É¼ü-Öµ¶ÔÓ¦µÄXMLÎļþ¡£µ«ÊÇresgenÓÐÒ»¸ö¾ÖÏÞÐÔ£¬Ëü²»ÄÜÖ±½ÓǶÈëÆäËû¸ñʽµÄÎļþ£¬±ÈÈçÄã¾Í²»ÄÜ°Ñ.bmpÒÔ¼ü-ÖµµÃ·½Ê½¶ÔÓ¦ÆðÀ´£¬ÒòΪÄãÊ×ÏȲ»ÄܺÜÈÝÒ׵ðÑ.bmpÒÔ£¨¼ü=Öµ£©¶ÔÓ¦µÄ¸ñʽ´¢´æÔÚ.txtÎļþÖС£ËùÒÔresgenÖ÷ÒªÊÇÕë¶ÔtxtÎļþʹÓá£

Ò»¸öÀý×Ó£ºcompany1.txtÎļþÄÚÈÝΪ£º

Title = Company1

Address = Company1 Address

Phone = 12345678

----------------------------------------------------------------

Resgen company.txt <outputfilename>.resources

Èç¹û²»Ö¸¶¨<outputfilename>£¬Ä¬ÈÏ»áÉú³Écompany1.resources¡£

È»ºó¾Í¿ÉÒÔͨ¹ýResourceManagerÀ´Ê¹ÓÃÁË¡£

»¹¿ÉÒÔÔÙ½øÒ»²½£¬Í¨¹ýAL.exe°ÑresourcesÎļþ±äΪһ¸öassembly£¨Ê¹ÓÃassemblyÓкܶàºÃ´¦£¨±ÈÈç¿ÉÒÔ¼ÓÈë°æ±¾ÐÅÏ¢ºÍCultureÐÅÏ¢µÈ£©Ïê¼û£¨.NETϵͳѧϰ----Assembly£©¡£

Al /out:company1.dll /embed:company1.resources

ͨ¹ýÉèÖÃResourcesManagerµÄ²»Í¬µÄconstructor¾Í¿ÉÒÔ·ÃÎÊAssemblyÖаüº¬µÄ.resourcesÎļþ£¨ÏÂÃæµÄÀý×ӻὲµ½£©¡£


2. ͨ¹ý±à³ÌʹÓÃIResourcsWriteÀ´Éú³É×ÊÔ´Îļþ

ÉÏÃæµÄ·½·¨µÄÒ»¸ö×î´óµÄȱµãÊDz»Äܷܺ½±ãµÄǶÈëÆäËû¸ñʽµÄ×ÊÔ´£¬ÒòΪ°ÑÆäËû¸ñʽµÄ×ÊÔ´±äΪ¼ü-Öµ¶ÔÓ¦µÃtxtÎļþ²¢²»ÊÇÒ»¼þºÜÈÝÒ×µÄÊ¡£ËùÒÔÎÒÃǽéÉÜÁíÒ»ÖÖ·½·¨£¬Í¨¹ý±à³Ì£¬Ê¹ÓÃ.NETÌṩµÄIResourcesWriteÀàÀ´ÊµÏÖ°ÑÈκÎ×ÊԴǶÈëµ½resourcesÎļþÖС£



ResourceGenerator¾ÍÊÇÓÃÕâÖÖ·½Ê½ÊµÏֵġ£

³ÌÐòµÄÖ÷½çÃ棺




Óõ½µÄÖ÷Òª·½·¨¾ÍÊÇ£º

private void OnGenerateResource(object sender, System.EventArgs e)

{

IResourceWriter rw = new ResourceWriter(¡°C:\test.resources¡±);

switch (sType)

{

case "system.string":

rw.AddResource(sKey,sValue);

break;


case "system.drawing.bitmap":

Bitmap bmp = new Bitmap(sValue);

rw.AddResource(sKey,bmp);

break;


case "system.drawing.image":

Image img= new Bitmap(sValue);

rw.AddResource(sKey,img);

break;

}

}

¸ù¾Ý×ÊÔ´µÄÀàÐÍ£¬Èç¹û²»ÊÇstringÀàÐ͵ģ¬ÎÒÃǾͰÑËü·ÖʵÀý»¯ÎªÏàÓ¦µÄstream£¬È»ºó¼ÓÈëµ½resorucesÖм´¿É£¨stringÀàÐÍ¿ÉÒÔÖ±½Ó¼ÓÈ룩¡£Éú³ÉµÄ¾ÍÊÇ.NET¿ÉÒÔÖ±½ÓʹÓõÄ.resourcesÎļþ¡£µ«ÊÇÕâÑùÉú³ÉµÄ×ÊÔ´CLR²¢²»Äܸù¾Ý²»Í¬µÄCulture×Ô¶¯Ê¶±ð¡£ÒªÏëCRL×Ô¶¯Ê¶±ð²¢¼ÓÔØÕýÈ·µÄ×ÊÔ´Îļþ£¬Ê×ÏȱØÐë°Ñ.resourcesת»»ÎªAssembly£¬²¢¸ù¾ÝÑϸñµÄÃüÃû·½Ê½ÃüÃû£¨²Î¿¼µÚËIJ¿·Ö£º×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Ê𣩣¬²¢²¿Êðµ½ÕýÈ·µÄĿ¼Ï£¬È»ºóCLR¾Í¿ÉÒÔ¸ù¾Ý²»Í¬µÄCultureÀ´¼ÓÔØÕýÈ·µÄ×ÊÔ´¡£



µÚÈý²¿·Ö ÔÚ³ÌÐòÖÐʹÓÃ×ÊÔ´Îļþ

WorldApp.csÊÇÒ»¸öWorld-ReadyµÄ³ÌÐò£¬ËüµÄÂß¼­½çÃæºÍ×ÊÔ´½çÃæÊÇ·Ö¿ªµÄ£¬¿ÉÒÔʵÏÖÂß¼­½çÃæÖ»BulidÒ»´Î£¬ÔËÐÐʱ¸ù¾Ýµ±Ç°µÄCultureµ÷ÓÃÏàÓ¦µÄSatellite Assembly£¨ÎÀÐÇ×ÊÔ´³ÌÐò¼¯£©À´ÊµÏÖ±¾µØ»¯¡£Ìí¼ÓÒ»¸öеÄCulture×ÊÔ´²»ÐèÒªÖØÐÂBuildÔ´³ÌÐò£¬Ö»ÐèÒª°ÑÏàÓ¦µÄ×ÊÔ´³ÌÐò¼¯²¿Ê𵽺ÏÊʵÄĿ¼¾Í¿ÉÒÔÁË¡£


ÏÂÃæ˵Ã÷WorldAppµÄʵÏÖ·½Ê½£º

³ÌÐòÖ÷½çÃ棺




³ÌÐòÔÚÆô¶¯µÄʱºò»á¸ù¾Ýµ±Ç°µÄCurrentUICulutreÈ¥¼ÓÔØÏàÓ¦µÄ×ÊÔ´Îļþ¡£


¶ÁÈ¡×ÊÔ´ÎļþµÄ´úÂëΪ£º

private void SetCulture( CultureInfo ci )

{

// Change current UI culture

Thread.CurrentThread.CurrentUICulture = ci;


// Load culture resources.

String AssemblyPath = Application.StartupPath + "\\Culture.dll";

Assembly asm = Assembly.LoadFrom(AssemblyPath);


// ResoruceManager constructor will load different resources acording to the

// CurrentUICulture. which means, if CurrentUICulutre is "en-US", rm will load

// "Culture.en-US.resources" automaticly.

// When loaded, give the resource name only.

ResourceManager rm = new ResourceManager("Culture", asm);

// Set title, culture info and logo.

this.lblTitle.Text = rm.GetString("Title");

this.lblCulture.Text = rm.GetString("Culture");

this.lblLogo.Text = rm.GetString("LogoTitle");

this.imgLogo.Image=(Bitmap)rm.GetObject("Logo");

}


Èç¹ûµ±Ç°µÄUICulture¸Ä±ä£¬¿ÉÒÔͨ¹ýÏÔʽµ÷ÓÃSetCulture( CultureInfo ci )À´¼ÓÔØÏàÓ¦µÄCulture×ÊÔ´¡£

ÏÖÔÚÈç¹ûÎÒÃÇÓÐÁËÒ»¸öеÄCulture×ÊÔ´°æ±¾£¬ÎÒÃÇÖ»ÐèÒª°ÑËü²¿ÊðÔÚ¶ÔÓ¦µÄCultureĿ¼Ï£¬WorldApp.exe¾Í¿ÉÒÔ×Ô¶¯¼ÓÔØ£¬WorldApp.exe³ÌÐò±¾Éí²¢²»ÓÃ×öÈκθü¸Ä£¨²»ÐèÒª±àÒ룩¡£

Äã¿ÉÒÔͨ¹ýÉÏÃæÖÆ×÷µÄС¹¤¾ßResoruceGeneratorÀ´Éú³É¶ÔÓ¦²»Í¬CultureµÄ×ÊÔ´£¬È»ºó°ÑÉú³ÉµÄAssemblyÕýÈ·²¿Êð¾Í¿ÉÒÔÁË¡£WorldApp¾ÍÓÖÓÐÁËÒ»¸öеÄCulture°æ±¾¡£¹þ!!



µÚËIJ¿·Ö ×ÊÔ´ÎļþµÄÃüÃûºÍ²¿Êð

Õⲿ·Ö˵Ã÷×ÊÔ´ÎļþµÄ²¿Êð·½Ê½ºÍCLRÊÇÈçºÎʶ±ð²¢¼ÓÔز»Í¬µÄCulture×ÊÔ´µÄ¡£

¡¤ ×ÊÔ´ÎļþµÄÃüÃû·½Ê½

¼ÙÉèÎÒÃǵÄÓ¦ÓóÌÐòÃûΪWorldApp.exe£¬Ä¬ÈϵÄ×ÊÔ´ÎļþΪculture.resources£¬¸ù¾ÝÕâ¸ö×ÊÔ´ÎļþÉú³ÉµÄAssemblyΪculutre.dll£¨Õâ¸öÊÇĬÈÏ°æ±¾µÄ×ÊÔ´Îļþ£©¡£È»ºóÎÒÃÇÓÐÁËÒ»¸öen-US Culture°æ±¾µÄ×ÊÔ´Îļþ£¬Ôòen-USµÄ×ÊÔ´ÎļþµÃÃû³Æ±ØÐëΪculture.en-US.resources£¬¸ù¾ÝÕâ¸ö×ÊÔ´ÎļþÉú³ÉµÄen-US°æ±¾µÄAssembly±ØÐëÃüÃûΪculture.resources.dllÇÒ±ØÐë¼ÓÈëCultureÐÅÏ¢£¨°ÑÒ»¸ö.resourcesÉú³ÉÒ»¸öAssembly£ºresgen /out:cluture.resources.dll /c:en-US /embedcluture.en-US.resources£©£¬Éú³ÉµÄAssembly±ØÐë·ÅÔÚ³ÌÐòÔËÐÐĿ¼ÏµÄen-USĿ¼Ï£¬ÕâÑùCLR²ÅÄÜ×Ô¶¯ÕÒµ½¡£Í¬Ñù£¬Èç¹ûÎÒÃÇÓÐÁËÒ»¸özh-CN°æ±¾µÄ×ÊÔ´Îļþ£¬Ôò×ÊÔ´ÎļþµÄÃû³Æ±ØÐëΪculture.zh-CN.resources£¬Éú³ÉµÄAssembly±ØÐëΪculture.resources.dll£¬²¢·ÅÔÚzh-CNĿ¼Ï¡£

ÖØÒª£ºÒòΪÉú³ÉµÄ.resourcesÎļþ±¾Éí²¢²»°üº¬CultureÐÅÏ¢£¬ËüµÄCultureÐÅÏ¢¾ÍÌåÏÖÔÚËüµÄÎļþÃûÉÏ£¬ËùÒÔ.resourcesµÄÃüÃû±ØÐë¼ÓÈëClutureÐÅÏ¢£¨Èç¹û²»¼ÓµÄ»°£¬Éú³ÉµÄ¾ÍÊÇĬÈÏ°æ±¾£©¡£´Ó.resourcesÉú³ÉAssemblyʱ£¬ÒòΪAssembly¿ÉÒÔÖ¸¶¨CultureÐÅÏ¢£¨Í¨¹ý/c:<culture>À´Ö¸¶¨£©£¬ËùÒÔAssemblyµÄÃû³ÆÖв»ÐèÒª¼ÓÈëCultureÐÅÏ¢£¬µ«ÊÇAssemblyµÄÃû×Ö±ØÐëÊÇ£ºÄ¬ÈÏ°æ±¾Ãû+<.resources>.dll£¬¾ÍÊÇ£ºculture.[resources].dll¡£


¡¤ ×ÊÔ´ÎļþµÄ²¿Êð·½Ê½

Ó¦ÓóÌÐòÕýÈ·µÄ²¿Êð·½Ê½£¨Ä¿Â¼½á¹¹£©Ó¦¸ÃÊÇ£º


<WorldApp> £¨Ó¦ÓóÌÐòÖ÷Ŀ¼£©

WorldApp.exe £¨Ö÷³ÌÐò£©

Culture.dll £¨°üº¬culture.resources×ÊÔ´Îļþ£©

<en-US> £¨en-US×ÊԴĿ¼£©

Culture.resources.dll £¨°üº¬culture.en-US.resources×ÊÔ´Îļþ£©

<zh-CN> £¨zh-CN×ÊԴĿ¼£©

Culture.resources.dll £¨°üº¬culture.zh-CN.resources×ÊÔ´Îļþ£©

<new-Culture> £¨net-Culture×ÊԴĿ¼£©

Culture.resources.dll £¨°üº¬cluture.new-Culture.resources£©

<¡­>

ÓÐÁËÉÏÃæµÄ²¿Êð£¬App.exeÔÚÔËÐÐʱ£¬»áÊ×Ïȸùµ±Ç°ThreadµÄCurrentUICulutureµ½¶ÔÓ¦µÄĿ¼ȥѰÕÒ×ÊÔ´Îļþ£¬±ÈÈ統ǰµÄCurrentUICulture=¡±en-US¡±£¬Ôòen-USĿ¼ÏµÄCulture.resources.dll AssemblyÖеÄculture.en-US.resources»á±»¼ÓÔØ¡£Èç¹ûCLR±éÀúÕû¸öĿ¼»¹Ã»ÓÐÕÒµ½¶ÔÓ¦µÄ×ÊÔ´Îļþ£¬ÔòĬÈϵÄ×ÊÔ´Îļþ°æ±¾¾Í±»¼ÓÔØ£¨MSDNÖгÆΪHub and Spoke model·½Ê½ Ïê¼û£ºms-help://MS.MSDNQTR.2004APR.1033/cpguide/html/cpconPackagingDeployingResources.htm£©¡£


¡¤ CLRÈçºÎ¼ÓÔØ×ÊÔ´Îļþ

ÖØÒª£ºCLRÔÚÆ¥Åä×ÊÔ´ÎļþµÄʱºò£¬²»ÊÇ°´ÎļþÀ´Æ¥ÅäµÄ£¬ËüÊÇ°´ÕÕ<data>×Ö¶ÎÒ»¸ökeyÒ»¸ökeyµÄȥƥÅä¡£¾Ù¸öÀý×Ó£º

ĬÈÏ°æ±¾µÄCulture×ÊÔ´ÎļþÖаüº¬Ëĸökey£ºTitle, Culture, LogoTitle,Logo¡£

ÖÐÎÄ°æ±¾µÄCulture×ÊÔ´ÎļþÖаüº¬Ö»ÓÐÈý¸öKey£ºTitle, Culture, Logo¡££¨Ã»ÓÐLogoTitle£©

Èç¹ûµ±Ç°µÄCultureÊÇ¡±zh-CN¡±£¬Ôòzh-CN°æ±¾µÄTitle, Culture, Logo¶¼»á±»¼ÓÔØ£¬µ«ÊÇÒòΪzh-CN°æ±¾Ã»ÓÐLogoTitle£¬ËùÒÔCLR»á×Ô¶¯¼ÓÔغÍzh-CNÎÄ»¯×îÆ¥ÅäµÄÒ»¸ö×ÊÔ´°æ±¾µÄLogoTitle¡£Èç¹û¶¼Ã»ÓУ¬×îºó²Å»áÈ¥¼ÓÔØĬÈÏ°æ±¾µÄ×ÊÔ´Îļþ¡£

ÕâÑù×öÓÐÒ»¸öºÜ´óµÄºÃ´¦£º¾ÍÊÇ˵²¢²»ÊÇËùÓÐ×ÊÔ´¶¼±ØÐëÒªÓжÔÓ¦CultureµÄ°æ±¾£¬ÎÒÃÇ¿ÉÒ԰ѹ²Í¨µÄ×ÊÔ´·ÅÔÚĬÈÏ°æ±¾ÖУ¬Ö»°ÑºÍÌض¨CultureÏà¹ØµÄ×ÊÔ´¸ôÀë¾Í¿ÉÒÔÁË¡£

ÖØÒª£º¹ØÓÚCulture£º

CultureÐÅÏ¢ÊÇÓÉÖ÷±ê¼Ç£¨ÎÄ»¯£©ºÍ´Î±ê¼Ç£¨µØÓò£©Á½²¿·Ö×é³ÉµÄ¡£¾Ù¸öÀý×Ó£º

en-US £¨Ó¢Óï-ÃÀ¹ú£©

en-GB £¨Ó¢Óï-Ó¢¹ú£©

en-AU £¨Ó¢Óï-°Ä´óÀûÑÇ£©

Ö÷±ê¼ÇÊÇen£¬±íʾCulture¶¼ÊÇÓ¢ÓïÎÄ»¯£¬´Î±ê¼Ç£¨µØÓò£©Çø·ÖÁËËüÃÇ·Ö±ðÊÇÄĸöµØÇøµÄÓ¢Óï¡£

˵Õâ¸öÓÐʲôÓÃÄØ£¿

ÒòΪCLRÔÚÑ°ÕÒ×ÊÔ´µÄʱºòÊÇÒÔÒ»ÖÖ»ØÍ˵ķ½Ê½À´Ñ°Õҵģ¬¾ÍÊÇ˵£¬Ëû»áÊ×ÏÈȥѰÕÒ×îÅúµÄÄǸö×ÊÔ´Îļþ£¬Èç¹ûûÓУ¬Ôò»áËÑË÷ÎÄ»¯²ã´Î½á¹¹£¬ÒÔ²éÕÒ×î½Ó½üÓÚÇëÇóµÄÆ¥Åä×ÊÔ´Îļþ£¬²¢°ÑÉú³ÉÒì³£×÷Ϊ×îºóÒ»ÖÖÊֶΡ£±ÈÈçCLRÔÚÑ°ÕÒen-US×ÊÔ´µÄʱºòûÓÐÕÒµ½£¬CLR²»»áÁ¢¼´¾ÍÈ¥ÓÃĬÈϰ汾ƥÅ䣬¶øÊÇ»áÊ×ÏÈËÑË÷ÎÄ»¯²ã´Î½á¹¹£¬ÒÔ²éÕÒ×î½Ó½üÓÚen-USµÄ×ÊÔ´£¨¿ÉÄÜÊÇen-GB»ò±ðµÄ£©£¬Èç¹ûÕÒµ½£¬ÔËÐÐʱ¾ÍʹÓÃÕâ¸ö×ÊÔ´£¬Èç¹û»¹ÕÒ²»µ½£¬Ôò»á¼ÌÐøËÑË÷ÏÂÒ»²ã£¬×îºó²Å»áÓÃĬÈϰ汾ƥÅ䣨Èç¹ûĬÈϰ汾ҲûÓУ¬Ôò»áÅ׳öÒ»¸öÒì³££©¡£


²Î¿¼×ÊÁÏ£º

l Applied Microsoft .NET Framework Programming ---- Jeffrey Richter

l MSND Library

……

Ïà¹ØÔĶÁ