I work out of New Zealand developing a web application for some Romanian clients. The application should default to ro-RO when viewed by a client using a Romanian machine and en-GB for pretty much everyone else at this stage. Problem is ALL machines I have used to test this are defaulting to en-US. That is, machines on Windows Azure European data centers, local machines here in NZ and various machine in Romania which I access via RDP.
So i use this code in a controller to set language based on user defaults:
public static void OnBeginExecuteCore(Controller controller)
{
if (controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG] != null &&
!string.IsNullOrWhiteSpace(controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG].ToString()))
{
// set the culture from the route data (url)
var lang = controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG].ToString();
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
}
else
{
// load the culture info from the cookie
var cookie = controller.HttpContext.Request.Cookies[Constants.COOKIE_NAME];
var langHeader = string.Empty;
if (cookie != null)
{
// set the culture by the cookie content
langHeader = cookie.Value;
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
}
else
{
// set the culture by the location if not specified
langHeader = controller.HttpContext.Request.UserLanguages[0];
if (langHeader.ToLower() == "en-us") langHeader = "en-GB";
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
}
// set the lang value into route data
controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG] = langHeader;
}
// save the location into cookie
HttpCookie cultCookie;
cultCookie = new HttpCookie(Constants.COOKIE_NAME, Thread.CurrentThread.CurrentUICulture.Name)
{
Expires = DateTime.Now.AddYears(1)
};
controller.HttpContext.Response.SetCookie(cultCookie);
}
where
langHeader = controller.HttpContext.Request.UserLanguages[0];
is always en-US. There are in fact 3 entries in this collection however:
but ro is clearly not weighted correctly. This is the same across all machines in all locales.
Globalization in web config is set to auto:
<globalization requestEncoding="utf-8"
culture="auto"
uiCulture="auto"
And regional windows settings are as follows:
Browser:
How can I make this work?
ANSWER
As per Martins answer in comments. Problem was that I had this in apparently all settings the world over.
when what I really wanted was this in Chrome... will get to the other browsers soon.
Ensure that your browser of choice (Internet Explorer, Chrome, Firefox, Safari etc.) has been set up to specify ro as the first language in the User Languages sent over HTTP to the server.
Updating the Windows and/or browser's UI language won't necessarily cause a non-English language ISO code to be sent to the server, which means that you'd just get English returned.
In Firefox, for example, this setting can be found under Options->Content->Languages->Choose.
Related
I am having a C# Asp.net core website with Razor pages and components. I am getting the culture either from Query string or from a External Javascript library(MS Teams). The website works well directly in browser and sets the expected culture. But if the same is in Iframe, Language keep falling back to Browser language for every event. Can someone help. Here is bit of code:
_host.cshtml
#{
string culture = HttpContext.Request.Query["culture"].ToString();
if (culture != null && culture.Length > 0)
{
var cultureVal = new CultureInfo(culture);
this.HttpContext.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(
new RequestCulture(
cultureVal,
cultureVal)));
}
}
All DateTimes are stored like this in the database:
myDate.ToUniversalTime()
I can see the dates are stored correctly in the database i.e. in UTC format.
When I, from an Android application created in Xamarin, create an object using parameters the newly created object is returned to the application and the UTC time is sent back correctly in that object.
However, when I close the app the same object does now have another timestamp i.e. not the UTC time stamp.
App -> CreatePayment(int amount) -> ASP.NET MVC does it's magic and returns the newly created object for the app to save locally.
In an example object the returned date was 21/10/2016 21:53:01.
Now I restart the app and the returned date value is 22/10/2016 4:53:01.
How could this happen?
Do I need to set some setting so that ASP.NET MVC knows that the dates loaded from the database are in UTC format?
Can Application_BeginRequest cause these problems with my settings?
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
if (cookie != null && cookie.Value != null)
{
var abbrivation = cookie.Value;
Thread.CurrentThread.CurrentCulture = new CultureInfo(abbrivation);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(abbrivation);
}
else
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
}
}
You can try to add <globalization> parameters in the web.config as shown below:
<system.web>
<globalization culture="es-MX" uiCulture="es" />
</system.web>
On the other hand, if the problem cannot be solved by applying this settings, you might have a look at MVC - UTC date to LocalTime.
I am developing a site in asp.net in multiple languages but i didn't understand how this can be done because we can manage multilingual language by using resource files. we did this, but my major problem is that how we can change globalization at run time for a particular user. if A user choose English language then he/she can view this i English and if B user choose Spanish then he/she can view this site in Spanish. How we can do this? or how we can choose a particular language resource file???
use this code
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); //'en-US' these values are may be in your session and you can use those
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");//'en-US' these values are may be in your session and you can use those
base.InitializeCulture();
}
I had this same question when i started developing multilingual sites and i found those two articles as the best starting point:
http://www.codeproject.com/KB/aspnet/localization_websites.aspx
http://www.codeproject.com/KB/aspnet/LocalizedSamplePart2.aspx
you could try something like this:
string culture = "en-US"; //could come from anything (session, database, control, etc..)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
I think it works!
you need to use localization for language and individual resource file. Now when your site is being access at client side you need to check the locale setting on client's machine his date/time setting and the Default language ... on the basis of this you can provide language that user wish...
We are using the following code to create the security cookie. Everything works fine in Staging environment, however in the production environment the following code is unable to create a cookie in Safari, Chrome or IE but it does create a cookie successfully in Firefox. anything that you guys think i am missing or is wrong in here ?
public static void SetAuthenticationCookie(CustomIdentity identity)
{
ConfigSettings configSettings = ConfigHelper.GetConfigSettings();
string cookieName = configSettings.CookieName;
if (cookieName == null || cookieName.Trim() == String.Empty)
{
throw new Exception("CookieName entry not found in Web.config");
}
string cookieExpr = configSettings.CookieExpiration.ToString();
string encryptedUserDetails = Encrypt(identity);
HttpCookie userCookie = new HttpCookie(cookieName.ToUpper());
if (cookieExpr != null && cookieExpr.Trim() != String.Empty)
{
userCookie.Expires = DateTime.Now.AddMinutes(int.Parse(cookieExpr));
}
userCookie.Values["UserDetails"] = encryptedUserDetails;
userCookie.Values["Culture"] = configSettings.Customer.Culture;
MyContext.Current.Response.Cookies.Add(userCookie);
}
Safari and IE8 don't accept third-party cookies by default.
When you call out to another domain using JSONP, every cookie set by that script will be blocked by Safari and IE8. There is nothing you can do about that (in IE8, you could add a P3P policy, but that doesn't work in Safari).
There are workarounds for maintaining state across JSONP calls, but it's pretty complicated (you'll have to manage state manually and use document.cookie in the called javascript)
As an alternative, you can ask your users to lower the privacy settings in their browser, but this isn't something worth considering IMHO.
did you check whether you have Web Developer add-on and disabled cookies? or disabled cookies inside of FF?
I've seen this issue related to the server having the incorrect UTC date/time. Firefox accepts regardless of the server date/time but other browsers won't set the cookie if the date/time is outside of a certain margin of error.
How can I make a website multilingual?
I want to create a website and in the home page i want the client to choose a language from English and Arabic. Then the whole website is converted to that language. What should I do to achieve this? I am creating this website in asp.net 2.0 with C#
What you're asking for is a tutorial, which you really should try googling for. Look at the links below, if there's something particular, more specific you don't understand - ask the question here.
http://www.beansoftware.com/ASP.NET-Tutorials/Globalisation-Multilingual-CultureInfo.aspx
http://www.asp.net/learn/Videos/video-40.aspx
http://www.about2findout.com/blog/2007/02/aspnet-multilingual-site_10.html
Good luck!
ASP.NET can use a number of mechanisms to change language settings - however you will need to perform the translations your self.
You could look at using Resource files for the common elements of your site - see this answer to Currency, Calendar changes to selected language, but not label in ASP.NET
However, for the main content you'd probably want to do something with the URL to ensure that your content is served correctly - the links that Honsa has supplied would be a good place to start.
Sample code i have done using resource file add global.asax
void Application_BeginRequest(Object sender, EventArgs e)
{
// Code that runs on application startup
HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"];
if (cookie != null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
}
else
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
}
}
http://satindersinght.blogspot.in/2012/06/create-website-for-multilanguage.html
http://satindersinght.wordpress.com/2012/06/14/create-website-for-multilanguage-support/
For Arabic you need to change the direction left to right