如何实现与 MVC 应用程序结构良好集成的区域标签文件?
背景:
我正在开发一个可在多个区域使用的网站。每个地区可以有不同的语言,或者同一语言的不同拼写。我们计划使用 XML 标签文件,该文件将包含为任何给定区域显示的所有文本。
因此,regionA 将加载 RegionALabels.xml,其所有文本都将来自该xml。 RegionB 将加载 RegionBLabels.xml,依此类推。
我正在考虑通过构造 XML 文件来实现这一点,以便它遵循与我的应用程序相同的结构。例如,
<Base> <!--BaseController-->
<Home> <!--HomeController-->
<HomePartial1>
<lblFirstName>text for label with ID lblFirstName</lblFirstName>
<lblLastName>text for label with ID lblLastName</lblLastName>
</HomePartial1>
<HomePartial2>
<!--a bunch of labels-->
</HomePartial2>
</Home>
<Accounts>
<AccountsPartial1>
<!--a bunch of labels-->
</AccountsPartial1>
<AccountsPartial2>
<!--a bunch of labels-->
</AccountsPartial2>
</Accounts>
</Base>
我想这样做的原因是因为我想采用约定优于配置,因此在哪里找到任何给定部分视图的文本就变得隐式了。
使用此结构,我可以使用 Linq-to-XML 选择与我要渲染的部分视图相关的文本。例如,如果我正在渲染局部视图 HomePartial1,我可以获取与该局部视图关联的标签,如下所示:
var nodes = from f in
(from res in this.XmlConfiguration.Descendants("Base")
select res).Descendants("Home")
select f;
var HomePartial1Labels = nodes.Elements("HomePartial1").ToList();
问题:
因此,在这种情况下,基本上我想问的是:在我尝试实现此方法之前,任何人都可以指出此方法中的任何明显漏洞吗?以前处理过这个问题的人可以提出更好的建议吗?
另外,我有一个特定的要求来渲染带有多个选项卡的部分视图,每个选项卡都必须显示文本和内容。与该选项卡相关的许多结果。例如,我有 3 个选项卡:
Fund
Company
Group
如果有人进行搜索,选项卡必须显示搜索词为该选项卡返回的结果数,因此,如果我搜索“JP Morgan”,它们将变为:
Fund (10) // 10 funds contain the name 'JP Morgan'
Company (12) // 12 companies contain the name 'JP Morgan'
Group (15) // 15 groups contain the name 'JP Morgan'
为了包含文本“标签文件中的“基金”、“公司”和“集团”,我还必须提供“(”和“)”,这意味着我认为我必须使用占位符并将其替换为我渲染部分时的实际值。所以 XML 将开始看起来像:
<Base> <!--BaseController-->
<Home> <!--HomeController-->
<Tabs>
<lblFundTab>Funds (#value#)</lblFundTab>
<lblCompanyTab>Company (#value#)</lblCompanyTab>
<lblGroupTab>Group (#value#)</lblGroupTab>
</Tabs>
</Home>
</Base>
但我认为这将开始看起来混乱并且有点黑客化。有人可以建议更好的东西吗?谢谢。
Background:
I am working on a site that will be available in multiple regions. Each region can have a different language, or different spellings for the same language. We are planning on using an XML label file which will contain all the text that will be displayed for any given region.
So regionA will load RegionALabels.xml and all of its text will come from that. RegionB will load RegionBLabels.xml, and so on.
I'm thinking of implementing this by structuring the XML file so that it follows the same structure as my application. For example,
<Base> <!--BaseController-->
<Home> <!--HomeController-->
<HomePartial1>
<lblFirstName>text for label with ID lblFirstName</lblFirstName>
<lblLastName>text for label with ID lblLastName</lblLastName>
</HomePartial1>
<HomePartial2>
<!--a bunch of labels-->
</HomePartial2>
</Home>
<Accounts>
<AccountsPartial1>
<!--a bunch of labels-->
</AccountsPartial1>
<AccountsPartial2>
<!--a bunch of labels-->
</AccountsPartial2>
</Accounts>
</Base>
The reason I want to do it this way is because I want to imploy Convention over Configuration, so it becomes implicit where to find the text for any given partial view.
using this structure, I can use Linq-to-XML to select the text relevant to the partial view I'm going to Render. For example, if I'm rendering the partial view HomePartial1, I can get the labels associated with that partial view with something like:
var nodes = from f in
(from res in this.XmlConfiguration.Descendants("Base")
select res).Descendants("Home")
select f;
var HomePartial1Labels = nodes.Elements("HomePartial1").ToList();
Question:
So this being the case, basically what I want to ask is if anyone can point out any obvious holes in this approach before I attempt to implement this? Can anyone who has dealt with this before suggest something better?
also, I have one specific requriement to render a partial view with a number of tabs, each of which has to display text & a number of results associated with that tab. For example, I have 3 tabs:
Fund
Company
Group
If somebody does a search, the tabs have to display the number of results the search term returns for the tab, so if I search for 'JP Morgan' they become:
Fund (10) // 10 funds contain the name 'JP Morgan'
Company (12) // 12 companies contain the name 'JP Morgan'
Group (15) // 15 groups contain the name 'JP Morgan'
In order to contain the text "Fund", "Company" and "Group" in the label file, I'm going to have to supply the "(" and ")" also, which means I think I'll have to use a place holder and replace it with the actual value when I'm rendering the partial. So the XML will begin to look like:
<Base> <!--BaseController-->
<Home> <!--HomeController-->
<Tabs>
<lblFundTab>Funds (#value#)</lblFundTab>
<lblCompanyTab>Company (#value#)</lblCompanyTab>
<lblGroupTab>Group (#value#)</lblGroupTab>
</Tabs>
</Home>
</Base>
but I think this is going to start to look messy and a little hackish. Can somebody suggest something better? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近在一个项目中也遇到了同样的事情。
我实现了一个 LabelService,它本质上包装了一个“LabelFiles”的单例数组,这些数组在 application_start 时加载到内存中。所以我的代码看起来很相似,但在内存对象而不是 xml 文件中工作,避免了很多潜在的 IO 问题(锁等)。
你的 xml 标签文件很好,假设它是由应用程序或可能是开发人员编辑的,但如果它是由客户/非技术人员编辑的,他们如何知道“基础”是什么?还是控制器?等等 - 我是 CoC 的粉丝,但有时最好更接近特定领域应用程序的领域术语。
I have recently had the same thing in a project.
I implemented a LabelService that essesntially wrapped a Singleton array of "LabelFiles" which were loaded into memory on application_start. So my code looks similar but is working on in memory objects and not xml files avoiding a lot of potential IO issues (locks etc).
Your xml label file is fine, assuming it is to be edited by an application or possibly a developer but if it is going to be edited by the client /non-techie how are they to know what "base" is? Or controller? Etc etc - I'm a fan of CoC but sometimes it is better to stick closer to the domain terminology for domain-specific apps.