简体   繁体   English

asp.net中的应用程序本地化

[英]Application Localization in asp.net

I am close to end of my ASP.net project, and now I am approaching to part where i need to translate my application. 我接近结束了我的ASP.net项目,现在我正在接近我需要翻译我的应用程序的部分。

My idea is to keep ID of control in my database table, on a page load I am simply taking "LanguageID" from Session variable that I stored earlier and make simply query to Database and end up with DataTable that looks like 我的想法是在我的数据库表中保持控件的ID,在页面加载时我只是从我之前存储的Session变量中获取“LanguageID”并简单地查询到Database并最终得到看起来像DataTable的

ControlID     |     Text
--------------------------------
tabAutoMail   | E-MAIL PATTERN
tabUser       | LOG IN
lblSignIn     | Please sign in
lblinputEmail | Email address

But my problem here now is that, I don't know how to start next ... if i go with something like this 但我现在的问题是,我不知道如何开始下一步......如果我选择这样的话

foreach (Control ctrl in Page.Controls)
{
    foreach (DataRow item in StaticTranslate.Rows)
    {
        if (ctrl.ID == item[0].ToString())
        {

        }
    }
}

Control doesn't have property of "Text", so I can't manually set it, if I go with something like this 控件没有“文本”的属性,所以如果我选择这样的话,我就无法手动设置它

foreach (DataRow item in StaticTranslate.Rows)
{ 
    var Test = this.Page.FindControl(item[0].ToString());
}

Again I end up with same problem, I don't know type of control and I can't cast "test" to proper control type 我最后遇到同样的问题,我不知道控制类型,我不能将“测试”转换为适当的控制类型

Can you please advice how to approach from here? 你能告诉我如何从这里接近吗? Thank you 谢谢

As mentioned in the comments there are other ways of making translations work 正如评论中所提到的,还有其他方法可以使翻译工作

If you want to make this work what you need to 2 is to store 3 properties 如果你想使这项工作你需要2是存储3个属性

  1. The control name 控件名称
  2. the property you want to localize 您想要本地化的属性
  3. the localized value 本地化的价值

(and of course the language selection needs to be stored somehow/somewhere) (当然语言选择需要以某种方式/某处存储)

The reason being is that some controls can have localizations in other ways than just 'Text'. 原因是某些控件可以通过其他方式进行本地化,而不仅仅是“文本”。 (I might want to localize an image source for example, or the maxlength of a postal code textbox can be language/country dependent) (例如,我可能想要本地化图像源,或者邮政编码文本框的最大长度可能取决于语言/国家/地区)

To set a property you then might use code like this. 要设置属性,您可以使用这样的代码。

foreach (DataRow item in StaticTranslate.Rows)
{ 
    var control = this.Page.FindControl(item["ControlName"].ToString());
    var property = control .GetType().GetProperty(item["PropertyName"].ToString());
    property.SetValue(control, item["LocalizedValue"].ToString());
}

This only deals with string localizations now and if i did want to localize the maxlength then i'd need some more work. 这只涉及现在的字符串本地化,如果我确实想要本地化maxlength,那么我还需要更多的工作。

I hope this convinced you to go the route NGPixel suggested and use a DB as a provider. 我希望这说服你去NGPixel建议的路线,并使用数据库作为提供商。 If that's too complicated you could even allow your resx file to be updated shown here https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx That way you could develop an admit tool to update your resx file and the rest of your site using 'standard' localization independently 如果这太复杂了你甚至可以让你的resx文件更新在这里显示https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx这样你就可以开发一个承认工具使用“标准”本地化独立更新您的resx文件和您网站的其余部分

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

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