I just started using explicit resource files. I performed these steps:
In the root create the folder: App_GlobalResources
Add two resx files: LocalizedText.en-us.resx and LocalizedText.resx
In both files I have a value called 'InstitutionTitle'
In LocalizedText.en-us.resx the value is 'Institution' and in the LocalizedText.resx the value is 'Instelling'
In my .aspx file I have the following label:
<asp:Label ID="lblInstitution" runat="server" Text="<%$ Resources:LocalizedText, InstitutionTitle %>" />
When I run this page, I always get the dutch version. Whether I set the language in my browser (FF and IE7) or not, I always get the dutch version. When I request the browsers' language I get en-us (using: Response.Write(Request.Headers["Accept-Language"]);).
What's the issue and how can I fix it?
Setting the language preferences in the browser is not enough. You have to make sure that the current thread's Culture and UICulture properties are set accordingly in ASP.NET.
You can do this either programmatically or declaratively on your page (Culture and UICulture attributes of the <%#Page %> directive).
Or you can let ASP.NET set them automatically by setting the web.config entry shown below and setting the Culture/UICulture properties of the page/masterpage to "auto".
// web.config:
<globalization enableClientBasedCulture="true" ...>
// page/masterpage:
<%# Page ... Culture="auto" UICulture="auto" %>
Check this page for details.
#martijn:
check the browsers caching settings.
Caching should be off all the time
when developing.
Install Firebug (FF) and Fiddler(IE)
to see what's being transfered over
the wire.
Hope this helps...
signed,
A fellow countryman
Related
I cannot post my code but I want to know how I can write in resource file and other the link text in masterpage like and dropdownlist multilinguage in asp.net c# website.i need to use global app.global resources due to my desire to localize it in many languages to handle the files.i will be happy if you show me in asp.net website only i do not have much knowledge in mvc and web application and moreover what i started to do is in asp.net website.
<li>Buy</li>
<li>Rent</li>
<li>Estimate</li>
<% if (Session["user"] == null)
{ %>
<li>Sell</li>
<% }
else
{ %>
<li>Sell</li>
<%} %>
<asp:ListItem>Select the Category</asp:ListItem>
<asp:ListItem>Villa</asp:ListItem>
<asp:ListItem>Apartment and Condos</asp:ListItem>
<asp:ListItem>Farm</asp:ListItem>
<asp:ListItem>Office</asp:ListItem>
<asp:ListItem>Store</asp:ListItem>
<asp:ListItem>Storey House</asp:ListItem>
<asp:ListItem>Plot</asp:ListItem>
<asp:ListItem>Shop</asp:ListItem>
<asp:ListItem>Other commercial</asp:ListItem>
</asp:DropDownList>
Add resource files for the different languages, e.g. LocalizedText.resx (fallback language), LocalizedText.fr.resx (french), LocalizedText.de.resx (german), etc.
Add keys and values for the texts to be translated to these resource files, e.g. Villa, ApartmentAndCondos.
Then ASP.net will resolve the correct language version of the resource file to be used based on the language sent by the browser.
<asp:ListItem><%$ Resources:LocalizedText, Villa %></asp:ListItem>
<asp:ListItem><%$ Resources:LocalizedText, ApartmentAndCondos %></asp:ListItem>
You can use the <%$ Resources:{FileName}, {ResourceKey} %> command anywhere to return translations. E.g. in a link:
<%$ Resources:Resources:LocalizedText, Sell %>
See Walkthrough: Using Resources for Localization with ASP.NET.
I'm trying to use resource files in an ASP.NET Web Forms application (.NET 4.0). I'm using VS2012. I have the following files inside the App_GlobalResources folder:
Address.resx (default language, English)
Address.ja-JP.resx (Japanese)
The problem is when I'm trying to display the text in Japanese in an ASP.NET page (*.aspx file). If I use the following syntax everything works fine:
<%= Resources.Address.Street1 %>
But when I try to bind it to a property of an asp:Label control the default text (English) is displayed instead of Japanese:
<asp:Label ID="lblStreet1" runat="server" Text='<%$ Resources:Address,Street1 %>'></asp:Label>
BTW culture is being set in session variables and then in the master page I have something like this:
Thread.CurrentThread.CurrentCulture = (CultureInfo) Session["ci"];
Thread.CurrentThread.CurrentUICulture = (CultureInfo) Session["uci"];
Also, I don't know if this is relevant or not but I generated the Address.ja-JP.resx outside Visual Studio (using Notepad++) and then moved the file to the App_GlobalResources folder and included the file in the solution.
Am I missing something here?
I was able to find a solution to my problem. In the code behind I had to override the InitializeCulture method, I did something like this:
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentCulture = (CultureInfo) Session["ci"];
Thread.CurrentThread.CurrentUICulture = (CultureInfo) Session["uci"];
base.InitializeCulture();
}
I would recommend you look in to using meta:resourcekey on the label control. In your case you could then use:
<asp:Label ID="lblStreet1" runat="server" meta:resourcekey="myStreet1Label"></asp:Label>
The resource key in your resx files would then be like this:
<data name="myStreet1Label.Text">
<value xml:space="preserve">The street data.</value></data>
I was recently assigned a task of changing our asp.net web site localization to use custom resource provider (using sql database) instead of the default asrx resource files. Right now I'm chalenged with replacing hundreds of meta:resourcekey="resource-key" with '<%$ Resources:[filename,]resource-key %>' in our web site too many web pages. I want to do it programmatically.
first of all I'm not able to open .aspx files using XmlDocument, then I wonder how can I read meta:resource entries inside the aspx file as meta:resource is not any regular node attribute. any thoughts or example code how to solve this.
Thx.
Note: in the inserted '<%$ Resources:[filename,]resource-key %>' filename name sould be based on the aspx file name & resource-key on the control type and the resource value.
exemple: in UserPage.aspx page <asp:Label id="uid" meta:resource="userName"> should be replaced with <asp:Label id="uid" Text='<%$ Resources:UserPage,LBL_userName_text %>'.
Html is not valid xml, so no wonder an XmlDocument didn't work. Especially with that <%$ .. %> syntax.
Why not read it as plain text and search for the string "meta:resourcekey"?
This isn't a programming answer, but a utility like PowerGREP may be a viable solution.
I'm working on a web form which works in a following way.
Read email template from database
Display email template on a web form in HTML format
User adds additional information to the web form and clicks on submit button
Before I get to a method which will process that request, I get A potentially dangerous Request.Form
I have looked at few articles that advise using .Net 2.0 in one of the web.config sections - that didn't work. I have set requestValidation = "false" for that page and it didn't work either.
My gut feeling is that I'm doing something fundamentally wrong...
HTML template is stored as VarChar(4000) in a database.
I have tried encoding text in a method before I send an email, but that didn't work either because the web form never got to executing that method.
What other options do I have? I have tried storing plain text in database, but then I have issue of tabs and returns etc.
Thank you
The remedy is in two parts and you MUST action both:
To disable request validation on a page add the following directive to the existing "page" directive in the file (you will need to switch to the HTML view for this):
ValidateRequest="false"
for example if you already have:
<%# Page Language="vb" AutoEventWireup="false"
Codebehind="MyForm.aspx.vb"
Inherits="Proj.MyForm"%>
then this should become:
<%# Page Language="vb" AutoEventWireup="false"
Codebehind="MyForm.aspx.vb"
Inherits="Proj.MyForm"
ValidateRequest="false"%>
In later versions of Visual Studio the value of this property is available via the page properties, so simply set "ValidateRequest" to "False". Either method of setting this achieves the same result.
Alternately, you can globally turn request validation off (but in which case be sure to implement item two below). To globally turn request validation off add the following to your web.config file:
<pages validateRequest="false" />
From: http://www.cryer.co.uk/brian/mswinswdev/ms_vbnet_server_error_potentially_dangerous.htm
As a first security lesson, never trust user input,so if you setting request validation to false then always HTML encode the input.
In basic either use: OnClientClick on submit and replace, < with & lt; and > with & gt; (no space with & and gt/lt)
or on submit method, use Server.HTMLEncode(inputtext)..or however you process it.
I have 2 links, one english one spanish. is there anyway i can localize that in .net? I was thinking of using sitemap
Sure, you can use resources for that, either global or local.
For local resource (pertinent to that .aspx page), switch to design mode, click Tools -> Generate Local Resource.
This will create the "meta:resoucekey" bits for you for all of your server side controls and also will generate the resource file under a "App_LocalResources folder". Copy that resource file and rename it to the spanish that you want to use.
For example If your file name was mypage.aspx, the local resource generated would be mypage.aspx.resx and your new file will be mypage.aspx.es.resx (this will use traditional spanish). Markup below:
<asp:HyperLink runat="server" ID="myLink" Text="The Link" meta:resourcekey="myLink" />
In the local resource files, make sure the key matches the id: keys should be "myLink.NavigateURL" and the values should be your link
If you go the global resource route, then just add new resource file to the App_GlobalResources folder in the solution, name it whatever you want and make a copy of it for the other language. For example the new global resource is MyGlobalResources.resx. and the spanish resource MyGlobalResources.es.resx. Now put the following in your markup:
<asp:HyperLink runat="server" ID="myLink" NavigateURL="<%$ Resources:MyGlobalResources, myLink %>" Text="The Link" />
In the global resource files, the key is whatever your want. Just make sure it matches what you put in the markup: in this case the key should be "myLink" and the value should be your url.
Now, when you switch languages, the correct resource file should pick up and the correct URL should be shown.
P.S. You can localize lots of properties from the server side controls. I hard coded the "Text" property just for simplicity.
Here are a few links that go over localization:
http://msdn.microsoft.com/en-us/library/ms227427.aspx
http://www.west-wind.com/presentations/wwDbResourceProvider/introtolocalization.aspx
http://msdn.microsoft.com/en-us/magazine/cc163566.aspx
Hope I was clear enough :)
EDIT
I don't know how I completely missed the "sitemap" factor. The answer is yes and no. In a single site map you can only localize the Title,Description and custom attributes, not the URL. However, you can create a different sitemap for each locale and add them to the web.config file. Here is a link that explains how to do that (bottom of the page): Localize SiteMap
-D