从数据库翻译字符串(本地化)(Translating a string from a database (localisation))

编程入门 行业动态 更新时间:2024-10-22 23:10:07
从数据库翻译字符串(本地化)(Translating a string from a database (localisation))

我正在研究.NET MVC5中我的Web应用程序的全球化和本地化。 这适用于我的静态字符串,但我也不想翻译从数据库调用的一些文本。 我的设置是这样的:

我有一个资源文件夹,包含2个资源文件,一个英文版(Resource.resx)和一个荷兰文(Resource.nl-NL.resx)。 在这些文件中是我的翻译。 然后我有一个带有CultureHelper类的Helper文件夹,我有一个BaseController。 我的Homecontroller由我的BaseController扩展。 在我的ViewModels中,我指的是这样的翻译:

[Display(Name = "Email", ResourceType = typeof(Resource))]

这一切都很好,但我也有一些存储在数据库中的字符串/字段。 我也想翻译这些字段。 但是我不知道怎么做。

这是我调用字段的部分(以及字段的值,我不想翻译值,只有fieldName)。

foreach (var fieldName in Model.LineViewAttributeNames) { <div class="editor-label"> @fieldName </div> <div class="editor-field"> @if (!string.IsNullOrEmpty(Model.SelectedLineView)) { @Html.EditorFor(x => x.LineViewItems.First(lvi => lvi.Id == Model.SelectedLineView) .LineFieldAttributes.Single(lfa => lfa.Name == fieldName).Value, null, fieldName, null) } else { <input type="text" name="@fieldName" /> } </div> }

有人可以帮我解释如何翻译fieldNames吗?

编辑,我的问题的答案

在我的帮手中:

public static string GetLabel(string fieldName) { ResourceManager resMngr = new ResourceManager("Project.Resources.Resource", typeof(Resources.Resource).Assembly); return resMngr.GetString(fieldName, System.Threading.Thread.CurrentThread.CurrentUICulture); }

在我看来:

@Project.Helper.ResourceHelper.GetLabel(fieldName)

I'm working on globalization and localisation of my web applications in .NET MVC5. This works fine for my static strings, but I also wan't to translate some text called from a database. My setup is like this:

I have a resource folder with 2 resource files, one English version (Resource.resx) and one Dutch (Resource.nl-NL.resx). In these files are my translations. Then I have a Helper folder with my CultureHelper class, and I have a BaseController. My Homecontroller is extended by my BaseController. In my ViewModels I refer to the translations like this:

[Display(Name = "Email", ResourceType = typeof(Resource))]

This all works just fine, but I also have some strings / fields that are stored in a database. I also want to translate those fields. But I do not know how.

This is the part where I call for the fields (and the value of the fields, I don't want to translate the the value, only the fieldName).

foreach (var fieldName in Model.LineViewAttributeNames) { <div class="editor-label"> @fieldName </div> <div class="editor-field"> @if (!string.IsNullOrEmpty(Model.SelectedLineView)) { @Html.EditorFor(x => x.LineViewItems.First(lvi => lvi.Id == Model.SelectedLineView) .LineFieldAttributes.Single(lfa => lfa.Name == fieldName).Value, null, fieldName, null) } else { <input type="text" name="@fieldName" /> } </div> }

Can someone help me explain how to translate the fieldNames?

EDIT, the answer to my question

In my helper:

public static string GetLabel(string fieldName) { ResourceManager resMngr = new ResourceManager("Project.Resources.Resource", typeof(Resources.Resource).Assembly); return resMngr.GetString(fieldName, System.Threading.Thread.CurrentThread.CurrentUICulture); }

In my view:

@Project.Helper.ResourceHelper.GetLabel(fieldName)

最满意答案

如果您的resx文件中包含所有翻译,则可以执行以下操作。

/* My resource files with the translations in them are named "Lang.ar-EG.resx, Lang.es-MX.resx, ..." and reside in my resource folder in the application call "MyApp" */ public static class Translator{} private static System.Resources.ResourceManager _langResource = new System.Resources.ResourceManager("MyApp.Resources.Lang", typeof(Language).Assembly); public static string Translate(string key, string culture) { return _langResource.GetString(key, CultureInfo.CreateSpecificCulture(culture)); } }

然后我可以在我的.cshtml中执行此操作

<th>@Translator.Translate("Date", "ar-EG")</th>

If you have all your translations in your resx files, you can do something like this.

/* My resource files with the translations in them are named "Lang.ar-EG.resx, Lang.es-MX.resx, ..." and reside in my resource folder in the application call "MyApp" */ public static class Translator{} private static System.Resources.ResourceManager _langResource = new System.Resources.ResourceManager("MyApp.Resources.Lang", typeof(Language).Assembly); public static string Translate(string key, string culture) { return _langResource.GetString(key, CultureInfo.CreateSpecificCulture(culture)); } }

and then I can do this in my .cshtml

<th>@Translator.Translate("Date", "ar-EG")</th>

更多推荐

本文发布于:2023-07-06 07:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数据库   Translating   localisation   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!