Problem when concatenating string and a resource value

白昼怎懂夜的黑 提交于 2019-12-12 13:33:03

问题


I'm having a strange behavior in my .aspx page. I'm internationalizing some pages, but as I want to keep only entities or value object in my resource I want to have a key-value like:

(pt-br)
    CITY - Cidade
    STATE - Estado
    ...

But when I list the entities, I want to put the " : " at end. Like:

<asp:Label ID="LabelCity" runat="server" Text="<%$ Resources:Localizacao, CITY %>:"></asp:Label>

But, if I put the " : " after the resource in text property, the page only shows " : ".

My simple solution is put after all definition of label, but I think this too wrong:

<asp:Label ID="LabelCity" runat="server" Text="<%$ Resources:Localizacao, CITY %>"></asp:Label>:

Suggestions?


回答1:


You named this correctly: the concatenation. The problem is, Concatenation is one of the most serious (as in high severity) i18n defect. Instead of doing what you do, I would advise you to put whole strings (or in the worst case scenario strings with placeholders) into resource files, even you have to duplicate resources (these duplicated resources might be translated in different way depending on a context).

Let me give you an example: back when I was in L10n Team, we had a console with multiple pages, each page had a title which was "Something Policy", i.e. "Firewall Policy", "Antivirus and Antispyware Policy". Somebody thought it is a good idea to save some precious bits and simply concatenated the title together:

String title = Resources.getString("Firewall") + "<b>" + Resources.getString("Policy") + "</b>";

The only problem we had is correct Polish translation sounds like "Polityka zapory ogniowej", that is "Policy" comes first. In reality, we had it translated as "Zapora ogniowa Polityka" which is nowhere near the correct translation (notice that translation of "Firewall" should be genitive... Translator had no clue what he is translating, thus the translation. And it couldn't be fixed as we were unable to re-order the sentence.

The fix required externalizing the whole string, so actually all the good intentions of original developer went to pave the hell.

If you still reading this, please keep in mind, that all colons in French language must be preceded by space (that's just their rule). Re-using the same translations in order to build multiple sentences might disallow that...




回答2:


Had the exact same problem and after some searching transformed this

<asp:Label ID="label" runat="server" Text="<%$ Resources: Resources, color %>" />:

to this:

<asp:Label ID="label" runat="server">
    <%= Resources.Resources.color + ":"%>
</asp:Label>



回答3:


Expressions (the <%$) are a set syntax so it is a all or nothing if you want to use an expression there. You can do what you have suggested or set the values from the code behind as alternatives.

However, I think your character should be in your resource file. Are all languages going to use that same character? Will that work for right-to-left lanugages?



来源:https://stackoverflow.com/questions/5298791/problem-when-concatenating-string-and-a-resource-value

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