EDIT
The mistake was in the country code. Albania's country code is sq-AL, and not al-AL.
For a full list of country codes: http://timtrott.co.uk/culture-codes/
I don't need to have different routing for different languages.
All I need is by the click of a button to toggle between two languages, Albanian, and English.
What I can't figure out: how to change the language? I'm not worried on how to detect which language needs to be selected, but how to actually change the language.
I have these 2 resource files:
Resources.resx, Resources.al-AL.resx
Always the strings from Resources.resx are used, how can I change so that the strings from Resources.al-AL.resx are used?
I tried something like this:
//I'm just trying stuff I read here in SO, none worked so far.
Resource.Culture = CultureInfo.CreateSpecificCulture("al-AL");
HttpContext.Session["culture"] = "al-AL";
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("al-AL");
return RedirectToAction("Index", "Home");
And in my view I have this:
#Resource.Culture
#Html.Label(Resource.CustomerName, new { #class = "control-label"})
When I first open the view, the first line is empty, after selecting one of the buttons to change the language, I verify that the Resource's culture is being changed, but the text stays the same.
EDIT:
I even added this to web.config <globalization uiCulture="al-AL" culture="al-AL"/>
Custom folder to hold the resource files
Instead of using culture code al-AL I had to use sq-AL. For a full list of culture codes:
http://timtrott.co.uk/culture-codes/
Related
I have created a custom Sitecore control (extending Control) to be used in the content editor. It calls a service and this services requires a language.
When the control is rendered it returns "en" as language every time, how can I get the user selection from the "language dropdown" in the content editor ?
In my method, I want to do something like this:
Language theLanguage = new Language.Parse("da-dk");
Item theLanguageSpecificItem = (Sitecore.Data.Database.GetDatabase("master")).GetItem(myId, theLanguage);
However, I don't want the da-dk part to be hardcoded, I want to get it from the language dropdown in the content editor. For some reason I always get "en" if I use Sitecore.Sites.SiteContext.Current.Language, Any idea ?
Ok, finally got it. When selecting another language you can request the language by simply doing like this.
string currentLanguage = HttpContext.Current.Request["scLanguage"];
Thank you all for helping.
Have you tried to use the Sitecore Context to get it?
Sitecore.Context.Culture.Name
The selected language is stored in a hidden field on the page:
<input type="hidden" id="scLanguage" name="scLanguage" value="en">
This changes when you change language, and is posted with each request.
string language = Sitecore.WebUtil.GetFormValue("scLanguage");
Internally this just does a HttpContext.Current.Request.Form[fieldName]. If you are extending a control then you may already have access to the language as a property, or take a look at one of the existing Sitecore controls and follow their implementation pattern, e.g. Sitecore.Shell.Applications.ContentEditor.LookupEx:
public string ItemLanguage
{
get
{
return this.GetViewStateString("ItemLanguage");
}
set
{
Assert.ArgumentNotNull((object) value, "value");
this.SetViewStateString("ItemLanguage", value);
}
}
I haven't tried this yet but looks like you may be able to get the selected language in the content editor by parsing the language parameter of the ServerProperties.
Language.Parse(Context.ClientPage.ServerProperties["language"] as string)
I have an application that needs to detect the user's country so I can adjust dates to that expectation for that country. I do have JS that is working already in detecting the locale. Now, I understand that JS is browser side and my CS file is server side. Is there a way to either:
1) Link that JS's answer up to my CS
or
2) Detect the locale within the CS file?
Thank you for your time
Kevin
Well, detecting the user's country isn't really the issue. If you need to format dates, you need to use something like this to detect the current culture rather than the country:
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
You wouldn't want to detect the country as there's plenty of users in a given country that don't use the program in that country's culture. For example, say someone has their system set to Spanish, but they live in the USA. You'd force them to use English, but using this method, you can take it directly from the system setting instead. Being dynamic is good!
That way, when you output a date, you can do something like this:
// Displays dt, formatted using the ShortDatePattern
// and the CurrentThread.CurrentCulture.
Console.WriteLine(dt.ToString("d"));
Reference: http://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx
See also: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx
I'm not sure of the terminology here, but I need to access a specific resource file who's name is the string that is calculated.
For example:
I have a resource file named "resFile-en-GB" I can access a string inside by using "resFile-en-GB.stringKey". Now I need to add the culture info to the end of a string.
string stringA = "resfile-";
string stringB = "en-GB";
string stringC = stringA + stringB;
Now I need to use "stringC" as the resource file name like "stringC.stringKey" How would I go about doing this?
you can access resources via the ResourceManager and then use GetString(key), probably easier in this case:
var resourceManager = new ResourceManager(stringC, Assembly.GetExecutingAssembly())
resourceValue = resourceManager.GetString(key);
You can make your application multi lingual with the use of resource files.
Add the resource files to your solution
That's the obvious first step. For example add a folder to your solution ResourceFiles and add the resx files:
LanguageStrings.resx
LanguageStrings-fr-FR.resx
LanguageStrings-nl-NL.resx
The LanguageStrings.resx will contain the default language, let's say english. The fr-FR will contain the French language and nl-NL the Dutch language. You can just add items to the resource files. For example HelloWorld = Hello world.
Right, so you added the HelloWorld key to your three language strings .resx files with the proper translations.
Set the culture
Somewhere in your application you want to be able to set the language. It could be in some event handler (a listbox or whatever you thought up). There you want to set the chosen culture info on the LanguageStrings.
var cultureInfo = new System.Globalization.CultureInfo("nl-NL");
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
LanguageStrings.Culture = cultureInfo;
This sets the language hardcoded to the Dutch language. Obviously you want to make the "nl-NL" variable so that you can set the language to whatever language the user choose.
Get your localized strings
Finally you want to retrieve the localized strings from the resources files. For example something like:
label.Text = LanguageStrings.HelloWorld;
Here the magic happens. You don't have to define which language resource file should be used to retrieve the localized string. That is done for you because you already set the desired language to your resource file (LanguageStrings.Culture = ..).
That's the basics, I hope this helps you on your way.
ResourceManager.GetString method should help you achieving this.
I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?
Yes! It's called resource (.resx) files. What you do is this:
Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
Create your program in one language, let's say English.
Next, change all your forms to another language like so:
Change the Language property of the form to the other language, let's say Spanish.
Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
Swap back and forth as needed during development.
When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.
And voilà! You're done!
You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g StringResource.Get("My_Message")). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.
You can make a multilingual application in two ways:
By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application.
You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.
By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.
Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below.
Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:
Spanish: (filename.aspx.es-ES.resx)
English: (filename.aspx.resx)
.
Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:
<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> |
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>
.
On your masterpage code behind, create a dynamic link to the Hyperlink tags:
////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION
.
Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.
On PageLoad:
///'LOCALIZATION
//dynamic querystring; add this to urls ---> ?" + Session["add2url"]
{
if (Session["version"] != null)
{
Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
}
else
{
Session["add2url"] = ""; // ENGLISH as default
}
}
///'LOCALIZATION
.
On Click Events sample:
protected void btnBack_Click(object sender, EventArgs e)
{
Session["FileName.aspx"] = null;
Response.Redirect("FileName.aspx" + Session["add2url"]);
}
I hope my descriptions were easy enough.
Good morning,
Apologies for the newbie question. I'm just getting started with ASP.NET internationalization settings.
Background info:
I have a website which displays a <table> HTML object. In that <table> HTML object, I have a column which displays dates. My server being in the US, those dates show up as MM/DD/YYYY. Many of my users plug into this webpage through Excel, via the Data --> Import External Data --> Import Web Query interface. My users, for the most part, are in the US, so those dates show up correctly in their Excel screens.
Now I need to make the webpage work for UK users. As is, they are downloading the dates as MM/DD/YYYY, which makes their spreadsheets unusable since their regional settings are set to DD/MM/YYYY.
My question is:
How do I make it so the web server realizes that the incoming request has a en-GB culture setting? I could engineer my own little custom workaround, but I'm sure I'm not the first programmer to come across this. How do the pro's handle this? I'm looking for a solution that would be relatively simple and quick to put up, but I don't want to just put some crappy buggy piece of my own logic togethe that I'm going to dread 6 months from now.
Thanks a lot in advance,
-Alan.
A couple of points:
The <globalization> element also needs the attribute culture="auto". The uiCulture attribute affects the language used to retrieve resources. The culture attribute affects the culture used for formatting numbers an dates.
As noted in this MSDN article, it is not a best practice to rely exclusively on browser settings to determine the UI culture for a page. Users frequently use browsers that are not set to their preferences (for example, in an Internet cafe). You should provide a method for users to explicitly choose a language or language and culture (CultureInfo name) for the page.
You can allow the browser to set your UI culture automatically if you wish, by opening up the web.config, like this:
<configuration>
<system.web>
<globalization uiCulture="auto" />
...
And then the culture set by the browser will be automatically set in your app. This means that when you have the framework display date/time values, they will be formatted according to the current thread's UI Culture.
This will also help if you are using currency and/or localized text (however you have to provide the localized resources for each culture you support).
You could also accept a query string parameter for overriding the culture settings.
Culture initialization should go in the Page.InitializeCulture method.
protected override void InitializeCulture ( )
{
Thread.CurrentThread.CurrentCulture
= Thread.CurrentThread.CurrentUICulture
= Request.QueryString [ "culture" ] != null ? new CultureInfo ( Request.QueryString [ "culture" ] ) : CultureInfo.InvariantCulture;
//base.InitializeCulture ( );
}
Usage: http://tempuri.org/page.aspx?culture=en-GB