惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 灵魂边缘

Detecting the File Download Dialog In the Browser The IE 'non-disappearing content' bug 10个强大的Javascript表单验证插件推荐 使用javascript检测客户端是否是mobile的类型,如果是就跳转到另外一个页面 在javascript中对一个对象数组进行排序 Devenv /ResetSkipPkgs - 灵魂边缘 Invalid Viewstate error and how to fix it? 在asp.net中如何管理cache 用Javascript取得URL参数(Get URL parameters/query string using Javascript) - 灵魂边缘 用JavaScript对表格排序 - 灵魂边缘 使用 jQuery 简化 Ajax 开发 Generating change scripts in SQL Server 2005 Management Studio Redirecting to custom 401 page when "Access denied" occures within an ASP.NET application - 灵魂边缘 .net中完成海量数据的批量插入和更新 - 灵魂边缘 - 博客园 存储过程跨系统跨数据库操作 - 灵魂边缘 ASP.NET 2.0 中配合Master Page 使用CSS - 灵魂边缘 正则表达式全部符号解释 - 灵魂边缘 - 博客园 [转]asp.net导出数据到Excel的三种方法 ASP.NET:创建Linked ValidationSummary, 深入理解ASP.NET的Validation (转)
Localization/Globalization in ASP.Net 2.0
灵魂边缘 · 2009-10-14 · via 博客园 - 灵魂边缘

There are many tutorials that explain you about localization/globalization concept. In this article I just tryed to explain every single point from the developer/programmer point of view.

Let me brief you about Resource files.

"aspx.resx" (resource)files:  Resource files are xml files, which stores string, file paths (image), which are required to translate in to other languages.

"App_GlobalResources": You can keep files, which need to be accessed throughout the application. So in our application I have kept "TextDirect" & "EmailFormat" which required to access in maximum pages. In short common strings.

"App_LocalResources": Local resources are associated with the single web pages, In this folder I have kept lables and strings which are specific to particualt aspx page it self.

Note: Since I am showing this in English and Arabic. So every aspx page will have two different resource files.

For Example:

I have "Registration.aspx" page, for this page I had made files as follows:

  1. Registration.aspx.ar.resx (for arabic content to be stored).
  2. Registration.aspx.resx (for english content to be stored).

The above files are local, now let us consider files which can be accessed globally as.

  1. MulResource.resx
  2. MulResource.ar.resx

These files are used for storing "EmailFormat" and "TextDirection" which is common for most of the pages.

To create a resource file manually:

Make sure that your Web site has a folder in which to store the resource file by doing one of the following:

If you are creating a global resource file, you must have a folder named App_GlobalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_GlobalResources Folder. There can only be one of these folders in an application, and it must be located at the root of the application.

If you are creating a local resource file, you must have a folder named App_LocalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_LocalResources Folder. There can be many of these folders in an application, and they can be located at any level in the application.

To create a resource file, right-click the App_GlobalResources or App_LocalResources folder, and then click Add New Item.

Global resource files must be in the App_GlobalResources folder. If you try to create a .resx file outside of this folder, Visual Web Developer prompts you to create it in the folder.

Globalization:It is process by which you can develop application/program so that it can be used by the various regions/cultures. Let us consider that you have made a product called as "Shopping Cart" and you want to sell it to different regions, since it is online say Arabic people should able to read the info in Arabic & English people should read it in English. To achieve this we have to use something called as localization

Localization:It is process of using specific regional/cultural info so that your program uses local language.

Culture:Every region has different language and language depends on different geographical location.

For example: "ar-EG" ar is the code for arabic language & EG means this language is spoken in Egypt country/region so "ar-EG" sets for Arabic-Egyption. Similarly "en-US" stands for English-United State.

Note: In IE the user can change the culture by going to Internet Options->General->Languages. For this to work, we need to set both the Culture and the UICulture to auto and enableClientBasedCulture = true

Setting Page Culture:

You can set the page culture by two ways:

  1. <globalization enableClientBasedCulture="true" culture="ar-EG" uiCulture="ar-EG"/>
  1. Setting culture by programmatically.

Protected Overrides Sub InitializeCulture()

          'string culture = Request.Form["ddSelLanguage"];

          Dim culture As String = Session("Language").ToString()

          If String.IsNullOrEmpty(culture) Then

                   culture = "Auto"

          End If

          'Use this

          UICulture = culture

          Culture = culture

          'OR This

          If culture <> "Auto" Then

                   Dim MyCltr As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(culture)

                   System.Threading.Thread.CurrentThread.CurrentCulture = MyCltr

                   System.Threading.Thread.CurrentThread.CurrentUICulture = MyCltr

          End If

          MyBase.InitializeCulture()

End Sub

Code:

In "Default.aspx" file I have a drop-down by which user can select specific language, I have Arabic and English languages and by default "Auto" is set. I have used Auto-postback property of the drop-down & even you can make use of submit button for the same.

Once you got the selected language you can hold that value in the session as explained in the above code.

Session["Language"] = Request.Form["ddSelLanguage"];

While overriding the InitializeCulture() method you can assign this to the culture variable.

In this application I have tryed to explain with two different forms:

  1. User registration form.
  2. Send Email (In which I have also explained that how can we make use of built-in asp.net validation controls).

And on both the pages I have shown as we can switch on different pages by making use of <asp:HyperLink>.

Now let us have a look at "Registration.aspx" page code.

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Registration.aspx.vb" Inherits="Registration" meta:resourcekey="RegResource" %>

This code is preety much similar with the code which get generated normally for any aspx page, only difference is that:

meta
:resourcekey="RegResource"

Here I want to explain in little depth, the concept of storing values like key-value pair and accessing values in the aspx page. Forexample if you refer to "Registration.aspx.resx" & "Registration.aspx.ar.resx" pages you will notice that a resourceKey "RegResource.Title" has value "Registration" similarly in the other language. "RegResource.Title" has  value, which is directly accessed as shown above.

<
html id="Html1" runat="server" dir='<%$ Resources:MulResource, TextDirection %>' xmlns="http://www.w3.org/1999/xhtml">

in the above <html> tag it should have runat="server" attribute or it will give you error use literal runat="server".

As I have already explained you about the global & local resources, in the above <html> we have used dir='<%$ Resources:MulResource, TextDirection %>'  attribute, now let us go little in dept for this, many of you must be aware of "DIR" attribute, it is nothing but the direction or the text in the page by default it is LTR (left to write) to have arabic type of appearance of the page we have to use it as "RTL". In short this act as common for all the pages either LTR or RTL so I have two global resource files as:

  1. MulResource.resx
  2. MulResource.ar.resx

In which you will find "TextDirection" ResourceKey and it's value associated with it, which is used in the above code.

I think so far so clear?

Now let us see how we can access the value of different labels in our project.

For example: On the registration page I have label called as "lblFirstName" and its values are stored in the corresponding resource files viz.

  1. Registration.aspx.resx
  2. Registration.aspx.ar.resx

Here is code to make use of the same.

<asp:Label ID="lblFirstName" runat="server" meta:resourcekey="lblFirstName"></asp:Label>

Note: None of the labels have assigned any default value, label get values assigned at runtime depend on the selected language on the default page