开发者

Techniques for localizing in ASP.NET? [duplicate]

开发者 https://www.devze.com 2023-04-07 01:17 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What do I need to know to globalize an asp.net application?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What do I need to know to globalize an asp.net application?

The default Visual Studio sample web-site contains:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
       AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>Welcome to ASP.NET!</h2>
    <p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET
          Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>

This is a good trial on needed techniques for localizing a web-site. How would you localize these:

  1. <h2>Welcome to ASP.NET!</h2>

  2. <p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.</p>

  3. <p>You can also find <a href="http://go.microsoft.com/fwlin开发者_运维问答k/?LinkID=152368&amp;clcid=0x409" title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.</p>

Note the use cases

  • html element:

    <H2>Welcome to ASP!</H2>

    <H2>Bienvenue sur ASP.NET!</H2>

    <H2>ASP.NET xoş gəlmisiniz!</H2>

  • text with embedded html (notice how the link moves)

    <P>To learn more about ASP.NET visit www.asp.net.</P>

    <P>Pour en savoir plus sur www.asp.net visitez ASP.NET.</P>

    <P>ASP.NET səfər www.asp.net haqqında daha ətraflı məlumat üçün.</P>

  • text with embedded html, with embedded localizable text

    <P>You can also find <A title="Documentation on ASP.net">documentation</A></P>

    <P>Vous pouvez également trouver de la <A title="Documentation sur ASP.net">documentation</A></P>

    <P>Siz həmçinin <A title="ASP.net sənədləşdirilməsi">sənədlərin</A> tapa bilərsiniz</P>

See also

  • ASP.NET Localization (Quick Reference)
  • Globalizing and Localizing ASP.NET Web Pages (Visual Studio)

One could use an asp:Localize element to translate the text in the first example:

<H2><asp:Localize>Welcome to ASP!</asp:Localize></H2>

<H2><asp:Localize>Bienvenue sur ASP.NET!</asp:Localize></H2>

<H2><asp:Localize>ASP.NET xoş gəlmisiniz!</asp:Localize></H2>

problem is this solution falls apart in the next case:

<P><asp:Localize>To learn more about ASP.NET visit www.asp.net.</asp:Localize></P>

<P><asp:Localize>Pour en savoir plus sur www.asp.net visitez ASP.NET.</asp:Localize></P>

<P><asp:Localize>ASP.NET səfər www.asp.net haqqında daha ətraflı məlumat üçün.</asp:Localize></P>

because the localized string cannot contain other HTML elements.

Now it starts to require something like:

<P><asp:Localize>To learn more about ASP.NET visit %s.</asp:Localize></P>

<P><asp:Localize>Pour en savoir plus sur %s visitez ASP.NET.</asp:Localize></P>

<P><asp:Localize>ASP.NET səfər %s haqqında daha ətraflı məlumat üçün.</asp:Localize></P>

And then the html containing the link can be embedded where %s is. But then the localizer must localize fragmented phrases:

  • To learn more about ASP.NET visit www.asp.net.

becomes

  • To learn more about ASP.NET visit %s.

and in other languages it pseudo translates to:

  • To learn more about %s visit ASP.net
  • ASP.net visit %s about to learn more

Edit: What is the approved technique to localize items in ASP.net.


  • HTML elements, I usually place just the text within the HTML elements in resx files, rather than the entire HTML structure. I find it makes the localized content more re-usable
  • Text with embedded HTML - In your examples, I would put everything other than the <p> tags in my resx files - so the hyperlinks I would keep in the resx files
  • Text with embedded HTML - this scenario seems the same as the second, I would use the same approach

Another scenario to consider is when you need to dynamically inject content into a resource string, i.e. "Good afternoon Mr. Smith, how are you doing?" I recommend capturing these with a single resource key such as "Good afternoon {0}, how are you doing?" in order to avoid breaking sentences down into multiple resource keys such as "Good afternoon", and "how are you doing", since other languages don't follow the same grammar rules that English follows.

Sometimes it doesn't make sense to localize individual page fragments as separate resource strings. All of the HTML in the sample page that you've referenced could reside in a separate English text file for instance. Then you could create other language text files as need be. These might be XML files on your file system, or they might be stored as database resources, where you then dynamically load the appropriate language part at runtime. This can help to simplify the translation process and provide a lot more flexibility per language.

I usually localize titles, headings, individual terms, form fields, captions, error messages, etc., as separate resource keys, and often leave entire static pages/content snippets as their own resource fragments, rather than breaking them down into small units.

On the subject of where to persist your resources, you can use .resx, or roll your own (many people use filesystem-based approaches), or database driven. Rick Strahl has shared a great database resource provider if you're interested in persisting all of your resources to database, http://www.west-wind.com/weblog/posts/2009/Apr/02/A-Localization-Handler-to-serve-ASPNET-Resources-to-JavaScript.


Regarding the 3rd item (with the link), I once wrote a tiny little class to solve.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号