Thursday, May 15, 2014

ASP.NET Localization

Today I want to cover about how to make use of resource file to create an ASP.NET web application that support language localization.

First of all, develop a web application normally with English language. For a quick one, just create a simple web page with a simple form.



Then, at this aspx page design view, click the [Tools] option in Visual Studio, then select [Generate Local Resource] to generate resource file automatically.

Note: The Generate Local Resource menu only available when the aspx designer view is focusing.



Then, you will notice a new local resource file is generated in the App_LocalResources folder. The filename is following your web page filename, and the resources are generated automatically base on the controls you have in that page.




Now, prepare another resource file for other language. For example, I want to create another resource file for Chinese language. So, just go to Solutions Explorer, then duplicate the resource file, and then rename it with this specific format: <Page Name>.aspx.<CultureInfo ISO Name>.resx.

For example: Default.aspx.zh-CN.resx.



Then, in the new resource file, replace the English characters with Chinese characters.



After you have the resource file ready, you can start coding to make the web application to load the language dynamically. The following is the sample code that change the page language base on the value from query string:

The InitializeCulture method is a method from Page object. It is used to initialize the page culture, so we have to override it and change the culture there before the ASP.NET Page Life Cycle events kick in.

protected override void InitializeCulture()
{
    base.InitializeCulture();

    if (Request.QueryString["lang"] == "cn")
    {
        //The culture info name must be same as the one named in the resource file
        //For example: zh-CN, it will refer to resource file Default.aspx.zh-CN.resx
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
        Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
    }

}


Run the application to test it out. This is what you will see originally:


Now, enter the query string to make the language change.


The following is the list of all Language ISO name that I had extracted from CultureInfo class:

af                Afrikaans
af-ZA             Afrikaans (South Africa)
am                Amharic
am-ET             Amharic (Ethiopia)
ar                Arabic
ar-AE             Arabic (U.A.E.)
ar-BH             Arabic (Bahrain)
ar-DZ             Arabic (Algeria)
ar-EG             Arabic (Egypt)
ar-IQ             Arabic (Iraq)
ar-JO             Arabic (Jordan)
ar-KW             Arabic (Kuwait)
ar-LB             Arabic (Lebanon)
ar-LY             Arabic (Libya)
ar-MA             Arabic (Morocco)
ar-OM             Arabic (Oman)
ar-QA             Arabic (Qatar)
ar-SA             Arabic (Saudi Arabia)
ar-SY             Arabic (Syria)
ar-TN             Arabic (Tunisia)
ar-YE             Arabic (Yemen)
arn               Mapudungun
arn-CL            Mapudungun (Chile)
as                Assamese
as-IN             Assamese (India)
az                Azeri
az-Cyrl           Azeri (Cyrillic)
az-Cyrl-AZ        Azeri (Cyrillic, Azerbaijan)
az-Latn           Azeri (Latin)
az-Latn-AZ        Azeri (Latin, Azerbaijan)
ba                Bashkir
ba-RU             Bashkir (Russia)
be                Belarusian
be-BY             Belarusian (Belarus)
bg                Bulgarian
bg-BG             Bulgarian (Bulgaria)
bn                Bengali
bn-BD             Bengali (Bangladesh)
bn-IN             Bengali (India)
bo                Tibetan
bo-CN             Tibetan (PRC)
br                Breton
br-FR             Breton (France)
bs                Bosnian
bs-Cyrl           Bosnian (Cyrillic)
bs-Cyrl-BA        Bosnian (Cyrillic, Bosnia and Herzegovina)
bs-Latn           Bosnian (Latin)
bs-Latn-BA        Bosnian (Latin, Bosnia and Herzegovina)
ca                Catalan
ca-ES             Catalan (Catalan)
ca-ES-valencia    Valencian (Spain)
chr               Cherokee
chr-Cher          Cherokee (Cherokee)
chr-Cher-US       Cherokee (Cherokee)
co                Corsican
co-FR             Corsican (France)
cs                Czech
cs-CZ             Czech (Czech Republic)
cy                Welsh
cy-GB             Welsh (United Kingdom)
da                Danish
da-DK             Danish (Denmark)
de                German
de-AT             German (Austria)
de-CH             German (Switzerland)
de-DE             German (Germany)
de-LI             German (Liechtenstein)
de-LU             German (Luxembourg)
dsb               Lower Sorbian
dsb-DE            Lower Sorbian (Germany)
dv                Divehi
dv-MV             Divehi (Maldives)
el                Greek
el-GR             Greek (Greece)
en                English
en-029            English (Caribbean)
en-AU             English (Australia)
en-BZ             English (Belize)
en-CA             English (Canada)
en-GB             English (United Kingdom)
en-HK             English (Hong Kong)
en-IE             English (Ireland)
en-IN             English (India)
en-JM             English (Jamaica)
en-MY             English (Malaysia)
en-NZ             English (New Zealand)
en-PH             English (Republic of the Philippines)
en-SG             English (Singapore)
en-TT             English (Trinidad and Tobago)
en-US             English (United States)
en-ZA             English (South Africa)
en-ZW             English (Zimbabwe)
es                Spanish
es-419            español (Latinoamérica)
es-AR             Spanish (Argentina)
es-BO             Spanish (Bolivia)
es-CL             Spanish (Chile)
es-CO             Spanish (Colombia)
es-CR             Spanish (Costa Rica)
es-DO             Spanish (Dominican Republic)
es-EC             Spanish (Ecuador)
es-ES             Spanish (Spain)
es-GT             Spanish (Guatemala)
es-HN             Spanish (Honduras)
es-MX             Spanish (Mexico)
es-NI             Spanish (Nicaragua)
es-PA             Spanish (Panama)
es-PE             Spanish (Peru)
es-PR             Spanish (Puerto Rico)
es-PY             Spanish (Paraguay)
es-SV             Spanish (El Salvador)
es-US             Spanish (United States)
es-UY             Spanish (Uruguay)
es-VE             Spanish (Bolivarian Republic of Venezuela)
et                Estonian
et-EE             Estonian (Estonia)
eu                Basque
eu-ES             Basque (Basque)
fa                Persian
fa-IR             Persian
ff                Fulah
ff-Latn           Fulah (Latin)
ff-Latn-SN        Fulah (Latin, Senegal)
fi                Finnish
fi-FI             Finnish (Finland)
fil               Filipino
fil-PH            Filipino (Philippines)
fo                Faroese
fo-FO             Faroese (Faroe Islands)
fr                French
fr-BE             French (Belgium)
fr-CA             French (Canada)
fr-CD             français (Congo [RDC])
fr-CH             French (Switzerland)
fr-CI             français (Côte d’Ivoire)
fr-CM             français (Cameroun)
fr-FR             French (France)
fr-HT             français (Haïti)
fr-LU             French (Luxembourg)
fr-MA             français (Maroc)
fr-MC             French (Monaco)
fr-ML             français (Mali)
fr-RE             français (Réunion)
fr-SN             français (Sénégal)
fy                Frisian
fy-NL             Frisian (Netherlands)
ga                Irish
ga-IE             Irish (Ireland)
gd                Scottish Gaelic
gd-GB             Scottish Gaelic (United Kingdom)
gl                Galician
gl-ES             Galician (Galician)
gn                Guarani
gn-PY             Guarani (Paraguái)
gsw               Alsatian
gsw-FR            Alsatian (France)
gu                Gujarati
gu-IN             Gujarati (India)
ha                Hausa
ha-Latn           Hausa (Latin)
ha-Latn-NG        Hausa (Latin, Nigeria)
haw               Hawaiian
haw-US            Hawaiian (United States)
he                Hebrew
he-IL             Hebrew (Israel)
hi                Hindi
hi-IN             Hindi (India)
hr                Croatian
hr-BA             Croatian (Latin, Bosnia and Herzegovina)
hr-HR             Croatian (Croatia)
hsb               Upper Sorbian
hsb-DE            Upper Sorbian (Germany)
hu                Hungarian
hu-HU             Hungarian (Hungary)
hy                Armenian
hy-AM             Armenian (Armenia)
id                Indonesian
id-ID             Indonesian (Indonesia)
ig                Igbo
ig-NG             Igbo (Nigeria)
ii                Yi
ii-CN             Yi (PRC)
is                Icelandic
is-IS             Icelandic (Iceland)
it                Italian
it-CH             Italian (Switzerland)
it-IT             Italian (Italy)
iu                Inuktitut
iu-Cans           Inuktitut (Syllabics)
iu-Cans-CA        Inuktitut (Syllabics, Canada)
iu-Latn           Inuktitut (Latin)
iu-Latn-CA        Inuktitut (Latin, Canada)
ja                Japanese
ja-JP             Japanese (Japan)
jv                Basa Jawa
jv-Latn           Basa Jawa
jv-Latn-ID        Basa Jawa (Indonesia)
ka                Georgian
ka-GE             Georgian (Georgia)
kk                Kazakh
kk-KZ             Kazakh (Kazakhstan)
kl                Greenlandic
kl-GL             Greenlandic (Greenland)
km                Khmer
km-KH             Khmer (Cambodia)
kn                Kannada
kn-IN             Kannada (India)
ko                Korean
ko-KR             Korean (Korea)
kok               Konkani
kok-IN            Konkani (India)
ku                Central Kurdish
ku-Arab           Central Kurdish (Arabic)
ku-Arab-IQ        Central Kurdish (Iraq)
ky                Kyrgyz
ky-KG             Kyrgyz (Kyrgyzstan)
lb                Luxembourgish
lb-LU             Luxembourgish (Luxembourg)
lo                Lao
lo-LA             Lao (Lao P.D.R.)
lt                Lithuanian
lt-LT             Lithuanian (Lithuania)
lv                Latvian
lv-LV             Latvian (Latvia)
mg                Malagasy
mg-MG             Malagasy (Madagasikara)
mi                Maori
mi-NZ             Maori (New Zealand)
mk                Macedonian (FYROM)
mk-MK             Macedonian (Former Yugoslav Republic of Macedonia)
ml                Malayalam
ml-IN             Malayalam (India)
mn                Mongolian
mn-Cyrl           Mongolian (Cyrillic)
mn-MN             Mongolian (Cyrillic, Mongolia)
mn-Mong           Mongolian (Traditional Mongolian)
mn-Mong-CN        Mongolian (Traditional Mongolian, PRC)
mn-Mong-MN        ᠮᠤᠨᠭᠭᠤᠯ ᠬᠡᠯᠡ (ᠮᠤᠨᠭᠭᠤᠯ ᠣᠯᠣᠰ)
moh               Mohawk
moh-CA            Mohawk (Mohawk)
mr                Marathi
mr-IN             Marathi (India)
ms                Malay
ms-BN             Malay (Brunei Darussalam)
ms-MY             Malay (Malaysia)
mt                Maltese
mt-MT             Maltese (Malta)
my                ဗမာ
my-MM             ဗမာ (မြန်မာ)
nb                Norwegian (Bokmål)
nb-NO             Norwegian, Bokmål (Norway)
ne                Nepali
ne-IN             नेपाली (भारत)
ne-NP             Nepali (Nepal)
nl                Dutch
nl-BE             Dutch (Belgium)
nl-NL             Dutch (Netherlands)
nn                Norwegian (Nynorsk)
nn-NO             Norwegian, Nynorsk (Norway)
no                Norwegian
nqo               ߒߞߏ
nqo-GN            ߒߞߏ (ߖߌ߬ߣߍ߬ ߞߊ߲ߓߍ߲)
nso               Sesotho sa Leboa
nso-ZA            Sesotho sa Leboa (South Africa)
oc                Occitan
oc-FR             Occitan (France)
om                Oromoo
om-ET             Oromoo (Itoophiyaa)
or                Oriya
or-IN             Oriya (India)
pa                Punjabi
pa-Arab           پنجابی
pa-Arab-PK        Punjabi (Islamic Republic of Pakistan)
pa-IN             Punjabi (India)
pl                Polish
pl-PL             Polish (Poland)
prs               Dari
prs-AF            Dari (Afghanistan)
ps                Pashto
ps-AF             Pashto (Afghanistan)
pt                Portuguese
pt-AO             português (Angola)
pt-BR             Portuguese (Brazil)
pt-PT             Portuguese (Portugal)
qut               K'iche
qut-GT            K'iche (Guatemala)
quz               Quechua
quz-BO            Quechua (Bolivia)
quz-EC            Quechua (Ecuador)
quz-PE            Quechua (Peru)
rm                Romansh
rm-CH             Romansh (Switzerland)
ro                Romanian
ro-MD             română (Republica Moldova)
ro-RO             Romanian (Romania)
ru                Russian
ru-RU             Russian (Russia)
rw                Kinyarwanda
rw-RW             Kinyarwanda (Rwanda)
sa                Sanskrit
sa-IN             Sanskrit (India)
sah               Sakha
sah-RU            Sakha (Russia)
sd                Sindhi
sd-Arab           Sindhi (Arabic)
sd-Arab-PK        Sindhi (Islamic Republic of Pakistan)
se                Sami (Northern)
se-FI             Sami, Northern (Finland)
se-NO             Sami, Northern (Norway)
se-SE             Sami, Northern (Sweden)
si                Sinhala
si-LK             Sinhala (Sri Lanka)
sk                Slovak
sk-SK             Slovak (Slovakia)
sl                Slovenian
sl-SI             Slovenian (Slovenia)
sma               Sami (Southern)
sma-NO            Sami, Southern (Norway)
sma-SE            Sami, Southern (Sweden)
smj               Sami (Lule)
smj-NO            Sami, Lule (Norway)
smj-SE            Sami, Lule (Sweden)
smn               Sami (Inari)
smn-FI            Sami, Inari (Finland)
sms               Sami (Skolt)
sms-FI            Sami, Skolt (Finland)
sn                chiShona
sn-Latn           chiShona (Latin)
sn-Latn-ZW        chiShona (Latin, Zimbabwe)
so                Soomaali
so-SO             Soomaali (Soomaaliya)
sq                Albanian
sq-AL             Albanian (Albania)
sr                Serbian
sr-Cyrl           Serbian (Cyrillic)
sr-Cyrl-BA        Serbian (Cyrillic, Bosnia and Herzegovina)
sr-Cyrl-CS        Serbian (Cyrillic, Serbia and Montenegro (Former))
sr-Cyrl-ME        Serbian (Cyrillic, Montenegro)
sr-Cyrl-RS        Serbian (Cyrillic, Serbia)
sr-Latn           Serbian (Latin)
sr-Latn-BA        Serbian (Latin, Bosnia and Herzegovina)
sr-Latn-CS        Serbian (Latin, Serbia and Montenegro (Former))
sr-Latn-ME        Serbian (Latin, Montenegro)
sr-Latn-RS        Serbian (Latin, Serbia)
st                Sesotho
st-ZA             Sesotho (South Africa)
sv                Swedish
sv-FI             Swedish (Finland)
sv-SE             Swedish (Sweden)
sw                Kiswahili
sw-KE             Kiswahili (Kenya)
syr               Syriac
syr-SY            Syriac (Syria)
ta                Tamil
ta-IN             Tamil (India)
ta-LK             Tamil (Sri Lanka)
te                Telugu
te-IN             Telugu (India)
tg                Tajik
tg-Cyrl           Tajik (Cyrillic)
tg-Cyrl-TJ        Tajik (Cyrillic, Tajikistan)
th                Thai
th-TH             Thai (Thailand)
ti                Tigrinya
ti-ER             Tigrinya (Eritrea)
ti-ET             Tigrinya (Ethiopia)
tk                Turkmen
tk-TM             Turkmen (Turkmenistan)
tn                Setswana
tn-BW             Setswana (Botswana)
tn-ZA             Setswana (South Africa)
tr                Turkish
tr-TR             Turkish (Turkey)
ts                Xitsonga
ts-ZA             Xitsonga (South Africa)
tt                Tatar
tt-RU             Tatar (Russia)
tzm               Tamazight
tzm-Latn          Tamazight (Latin)
tzm-Latn-DZ       Tamazight (Latin, Algeria)
tzm-Tfng          Tamazight (Tifinagh)
tzm-Tfng-MA       Central Atlas Tamazight (Tifinagh, Morocco)
ug                Uyghur
ug-CN             Uyghur (PRC)
uk                Ukrainian
uk-UA             Ukrainian (Ukraine)
ur                Urdu
ur-IN             اردو (بھارت)
ur-PK             Urdu (Islamic Republic of Pakistan)
uz                Uzbek
uz-Cyrl           Uzbek (Cyrillic)
uz-Cyrl-UZ        Uzbek (Cyrillic, Uzbekistan)
uz-Latn           Uzbek (Latin)
uz-Latn-UZ        Uzbek (Latin, Uzbekistan)
vi                Vietnamese
vi-VN             Vietnamese (Vietnam)
wo                Wolof
wo-SN             Wolof (Senegal)
xh                isiXhosa
xh-ZA             isiXhosa (South Africa)
yo                Yoruba
yo-NG             Yoruba (Nigeria)
zgh               Standard Morrocan Tamazight
zgh-Tfng          ⵜⴰⵎⴰⵣⵉⵖⵜ
zgh-Tfng-MA       ⵜⴰⵎⴰⵣⵉⵖⵜ (ⵍⵎⵖⵔⵉⴱ)
zh                Chinese
zh-CN             Chinese (Simplified, PRC)
zh-Hans           Chinese (Simplified)
zh-Hant           Chinese (Traditional)
zh-HK             Chinese (Traditional, Hong Kong S.A.R.)
zh-MO             Chinese (Traditional, Macao S.A.R.)
zh-SG             Chinese (Simplified, Singapore)
zh-TW             Chinese (Traditional, Taiwan)
zu                isiZulu
zu-ZA             isiZulu (South Africa)
zh-CHS            Chinese (Simplified) Legacy
zh-CHT            Chinese (Traditional) Legacy



5 comments:

  1. Seng, I want to recommend you a tool for managing the localization of language files (including .resx).
    It's https://poeditor.com/
    It's simple to use and good for translating collaboratively, as well as crowdsourcing translations.
    Your projects' workflow can be automated with the use of the POEditor API and with its translation memory.
    Regards

    ReplyDelete
  2. This was really an informative post. Thank you for sharing your expertise with us.
    We also have a blog related to the same field of information.
    Do have a look App localization service

    ReplyDelete
  3. I really want to thank you for such a nice blog post that helped me to understand why it is important. Best ASP.net Development Service

    ReplyDelete

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...