I want to internationalize my asp.net application. How to do this? What steps exactly do I have to follow?
1.) If you use database, then you must modify your tables. At least with adding the LCID column.
2.) Set default culture and UI culture in web.config
<system.web>
<globalization culture="cs-CZ" uiCulture="cs-CZ"/>
</system.web>
3.) Then you can set actual thread culture either in global.asax in e.g. BeginRequest event, or in base class of your page classes in InitializeCulture method
protected override void InitializeCulture()
{
string language = Request["lang"];
if (!string.IsNullOrEmpty(language))
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
}
else
{
base.InitializeCulture();
}
}
For static texts you can use Resources. E.g. you create Mytexts.resx where you write texts for default laguage (en-us) and then you create Mytexts.en-UK.resx for uk english and overwrite text that are different from default laguage. Then you can insert this strings in your page :
<asp:Label runat="server" Text='<%$ Resources: Mytests,WelcomeMessage %>' />
This are only briefly steps for beginning with localization, but for small pages / apllications is it sufficient.
Simply make a basepage class that will inherited from Page class, put this method in basepage class and inherit basepage class in your every aspx.cs page to acheive globalization.
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
base.InitializeCulture();
}
set in this method whatever culuture you want, like ar-sa for arabic....
You have to explore the topics of using resource files in your web application. If you need database based localization support you may try the excellent free product from westwind
Localization and Globalization topics in MSDN are your best bet for this.
It's a pretty big question to be able to give you the exact steps, and there are several different approaches.
The approach we took on my most recent project (simplified) was:
Set up a domain for each country
Create a resource file for all the
hard-coded strings (form labels etc)
for each culture (en-US, de-DE,
fr-FR)
Change the Thread.CurrentCulture based on the domain the site is
being accessed from - this means
that all your number formats, date
formats will be correct and use the
correct localised resource file
Hope this helps!
See here for the Microsoft white papers on Internationalization.
This is a complex topic and requires a lot of work to get right, through all layers of your system.
Start here.
ASP.NET resx files which will let you configure constant strings easily, but your DB will also need to support unicode, and you'll need to do different things depending on the languages you wish to support.
Good luck, and ask questions when you have specific problems.
See this link on creating localized resource files: http://msdn.microsoft.com/en-us/library/ms247246.aspx
Basically you create a new resource file each language/culture you want to support. Then you access the strings inside them by name in your markup pages and code behind files.
Additionally these resource files need to be in a specific folder in your project called: App_GlobalResources
Global resource files must be in the App_GlobalResources folder. If you
try to create a .resx file outside of
this folder, Visual Web Developer
prompts you to create it in the
folder.
Related
I have sharepoint application, that needs to be made support Globalization(multi language support).
I am planning have a drop down box with list of languages.
Problem: By Default the CurrentCulture is en-US as expected.Lets say the if user choose some other language(chinese) from the dropbox then I need to set CurrentCulture to chinese so that it can access corresponding resource xml file.
I tried overriding the currentculture based on user selection.But it is not getting effected for all the threads.
I tried setting in web.config even that it don't work.
Please suggest how to change the CurrentCulture and CurrentUICulture for entire application(threads) based on user selected language.
Regards,
Archu
Have you tried doing it the Sharepoint way:
http://www.denisstadler.com/sharepoint-2010/sharepoint-2010-publishing-feature/set-up-multilingual-support-in-sharepoint-2010/
If you really need to have control on the culture of the thread you can set them like this:
System.Threading.Thread.CurrentThread.CurrentUICulture =
New CultureInfo("de")
System.Threading.Thread.CurrentThread.CurrentCulture =
New CultureInfo("de-DE")
But only of you are spawning the threads yourself.
I have ASP.NET 4 project (not MVC). I need to create url route based on user input language.
Project has only two languages "he" and "en".
User can enter the site and if his culture is set to anything besides he-IL i want to re-route him to website.com/en/ otherwise to website.com/he/
Default.aspx should remain same page which uses Globalization features translate values based on user's culture settings in browser.
How can i do that? what should i do besides writing a route in Global.asax and How to write this route.
This shouldnt be hard. Yes the Global.ascx is the best place to start.
First map the Routes,
protected void RegisterRoutes(RouteCollection routes)
{
//Contact route for EN culture
routes.MapPageRoute(
"contactRouteEN",
"en/contact",
"~/Contact.aspx"
);
routes.MapPageRoute(
"contactRouteHE",
"he/contact",
"~/Contact.aspx"
);
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
That much establishes the routes.
The problem your describing sounds more like a Globalization issue than url routing problem. The url portion of the issue will be cosmetic to the user but won't attack the underlying issue in my view. ASP.Net provides facilities for Globalization. For example you can use LocalResources. To do this for the pages at your applications root level (not nested inside folders)
Right click the website and choose Add ASP.Net Folder
Choose App_LocalResources.
Right click the App_LocalResources folder and choose Add Item
Choose Resource File.
It is important that you name the file according to the culture you plan to target
You can create the first file to be Contact.aspx.resx to be the default resource file (maybe english?)
ASP.Net will try to find the most specific culture to match the resource files to and will resort to the default if a more specific is not provided.
The naming convention follows PageName.aspx.languageID-cultureId.resx
You could have Contact.aspx.he.resx
In a label control for example you could set it like this
<asp:Label ID="lbContactMessage" runat="server" Text="something" meta:resourcekey="yourmatchingkeyfromresourcefile"></asp:Label>
For more info see
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx
Seems like you are trying to do something unintended with Routing.
If the language in URL does nothing and you need for it to appear in URL only then you either make hackish solution with HTTP modules rewriting urls to remove it and add back to generated html or simply map the same site to two virtual folders /en and /he in IIS and make a simple Default.aspx page at / to redirect to appropriate one based on user culture.
Is it possible to compile a web application project .ascx (user control) into a dll?
I want to do the following:
Use the same control in multiple websites
Embed css and .js as resources into the control
Be able to update super easy. If the user control updates, I just want to update 1 .dll
I have successfully followed this article, http://msdn.microsoft.com/en-us/library/aa479318.aspx.
However, that uses web site projects, and I cannot embed js css as resources into web site projects.
Any ideas? Am I going about this wrong, should I change the approach?
Conversion is easy, and could even be fully automated. It simply requires changing a few settings and base classes in the DLL Project you want your ASCX controls embedded in.
1... For each UserControl, set the ASCX file's Build Action (under Properties) to "Embedded Resource", and delete its associated designer file.
2... Save the project.
3... Right click the project and choose "Unload Project".
4... Right click it again and choose the "Edit *.csproj" option.
Change sections that look like this (where the asterisk represents your class name):
<Compile Include="*.ascx.cs">
<DependentUpon>*.ascx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
to look like this
<Compile Include="*.ascx.cs" />
That will cause the code-behind files to be compiled independently of the ASCX files.
5... Save changes, and right click the project and choose "Reload Project".
6... Open all your "*.ascx.cs" files and make them inherit from the following custom UserControl class, instead of the System.Web.UI.UserControl class (you may need to locate parent classes to complete this step).
public class UserControl : System.Web.UI.UserControl
{
protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
string content = String.Empty;
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( GetType().FullName + ".ascx" );
using (StreamReader reader = new StreamReader(stream))
content = reader.ReadToEnd();
Control userControl = Page.ParseControl( content );
this.Controls.Add( userControl );
}
}
This base class will take care of loading and parsing the embedded ASCX file.
7... Finally, you may need to place ASCX files in subfolders so that their resource names (automatically determined by folder path) match the full type name of their associated class (plus ".ascx"). Assuming your root namespace matches your project name, then a class named "ProjectName.Namespace1.Namespace2.ClassName" will need its ASCX file in a subfolder "Namespace1\Namespace2", so it gets embedded with the name "ProjectName.Namespace1.Namespace2.ClassName.ascx".
And that's it! Once you compile the DLL and include it in another project, you can instantiate instances of your user controls using the "new" operator, like any other class. As always, your control will be "caught up" to the current page event once added as a child control to the page or another control on the page.
It is difcult to use user controls in this way due to the markup ascx file. If you want to create reusable control libraries your are much better off creating custom controls.
Another way would be to convert user control to the custom control. There is an nice article on MSDN: Turning an .ascx User Control into a Redistributable Custom Control which describes exactly how to do that. Here is the summary:
Write your user control as you normally would, typically using the Visual Studio designer.
Test it using a simple page before trying to deploy it.
Deploy the application to precompile it.
Grab the user control's assembly produced by the deployment step,
and you're essentially done: You have your custom control.
Finally, use your custom control in other apps the same way as you
always use custom controls.
Hope this helps.
I had to do it once and I followed this article
http://www.codeproject.com/KB/user-controls/EmbeddedUserControl.aspx
It's based on the possibility of mounting a virtual file system on different places (an assembly, database, etc)
There are quite a few articles out there on how to do exactly that:
http://www.nathanblevins.com/Articles/Compile-a-Web-User-Control-into-a-DLL-.Net-c-.aspx
http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx
I know this is old, but yes its possible.. I do it all the time, see https://kocubinski.wordpress.com/2013/05/06/compile-asp-net-webforms-ascx-usercontrols-into-assembly-for-re-use/
I was wondering if its possible to have an ASP.NET AJAX custom usercontrol 'register' its use of CSS files like it does with JS files. ?
Eg: Implementing the IScriptControl interface allows for the GetScriptReferences() to be called by the Script Manager, is there a similar thing for CSS files?
No.
You could write your own methods to use embedded resources and generate html to write them to the page.
As a best-practice approach, though, I wouldn't recommend this. You should have a clear separation of style and markup. If you want to write a custom control that is dependent on a stylesheet, then I suggest that you provide a default css file along with your control.
EDIT (to answer questions in the comments)
You can run this code to add a reference to the CSS file to your page (at runtime). Note that because you're using the RegisterClientScriptBlock method, you can manage insert duplication. Here is an excerpt from the link for the method:
A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.
StringBuilder sb = new StringBuilder();
sb.Append(#"<link rel=""stylesheet"" type=""text/css"" href=""");
sb.Append(this.Page.ClientScript.GetWebResourceUrl(this.GetType(), resourceName));
sb.Append(#""" />");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyCSS", sb.ToString());
This is driving me absolutely nuts.
I created a new WAP project in VS 2008. Copied over the files in my Web Site Project. Added any required references. Tried to convert the Web Project to a Web Application using the "Convert to web application".
None of my user controls are able to see methods in their code behind. They don't even see them so I get errors everywhere saying it doesn't know what this or that method is.
Example:
<%=CreateMenu(xxx.WebMenuType.Occasion, "menuShopOccasion", "Occasion") %>;
That is in my Header.ascx
And so it errors out because it has no clue what CreateMenu is!
In my Header.ascx.cs it's there and was being referenced with no problem in my old Web Site Project:
protected string CreateMenu(xxx.WebMenuType menuType, string menuID, string title)
{
...
}
It's probably a namespace problem. Make sure that the Inherits attribute in your <%# Page ... %> declaration refers to the correct path to the code behind file, including the namespace. The designer file must also be in the same namespace as the code behind.
I am not entirely sure this is your problem but....
you may be missing the .designer.cs files. For your example above there would also be a Header.ascx.designer.cs which contains a partial class (Header) which has all the declarations of the controls in the Header.ascx file?