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&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:
<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&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.
Related
I have a kendo single page application that I need to localize. I tried using Javascript to translate text parts, but it gets complicated when translating non HTML bits like templates (inside script tags) and properties like "data-title".
Next I tried changing the extension to .aspx and using ASP.NET resources, as in this example:
<!-- logout -->
<div id="logout" data-role="view" data-layout="layout-logout"
data-title="<asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:TextStrings, Logout%>" />"
data-before-show="myproject.logout">
<div style="padding: 50px 0; text-align: center;">
<p id="lang_LoggedOut"><asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:TextStrings, Logout_Success%>" /></p>
</div>
</div>
This works to some extent, as the <% resources %> bits get replaced.
However I had to transfer login to a separate .aspx page to apply the language, after login (and discovering user culture), but before single page app load.
The login page calls the customary:
protected override void InitializeCulture()
to set the culture, and then redirects to the main app page.
As far as I can make out, the InitializeCulture is ignored, but the page is rendered with the strings from the resource files, sometimes in one language, sometimes in another.
How does ASP.NET decide what culture to apply, and how can I control that choice?
Notes:
failing this, the kendo people seem to suggest having one page for each language, which rather goes against my DNRY inclinations.
Code behind and inline code (<%Page%> directive) are in my experience incompatible with kendo. If I understand right page events get stolen by one another. Kendo is compatible with MVC, but I must learn MVC first, and then get resource files to work with MVC.
The short answer is yes. I have seen it work with VS2010 and IIS 7.5.
The page with no code behind must have an .aspx extension.
the language selection must happen on another .aspx page with code-behind. Call InitializeCulture() here
from this page you must go to the main page using Server.Transfer. See: Server.Transfer Vs. Response.Redirect
Server.Transfer will keep the settings and context you set, and apply the language to the page with no code-behind while the user navigates the page.
I am localizing an ASP.NET site using automatic feature , where it creates a local resource file and adds the meta keyword to asp.net controls. However I have a lot of HTML like below
<h2> Welcome to our page"</h2>
<li> Option one </li>
Is there a way to get these automatically translated using the automatic localize utility ?
I tried adding the runat="server" for these tags but to no avail.
Also instead of localizing page by page is there a way to localize bulk - it a directory or a site at one go
thanks
You need to use Localize control for static text - for example,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="Welcome to our page" meta:resourcekey="WelcomeMessage" />
</h2>
Alternatively,
<h2>
<asp:Localize runat=server ID="WelcomeMessage"
Text="<%$ Resources:WebResources, WelcomeMessage %>" />
</h2>
You can also use syntax such as
<h2><%= Resources.WebResources.WelcomeMessage %></h2>
where Resources.WebResources is strongly typed resource class generated by Visual Studio resource generator. For across page resources, you can create global resources and then refer then using syntax as shown above (meta key will not work for global resources).
See MSDN for more information.
I'm working on internationalizing and localizing an ASP.NET app, and I'm running into problems with cases where string literals are mixed with markup. Like this example:
Acme Carpet Retailers <a href="#" class="link" id="ssoLoginUrl"
runat="server">click here</a> to log into the site.
For most string literals I'm using the Localize control. I can embed this in a Localize control, tags and all, but the problem is then the link isn't a server control anymore. (It's referenced on the page by its ID.)
I can't really split up the strings ("Acme Carpet Retailers", "click here", etc.) because the word order could be different in a different language.
I've seen several suggestions on how to do this, but they all assume that the text with embedded link is assigned in code, not in markup, and that it's not a server control.
Any advice on how to localize this successfully?
Can't you do just:
<%= String.Format(Resources.MyResource.mydata,
"<a href=""#"" class=""link"" id=""ssoLoginUrl"" runat=""server"">",
"</a>"
) %>
Then I would have the resource mydata defined as:
"{0}click here{1} to log into the site."
If the word order could be different in a different language, you have no choice but to localize the entire text (tag included) inside the resx.
But, of course, if this is too troublesome, you can change the app to make the text more structured:
Acme Carpet Retailers <a href="#" class="link" id="ssoLoginUrl"
runat="server">(Log in here)</a>
This is my layout template (ascx without code behind)
<%# Control Language="C#" AutoEventWireup="true" Inherits="ws.helpers.LayoutUC" %>
<div>blah blah blah</div>
<ws:Panel runat="server" ID="left"></ws:Panel>
<ws:Panel runat="server" ID="main"></ws:Panel>
<ws:Panel runat="server" ID="right"></ws:Panel>
Modules will be added into ws:Panel later.
I also allow my user create their own ascx file to custom their page layout. And because of this i do a string replace all dangerous part like script tag (runat="server"), all asp.net html tag, <%, <%#, <#.... from their custom.
Im not worry about XSS, so dont comment on it, and ask why?
I want know your thinking about this. Is is safe? Is it scalable? Is it standard or a bad way?
Have a look at the INaminingContainer Interface http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx.
<asp:YourControl>
<LeftColumn>
<asp:Literal ID="literal1" runat="server" Text="User created literal" />
</LeftColumn>
</asp:YourControl>
In the .ascx from the users, they register your control and insert asp.net code into properties. In the 'YourControl' class you create placeholders and insert the markup set to a specific property into these placeholders. (e.g. everything between <LeftColumn> and </LeftColumn> will the inserted into
<asp:Placeholder ID="PlaceholderLeftColumn" runat="server"/>
Edit: I summed some of the TemplateContainer issue up and posted it here: http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
You are allowing user-uploaded content; this is inherently unsafe and there are whole books dedicated to best practices. Given that you are doing it anyway, as long as you make sure you scrub the input, is it scalable? You are allowing creation of user-uploaded files on your site. How many will there be? How many users? What about load-balancing? This solution will not scale for many users, files, or servers.
It sounds like you are trying to create a simple CMS. Why not use one that exists currently, or adopt parts of an open source solution?
In my application I have paragraphs with mixed static text and HTML formatting and links. I'm looking for a good localization solution that keeps resources decoupled from markup. Let's say we have the following paragraph:
<p>Let's have a cup of coffee and get <b>energized</b>.</p>
Using the standard resx solution forces me to embed the HTML markup and the link destinations in the resx string. This is bad because it couples markup/CSS/app structure with resources.
The next best thing is to split the paragraph such that localized content never contains markup. In the above example I would have 4 para fragments:
1) "Let's have a" as plain text
2) "cup of coffee" as a link
3) "and get" as plain text
4) "energized" as bold text
The problem with this solution is that fragmentation makes maintenace of resources a complete nightmare plus it forces a certain order of the paragraph fragments which might not fit the grammar of all cultures. For instance, in the translated language the proper translation might be
<p>Let's get <b>energized</b> with a cup of coffee.</p>
I don't think I can quite get away with not embedding markup into resources and that might not be a huge deal. Using proper markup/CSS (span, div, id, class) I can create abstractions that would lessen the impact of coupling.
What do I do about the link URLs though? Thanks,
Stefan
Don't view the HTML as "formatting", but as structure, and save all of it to a resource data store (such as resx, or a database, or xml files or something). Then you can stop worrying about little bits of text inside a paragraph. Instead, you'll have some reference to a resource called "paragraph_energized_with_coffee" or something, per locale, and whatever software you use to edit the resources will determine what flexibility editors have in structuring the html inside each resource.
Do it like this:
<%= String.Format("Resources.MyResource.mydata","someURL") %>
and use this as your resource string:
<p>Let's have a cup of coffee and get <b>energized</b>.</p>
I was having the same problem. If I'm reading your question correctly this is what I was able to do to solve it:
In my resources, I added the following text:
<p>Let's get <b>energized</b> with a cup of coffee.</p>
And in my ASP.NET view (using a Razor view), I added the following:
#Html.Raw(String.Format(ShopSavvy.Retailers.Web.Resources.Home.Index.perksReachShoppersBody1, "someURL"))
Doing this gave me all the word-specific formatting I needed for the proper effect.
I'm on Rahuls side - I would consider the html/css of content...just that...content.
The main reason is because when you want to do bulk content updates to a website, you will just have to copy paste from provided html/css into the resx files. It's also a lot quicker to do the orginal templating from html/css into asp.net.
There are also some refractor tools that have "export to resx file" options when content is selected that may speed things up.
Thanks!
I came across this googling the exact same problem. It is intentional, since if you send the resources file to a translator he/she might not understand HTML and could damage your code. I don't like this either.
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&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
</asp:Content>
How would you localize these:
<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&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.</p>