目前我正在开发一个 ASP.net webforms 项目,我想向用户指示当前页面上的哪些元素包含本地化资源。
为了识别哪些 HTML 元素包含哪些本地化资源。我想标记所有渲染的 HTML 元素,这些元素在渲染页面时从 ResourceProvider 检索到资源。从而创建 HTML 元素和所使用的资源之间的映射。
标记将通过向 html 元素添加 data-localized
属性来完成。该属性将包含所使用的资源标识符(即 resx 文件名和资源键),然后当浏览器显示页面时,JavaScript 将使用此信息执行任何操作。
到目前为止,我已将默认资源提供程序替换为我自己的提供程序,该提供程序从数据库而不是常规 .resx 文件检索资源(如 这篇文章)。这为我提供了有关使用的所有资源的信息,并且通过将这些信息记录在 HttpContext 中,我知道当前页面使用的所有资源。
现在进行映射,因为本地化资源并不总是必须出现在 HTML 元素中(例如,它可能只是由 Literal
控件生成的字符串)。我引入了一个新的 CompositeControl
,它用 包装了包含本地化资源的部分。
。
我的问题:如何将包装器与其子级使用的资源映射?
Currently I am working on an ASP.net webforms project where I would like to indicate to the user which elements on the current page contain localized resources.
In order to identify which HTML elements, contain which localized resources. I'd like to tag all rendered HTML elements that had a resource retrieved from the ResourceProvider while rendering the page. Thereby creating a mapping between HTML element and the used resource(s).
The tagging will be done by adding a data-localized
attribute to the html element. The attribute will contain the resource identifier(s) used (i.e. resx file names and the resource keys), and then when the in the browser displays the page a javascript will use this information to do whatever.
So far I have replaced the default resource-provider with my own provider that retrieves resources from a database instead of the regular .resx files (as described in this article). This gives me information about all the resources used and by logging these in the HttpContext, i know all the resources used for the current page.
Now for the mapping, because the localized resource doesn't always have to occur within an HTML element (e.g. it could be just a string yielded by a Literal
control). I have introduced a new CompositeControl
that wraps a section containing localized resources with a <div data-localized=""> </div>
.
My question: How can i map the wrapper with the resources used by its children?
发布评论
评论(1)
由于您将使用 JavaScript 对数据进行后处理,因此您可能会考虑以前缀和分隔符的形式添加所需的内容到实际翻译的文本中,例如“示例文本”将变为“##strings.resx##login”。 form.sample.text##示例文本".
然后,您的客户端脚本将查找 ## 分隔符并从页面中删除它们(或执行您想要的任何操作)。
我知道这不是您所要求的,但我相信它更简单且更容易实现。
Since you are going to post-process the data with JavaScript, you may think of adding what you need in form of prefix and delimiter to actual translated text, for example "Sample text" will become "##strings.resx##login.form.sample.text##Sample text".
Your client side script would then look for ## delimiters and remove them (or do whatever you want) from the page.
I know it is not what you asked for but I believe it is simpler and easier to implement.