在资源文件中使用 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1491062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:57:04  来源:igfitidea点击:

Using HTML inside resource files

asp.nethtmlasp.net-mvcresources

提问by Peter

I am developing a standard small ASP.NET MVCwebsite which will be made multilingual using ASP.NETresource files.

我正在开发一个标准的小型ASP.NET MVC网站,该网站将使用ASP.NET资源文件实现多语言。

My question is about the resource files. See if you have a block of text which should be shown in paragraphs, is it appropriate to add <p>tags inside your resource file text?

我的问题是关于资源文件。看看你是否有一段应该显示在段落中的文本块,<p>在你的资源文件文本中添加标签是否合适?

If not, what would be the best way to deal with it?

如果没有,最好的处理方法是什么?

回答by Evrm

You can use the @Html.Rawmethod inside your view, e.g. @Html.Raw(STRING FROM RESX FILE HERE)

您可以@Html.Raw在视图中使用该方法,例如@Html.Raw(STRING FROM RESX FILE HERE)

回答by Peter

When I faced the same question I decided against having paragraphs directly in resources. Separate text block by simple new lines (Shift+Enter if I'm not mistaken). Then create a helper method to wrap blocks into paragraphs.

当我面临同样的问题时,我决定不直接在资源中使用段落。用简单的新行分隔文本块(如果我没记错的话,Shift+Enter)。然后创建一个辅助方法来将块包装成段落。

using System;
using System.Text;
using System.Web;

namespace MyHtmlExtensions
{
  public static class ResourceHelpers
  {
    public static IHtmlString WrapTextBlockIntoParagraphs(this string s)
    {
      if (s == null) return new HtmlString(string.Empty);

      var blocks = s.Split(new string[] { "\r\n", "\n" },
                            StringSplitOptions.RemoveEmptyEntries);

      StringBuilder htmlParagraphs = new StringBuilder();
      foreach (string block in blocks)
      {
        htmlParagraphs.Append("<p>" + block + "</p>");
      }

      return new HtmlString(htmlParagraphs.ToString());
    }
  }
}

Then in your view you import your extension method namespace:

然后在您的视图中导入您的扩展方法命名空间:

<%@ Import Namespace="MyHtmlExtensions" %>

And apply it:

并应用它:

<%= Resources.Texts.HelloWorld.WrapTextBlockIntoParagraphs () %>

I think it is a more cleaner way than putting markup tags into text resources where they in principle do not belong.

我认为这是一种比将标记标签放入它们原则上不属于的文本资源更干净的方法。

回答by Wim ten Brink

Yes and no. Resources are a good place to store localized text and basically this will allow you to send the whole resource to some translation agency who will do all translations for you. So, that's a plus. However, it also increases the chance of bugs simply because you might forget to encode or decode the HTML. The .resx file is an xml file so it needs to be stored encoded in the resource. (Fortunately, it will encode it automatically.) When you retrieve it again, it will be decoded automatically again. However, if you pass the text to other functions, there is a risk that it will be encoded again, resulting in <b>bold</b> instead of bold... So you will need some additional testing. Which is something you can manage.

是和否。资源是存储本地化文本的好地方,基本上这将允许您将整个资源发送给某个翻译机构,该机构将为您完成所有翻译。所以,这是一个加分项。但是,这也增加了出现错误的机会,因为您可能会忘记对 HTML 进行编码或解码。.resx 文件是一个 xml 文件,因此它需要以编码方式存储在资源中。(幸运的是,它会自动编码。)当您再次检索它时,它会再次自动解码。但是,如果您将文本传递给其他函数,则存在再次编码的风险,从而导致 <b>bold</b> 而不是粗体……因此您需要进行一些额外的测试。这是您可以管理的。

But the big problem could be for the person who does the translations. If they just translate the .resx file, they'll see encoded strings. This could be solved if they use something to make the resources more readable. Just as long as they won't translate the tags themselves. They could confuse the persons who will be doing the translations.

但最大的问题可能出在翻译人员身上。如果他们只是翻译 .resx 文件,他们会看到编码的字符串。如果他们使用一些东西使资源更具可读性,这可以解决。只要他们不会自己翻译标签。他们可能会混淆将要进行翻译的人员。

回答by Tony Bennett

You can wrap the data for the resx value field in a CDATA block, <![CDATA[ ]]>.

您可以将 resx 值字段的数据包装在 CDATA 块中<![CDATA[ ]]>

e.g.

例如

<![CDATA[My text in <b>bold</b>]]>

Depending on how you want to use it, you will probably have to strip off the <![CDATA[and ]]>within your app.

根据您想如何使用它,您可能需要在您的应用程序中去除<![CDATA[]]>

This approach allows you to include any html markup in the resx file. I do this to store formatted text messages to be displayed in a jquery-ui dialog from various views within an asp.net mvc app.

这种方法允许您在 resx 文件中包含任何 html 标记。我这样做是为了存储格式化的文本消息,以便从 asp.net mvc 应用程序中的各种视图显示在 jquery-ui 对话框中。

回答by UpTheCreek

I would say that this should be avoided, but sometimes it is practically unavoidable. Ideally it would be best to store your resources at a level of granularity that allows you to add the presentation markup in common for all languages in the appropriate area of the app.

我会说应该避免这种情况,但有时实际上是不可避免的。理想情况下,最好以一定的粒度级别存储您的资源,以便您可以在应用程序的适当区域中为所有语言添加通用的表示标记。

Sometimes gramatical differences between languages make this difficult. Sometimes you want to store large chunks of text based resources (large enough that they would have to encompass things like p tags). It depends a little what you are doing.

有时,语言之间的语法差异使这变得困难。有时您想要存储大块基于文本的资源(足够大以至于它们必须包含诸如 p 标签之类的东西)。这在一定程度上取决于你在做什么。

回答by t_plusplus

You might as well open your resource file in designer and simply type html and text as you wish ..

您不妨在设计器中打开您的资源文件,然后根据需要简单地输入 html 和文本。

For example: Resource Text <b>Data</b>

例如: Resource Text <b>Data</b>

However the solution from @Tony Bennett works perfectly..

然而,@Tony Bennett 的解决方案非常有效。

Also something like this works; the special characters here are use to open/close the triangle brackets of < and >:

也有类似的东西;这里的特殊字符用于打开/关闭 < 和 > 的三角括号:

are you targeting to &lt;b&gt;sustain&lt;/b&gt; the Mark they achieved

回答by Blantant

Use URLEncoder to store the HTML string within the resource; then Decode the String and use webview rather than converting all the HTML tags.

使用 URLEncoder 在资源中存储 HTML 字符串;然后解码字符串并使用 webview 而不是转换所有的 HTML 标签。

URLDecoder decodes the argument which is assumed to be encoded in the x-www-form-urlencoded MIME content type. '+' will be converted to space, '%' and two following hex digit characters are converted to the equivalent byte value. All other characters are passed through unmodified.

For example "A+B+C %24%25" -> "A B C $%".

URLDecoder 解码假定在 x-www-form-urlencoded MIME 内容类型中编码的参数。'+' 将转换为空格,'%' 和后面的两个十六进制数字字符将转换为等效的字节值。所有其他字符都未经修改地传递。

例如“A+B+C %24%25”->“ABC $%”。

回答by Anthony Johnston

We do.

我们的确是。

You could use a text-to-HTML converter, but as text contains so much less information than HTML you'd be restricted.

您可以使用文本到 HTML 的转换器,但由于文本包含的信息比 HTML 少得多,因此您会受到限制。