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.
Related
I am creating a website that will be multilingual in nature.
I am providing a functionality whereby the user can select desired language by selecting it from the drop down.
Now my problem starts here (when a language is selected from the drop down). I am currently implementing 2 languages English and Arabic.
Problem is when the user select Arabic from the drop down on the login page the page is refreshed and the browser loads all the content in Arabic.
But...
When i select English back again, the page refreshes but the language content does not change !!
i have check the code and the values (culture name value) are being applied correctly!!
Any clues as to what is wrong and where...
Here is my code...
protected override void InitializeCulture()
{
String selectedLanguage = string.Empty;
if (Request.Form["ddlLanguage"] != null)
{
selectedLanguage = Request.Form["ddlLanguage"];
CultureInfo ci = new CultureInfo(selectedLanguage);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
base.InitializeCulture();
}
Any help will be great !!
Thanks
The Culture settings must set on each request. It's not enough to set the Thread cultures once when the selection changes.
In order to set the culture according to the user selection on each request, there are several possible ways.
Set the culture in (for instance) the Page_Init event of the master page.
Create custom base page (maybe MyBasePage) for the content pages and override the InitializeCulture method of that class. Then make all the content pages derive from the class instead of directly from Page.
Add code for setting the culture in a suitable event in Global.asax.
Probably there are several other possible ways to handle this as well.
Regardless of which method you will use, the culture that the user has chosen must be available to the code that is going to set the culture on the thread. So when the user changes his/her selection, you must save that selection in a place where you can access it in the coming requests. This can also be solved in several possible ways. Here are some options:
If you are using a ProfileProvider, you could save the selection to the user's profile.
Alternatively you can store it in a cookie.
You could possibly store it in the Session although that would mean that the user must re-select it whenever the Session has been reset.
You could use some other custom way to store the selection (database, some file on disk etc).
For a more detailed exmple of how this could be done using Global.asax and a cookie, have a look over here.
Check this article - How to create ASP.NET MVC multilingual web application ?
We will see mainly two approaches-
Approach 1: Using Static Pages
Approach 2: Using Dynamic page with localized data at runtime
I have several pages in asp.net each with lots of controls. I Also have some roles in my application that each has some setting options. Now I want to prepare my page based on these settings. Maybe it’s not too clear, so please take a look at my example.
Example: There are some buttons, some textboxes, some datetime picker, and a chart in a page, now what I want is when a user sees this page, the controls appear and disappear based on the users role. An important thing is that I don’t want to have only visible and invisible controls, in some scenarios I need to show controls with some customizations. For example change chart data source, limit selecting date time and so on.
The first solution that I can think of, is saving the settings in database and after visiting the page by user, the settings fetch from database and based on those, I can customize the controls with conditional phrases (if and else). But I suppose it is not a good approach and my page will get very messy.
Please help me with any better solutions and if you know good references about it, please let me know.
Please see this link...use of ControlAdapters may help you...
Role-based enabling/disabling of controls in asp.net
You must use Thread.CurrentPrincipal.
A. When user login to your application, you attach his identity to thread, for example
string[] rolesArray = .....; //Get roles from dataBase by identity.
Thread.CurrentPrincipal = new YourCustomPrincipal(new YourCustomIdentity("YouName", "..."), rolesArray);
B. And when you navige about your application you test Thread.CurrentPrincipal
IPrincipal threadPrincipal = Thread.CurrentPrincipal;
if(threadPrincipal.Roles.Contains("roleTest"))
{
//Adjust your control
}
I have created a custom culture for client-specific language, i.e. I have resx files for that culture.
The issue I have is that I can't seem to set the culture early enough in the page cycle. By the time I call Thread.CurrentThread.CurrentUICulture = culture and Thread.CurrentThread.CurrentCulture = culture the page seems to already have picked the language from the base resx file :(
So I'm having to set the culture and then redirect back to the same page?
I've tried setting it in the Page_PreInit and its still not early enough? Is there an earlier event I can hook into?
You need to override the page's InitializeCulture() method, look here for details.
I've implemented localization in this ASP.NET project using resource files and that's working as expected.
But there is one situation where I have to display the whole page in one language and then a section of it in some other language based on the selection from a dropdown list.
From what I can see, the GetGlobalResourceObject() and GetLocalResourceObject() methods work from the page culture, but I need to adjust the language programmatically. Is there a way to override those methods' behaviour so that I can specify the language?
I believe you can access a specific resx file by using this method:
value = HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture)
You can initialize your culture like this:
Culture culture = CultureInfo.CreateSpecificCulture("pt-PT");
Just pick up your value from the dropdown and switch the culture in the CreateSpecificCulture method.
I havent tested it, let me know if that doesnt work.
EDIT: its the same for GetLocalResourceObject
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.