Globalization and links in sentences

社会主义新天地 提交于 2019-12-24 22:14:22

问题


I am working on globalizing/localizing an asp.net application with C# as the backend. We are in the process of extracting strings to a resource file, and have run into a problem. We are trying to keep sentences together so that they are translatable, but this is not possible with links. For Example:

<%= Strings.BeginningOfSentence %>
<asp:HyperLink id="exampleLink" runat="server"><%= Strings.MiddleOfSentence %></asp:HyperLink>
<%= Strings.EndOfSentence %>

Strings is the resource file. If this were normal html for the link, I could use String.Format and keep the sentence together, adding in the html as two parameters, but that breaks it here. Any ideas in how to make this work?


回答1:


You don't have to use a HyperLink control for this do you? If you need a dynamic link then you can store the anchor tag in a parameterised string and add the necessary attribute values using string.Format, like you suggested. Something like this:

Code:

myLiteral.Text = string.Format("{0} <a href=\"{1}\">{2}</a> {3}", Strings.BeginningOfSentence, myUrl, Strings.MiddleOfSentence, Strings.EndOfSentence);

ASPX:

<asp:Literal id="myLiteral" runat="server" />



回答2:


I have found that parameterized strings greatly simplifies translations mixed with dynamic content. For instance, you can have placeholders in the translated string into which link-html can be inserted. This may rule out the use of server-side hyperlink controls though. Example strings:

English:

"The <a href=\"http://images.google.se/images?q=house&tab=wi\">house</a> in which we lived"

Swedish:

"<a href=\"http://images.google.se/images?q=hus&tab=wi\">huset</a> som vi bodde i"

Note how the link has moved in the sentence in relation to the link (there is no text before the link in the Swedish version).

If you do not want to include the markup in the translation, I guess that may be used as a parameterized template in itself:

string googleSearchTemplate = "<a href=\"http://images.google.se/images?q={0}&tab=wi\">{1}</a>"

Then you can parse translated pieces into the link html, and then insert that piece into the final string:

string.Format("The {0} in which we lived", string.Format(googleSearchTemplate, "house", "house"));

Then you just need to insert the resulting string into the page.



来源:https://stackoverflow.com/questions/1262784/globalization-and-links-in-sentences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!