我正在研究.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>更多推荐
发布评论