简体   繁体   English

本地化asp.net下拉列表

[英]Localizing asp.net dropdownlist

I want to create a multilingual ASP.NET web form with some dropdownlist (DDL) items. 我想用一些下拉列表(DDL)项目创建一个多语言ASP.NET Web表单。 The DDLs need to be populated from an easily maintained source (eg in web.config or better still, in .resx files). DDL需要从易于维护的源中填充(例如,在web.config或更佳的.resx文件中)。 The DDL list items must use a name/value pair. DDL列表项必须使用名称/值对。

The end result should be something like this... 最终结果应该是这样的...

When the UI culture is en-GB... 当用户界面文化为英语时...

    <select id="MyDDL">
        <option value="1">Dog</option>
        <option value="2">Cat</option>
    </select>

When the UI culture is fr-FR... 当UI文化是fr-FR ...

    <select id="MyDDL">
        <option value="1">Chien</option>
        <option value="2">Chat</option>
    </select>

What is the most elegant way to achieve the desired result? 达到预期效果的最优雅方法是什么?

Create two resource files in App_GlobalResources folder. 在App_GlobalResources文件夹中创建两个资源文件。 Name them like 给他们起个名字

Main.fr-FR.resx Main.fr-FR.resx

Main.resx Main.resx

Then in UI set value using resource file like: 然后在UI中使用资源文件设置值,例如:

<select id="MyDDL">
    <option value="1"><%=Resources.Main.value1 %></option>
    <option value="2"><%=Resources.Main.Value2 %></option>
</select>

Finally set the UI culture in the config and test. 最后,在配置中设置UI文化并进行测试。

You need to have a resource file (. resx ) for each language (including English) with culture name in file name. 您需要为每种语言(包括英语)创建一个资源文件( .resx ),文件名中应包含区域性名称。

Eg 例如

Site.resx (default) Site.fr-FR.resx (for french) Site.resx (默认) Site.fr-FR.resx (法语)

In these resource files map your English words to the French (or any other language you want). 在这些资源文件中,将您的英语单词映射到法语(或您想要的任何其他语言)。 Then set the culture in your master page or in a base class. 然后在您的母版页或基类中设置区域性。

Here's a complete example how you Globalize your application. 这是一个如何全球化应用程序的完整示例。

For security reasons I ended up using external .csv files to hold the language for dropdown lists. 出于安全原因,我最终使用外部.csv文件保存下拉列表的语言。

My .csv file looks like this... 我的.csv文件如下所示...

ID,Col1_english,Col1_irish
0,-- Select an option --,-- Roghnaigh --
64,Suggestion,Moladh a dhéanamh
59,Complaint,Gearán
61,Praise,Moladh a thabhairt

I use it populate the DropDownList1 like this... 我用它像这样填充DropDownList1 ...

DropDownList1.DataSource = new DataSet().GetDataFromCSV("Sentiments",Server.MapPath(my_CSV_path));
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Col1_english";

The GetDataFromCSV extension method... GetDataFromCSV扩展方法...

public static DataSet GetDataFromCSV(this DataSet ds, string data_table_name,string csv_file_path)
    {
         string strLine;
         string[] strArray;
         char[] charArray = new char[] {','};
         DataTable dt = ds.Tables.Add(data_table_name);
         FileStream aFile = new FileStream(csv_file_path,FileMode.Open);
         StreamReader sr = new StreamReader(aFile);

         strLine = sr.ReadLine();

         strArray = strLine.Split(charArray);

         for(int x=0;x<=strArray.GetUpperBound(0);x++) 
         {
            dt.Columns.Add(strArray[x].Trim());
         }

         strLine = sr.ReadLine();
         while(strLine != null) {
            strArray = strLine.Split(charArray);
            DataRow dr = dt.NewRow();
            for(int i=0;i<=strArray.GetUpperBound(0);i++) 
            {
               dr[i] = strArray[i].Trim();
            }
            dt.Rows.Add(dr);
            strLine = sr.ReadLine();
         }
         sr.Close();
         return ds;
    }

during Page_Load I then read from CurrentCulture and switch the DropDownList1.DataTextField value accordingly. 然后在Page_Load期间,我从CurrentCulture读取并相应地切换DropDownList1.DataTextField值。

It's not elegant but works well because I need non technical people to keep the DropDownList text, and translations, up to date and can't use a database. 它不是很优雅,但是效果很好,因为我需要非技术人员来保持DropDownList文本和翻译为最新状态,并且不能使用数据库。

Thanks @Saminda & @Sam - you were both on the right track to that elusive elegant solution. 谢谢@Saminda和@Sam-你们俩都处在这个难以捉摸的优雅解决方案的正确轨道上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM