I am trying to localize an asp.net website, however its not working properly.
At the moment I have a user control, which I wish to localize. So inside the UserControls folder, I have created an App_LocalResources folder, and 4 files which will be used for the localization :
UCMain.aspx.resx
UCMain.aspx.it-IT.resx
UCMain.aspx.de-DE.resx
UCMain.aspx.fr-FR.resx
In the UserControls folder, I have the main UCMain.aspx and inside the UCMain.aspx, I have the following simple file:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UCMain.ascx.cs" Inherits="SCPerformance.UserControls.UCMain" %>
<%# Import Namespace="SCPerformance.Shared.Models" %>
<div class="PanelContainer">
<div class="ContentTitle2">
<asp:Label runat="server" ID="lblAboutText" meta:resourcekey="lblAboutTextResource1" />
</div>
</div>
Inside the localized files (resx) I have the following for all the languages:
Name Value
lblAboutTextResource1.Text Informazione
The culture is set correctly when I click on the flags I have, so that is not the problem. The problem is to retrieve the actual text, I am always getting the English version.
What could be my problem?
Thanks for your help and time.
Here's another solution that works for my asp website with 4 languages in my "GlobalRessource" folder.
Have you tried this option?
C# -- overload function InitializeCulture
protected override void InitializeCulture()
{
try
{
string langID = "fr-FR";
Thread.CurrentThread.CurrentUICulture = new CultureInfo(langID);
base.InitializeCulture();
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
}
}
ASP --> <%$ Resources:[resource directory name],[resource label name] %>
<asp:Label runat="server" ID="lblAboutText" Text="<%$ Resources:UCMain,lblAboutTextResource1 %>" ></asp:Label>
Related
I have the same issue mentioned in this question, but the accepted answer doesn't work for me. Basically I simply would like to add translations to elements (e.g. button, label) in a webforms page.
I checked other sites too, they all point to the same solution. E.g. this this MSDN article says:
For example, when localizing content automatically, you can set the Text property of a server control using expression syntax, as in this example:
<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />
In the App_GlobalResources folder, you could have resource files named Messages.resx, Messages.es.resx, Message.de.resx, and so on—a Messages resource file for each language you want to support
In my case, the translation is not picked up, I see always the neutral language (English).
I made a dummy skeleton project too with only a few lines of code, there I have 2 resource files in the App_GlobalResources folder:
MyResources.resx
MyResources.nl.resx
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("nl");
}
Default.aspx
<%-- this displays the translation (nl) --%>
<%= Resources.MyResource.MyKey %>
<%-- this displays the neutral language (en) --%>
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: MyResource, MyKey %>" />
Any ideas what do I miss?
After several tries (and failures) I got it working. This answer and this MSDN page put me on the right track.
In short, I needed override the InitializeCulture method like this:
protected override void InitializeCulture()
{
UICulture = "nl";
Culture = "nl"
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nl");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl");
base.InitializeCulture();
}
To make it a general solution, I had to create a new PageBase class, which is inherited by all pages, as it is nicely described here.
First of all, I'm very new to asp.net (Few days worth of experience).
I am using the default content provided by Visual Studio 2013 to work on a Web Forms Application.
Scenario: I would like to change the text of a Histamine(h2) after clicking a button.
Problem: I am using the default master page provided by Visual Studio but it is not loaded correctly on postBack. From what I can tell the resources it uses for it's layout can't be accessed? I'm not entirely sure what I'm doing wrong. If someone could enlighten me I would very much appreciate it.
My code so far:
Default2.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
<h2 id="h1" runat="server">Change me please.</h2>
<asp:Button ID="b1" Text="Submit" runat="server" OnClick="dothis" />
</asp:Content>
Default2.aspx.cs:
public partial class Default2 : Page
{
protected void dothis(object sender, EventArgs e)
{
Header.InnerHtml = "Hello world.";
}
}
See change html tags text , on server side (C#, ASP.NET) for the code you need to implement. In your case:
h1.InnerHtml = "Hello";
As an alternative, instead of using an <h2> server control, you can wrap something like a label or literal control within the header tag, like this:
<h2><asp:LiteralControl runat="server" id="HeaderLiteral">Change me please</asp:LiteralControl></h2>
Then in your code-behind you can do this:
HeaderLiteral.Text = "Hello World!";
Personal preference but I generally shy away from Server Controls. No particular reason, but offering this suggestion for completeness sake.
I am going to write a web form via Visual Studio 2013 with Devexpress v14.1.
The web form is required to change the language with CheckedChanged event.
I have read some of the articles from Google,
but it seems that it required to set all of the controls one by one.
For example, if there are 30 labels in my web page, it is necessary to add 30 lines:
Label1.Text = ...;
Label2.Text = ...;
...
Label30.Text = ...;
What is the best approach to make multi-language web page ?
Please help!!!
Implementing multi-lingual is not as simple as you think.
Prerequisite: -
All controls which need multi-lingual on your page should be server control. e.g.
<Label runat="server" ID="lblName" Text="Type your name"></Label>
Create a resource file
To generate a local resource file from an ASP.NET Web page
Open the page for which you want to create a resource file.
Switch to Design View.
In the Tools menu, click Generate Local Resource. (It will create the a resource file in local resources folder)
Type values for each resource that you need in your application, and then save the file.
Read more Creating Resources From a Web Page
When you have successfully created a default resource file (e.g. mypage.resx) then copy/paste it and rename the copied file with the language specific e.g. mypage.fr.resx for french
Change the values to the language specific values
Asp.net uses the resx file based on the current thread culture but the problem is CheckedChanged event occurs after the Page_Load event so CheckedChanged event method is not the correct place to change thread culture.
So you will need to capture the CheckedChanged value manually in Page_Init event (which occurs before Page_Load) event from Request.Form values and set the culture
Or inside CheckedChanged save a value in session or cookie and reload the page and in Page_Init use the session/cookie value to set the thread culture
I hope these articles ASP.NET Web Page Resources Overview and How to: Retrieve Resource Values Programmatically can help you to solve your problem:
Creating Resource Files for ASP.NET Web Sites
Working with Resources in Web Pages
Selecting Resource Files for Different Languages
eg.
<%# Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text =
GetLocalResourceObject("Button1.Text").ToString();
Image1.ImageUrl =
(String)GetGlobalResourceObject(
"WebResourcesGlobal", "LogoUrl");
Image1.Visible = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click"
Text="Get Resources" />
<asp:Image ID="Image1" runat="server"
Visible="false" />
</div>
</form>
</body>
</html>
Take into account several aspects:
1. You have to keep track of the UICulture in your session (for example, using your SessionManager class). Then, you have to initialize in on your Page.
Code in your SessionManager class:
public string SUICulture
{
get
{
if (HttpContext.Current.Session["SUICulture"] == null)
{
HttpContext.Current.Session["SUICulture"] = "es";
}
return HttpContext.Current.Session["SUICulture"].ToString();
}
set
{
HttpContext.Current.Session["SUICulture"] = value;
}
}
Code in your Page:
protected override void InitializeCulture()
{
String currentUICulture = clsSessionManager.GetInstance().SUICulture;
if(currentUICulture != null){
UICulture = currentUICulture;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentUICulture);
}
base.InitializeCulture();
}
2. Change the UICulture on the specific event of your page (onCheckedChanged in this case). In this example the page just offers two possible languages, English or Spanish
protected void chckOnCheckedChange(object sender, EventArgs e)
{
if (clsSessionManager.GetInstance().SUICulture== "es")
{
clsSessionManager.GetInstance().SUICulture= "en";
}else
{
clsSessionManager.GetInstance().SUICulture= "es";
}
Response.Redirect(Page.Request.Url.ToString(), true);
}
3. Change the labels of your page. Best approach: use resource files i.e. you can have two resource files.
You have to use server controls in order to achieve this. For example:
<asp:Literal ID="lblTitle" runat="server" />
Then you have to change it in your codebehind:
lblTitle.Text = GetGlobalResourceObject("Default","labelTitle").ToString();
You will find more information here
https://msdn.microsoft.com/en-us/library/fw69ke6f.aspx
4. Beside using resource files for the content of your page there is a possibility that you need to translate the menu as well (when using a sitemap).
This page show you how to do that https://msdn.microsoft.com/en-us/library/ms178426.aspx
I'm trying to reference to a .dll file from a .aspx web page. However, I get the following error:
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: There is no build provider registered for the extension '.dll'. You can register one in the <compilation><buildProviders> section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
Source Error:
Line 2: <%# Page Title="" Language="C#" MasterPageFile="CSharpBPTestMaster.master" AutoEventWireup="true" CodeFile="CSharpBPTest.aspx.cs" Inherits="Button" Debug="true"%>
Line 3:
Line 4: <%# Register tagprefix="blnc" tagname="Balanced" src="bin/Debug/BalancedTest.dll" %>
Line 5:
Line 6:
Source File: /preview/1/balanced-csharp-master/src/BalancedTest/CSharpBPTest.aspx Line: 4
I'm not sure what I'm doing wrong. I build the .csproj file. I have the following in bin\Debug\:
Balanced.dll
Balanced.pdb
BalancedTest.dll
BalancedTest.pdb
Here is my CSharpBPTest.aspx:
<%# Page Title="" Language="C#" MasterPageFile="CSharpBPTestMaster.master" AutoEventWireup="true" CodeFile="CSharpBPTest.aspx.cs" Inherits="Button" Debug="true"%>
<%# Register tagprefix="blnc" tagname="Balanced" src="bin/Debug/BalancedTest.dll" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<form id="form1" runat="server">
<div>
<asp:Button ID="button1" runat="server" Text="Submit" OnClick="Button_Command"/>
<br />
<br />
<br><asp:label id="warningLabel" Text="" ForeColor="Red" runat="server"/><br>
<br />
</div>
</form>
</asp:Content>
And in my C# file, I "import" the project like this (at the top):
using Balanced;
I want to be able to use this compiled .dll file on my C# side. The Balanced.dll is an external library. It just came with the files and a .csproj file. I did a build and now I'm trying to use this Balanced.dll file. Am I missing something? I'm sorry if this is a bad question. I'm new to asp.net and csproj.
You can't do that. You need to specify the namespace, not the src. src is for usercontrols/servercontrols within the project.
Your namespace value will depend on how the DLL was compiled and its namespaces are structured.
Also, you do not specify the tagName in this case. The tagName will be the DLL control's class name.
A very basic example is:
<%-- assumes that 'BalancedTest' is the namespace that this control is in, and that the BalancedTest.dll already has a class that inherits UserControl (or a child of it) with the name 'BalancedTest'. --%>
<%# Register tagprefix="blnc" Namespace="BalancedTest" %>
I started to build a website using Umbraco and I noticed that button click events (and probably other events) are not working.
I created simplest usercontrol with one button, added it to a page, When I debug it the Page_Load is called (breakpoint being hit), but not button click.
The code is very standard, but here it is:
.aspx file
<%# Control Language="C#" AutoEventWireup="true" CodeFile="TestControl.ascx.cs" Inherits="usercontrols_TestControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
and code behind:
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = "Button clicked!";
}
Where can be the problem?
Make sure you are wrapping your body with <form runat="server">...</form> tags, the user control/macro should be inside the form tags. Also ensure that you are adding the user control correctly. To help you out with this, here are a few resources:
A demo by Niels Hartvig. (Niels is using a current Umbraco version.)
Tim Geyssens' screencast. (Tim is using an older version of Umbraco in the screencast (not 4.7.*), but there isn't much difference.)
Step-by-step instructions by Skiltz.