I have a webpage that has to be displayed in several different languages based on user selection. For that, I'm using RESX files for each of the asp.net webpages. I don't want to used the automatic detection of the language in the browser, but I want to set the language, again, based in the user selection. In order to accomplish this I'm doing the following:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-MX", false);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-MX", false);
OR
Page.Culture = "es-MX";
Page.UICulture = "es-MX";
But neither of those are working as expected! I'm initializing the Culture in the Init method of the page but it will always display the default language. I'm inspecting the values of those properties and those have the culture correctly, but still is not being rendered using the RESX file. Any ideas? Suggestions?
Thanks
In case someone runs into this issue when working with Explicit localization, here is what has to be done:
protected override void InitializeCulture()
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}
From the net-tutorials.com website:
Since the Page directive is just a shortcut to the Page class, this can be done from CodeBehind as well. However, we have to do it at a certain point, before the page is being rendered, to make sure that it has the desired effect. This is where the InitializeCulture() method comes into play, a method that is called by ASP.NET pretty early in the Page life cycle, which you can override.
Try this
System.Resources.ResourceReader resourceReader
= new System.Resources.ResourceReader("RES_PATH");
Now you can use this to load users language like es.resx
System.Resources.ResourceReader resourceReader
= new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0]
+ ".resource");
Related
I know it's easy to localize Windows Forms App: set Localizable=True, change Language and set text in Controls for every Language. This information saves in resx-files and application will automatically select the required file. Great!
I know about disadvantages of this solution (you need to rebuild the app if there a typo, it's impossible to change language in runtime, etc), but it's not a problem for me and "resources" is the simpliest, built-in solution.
But this mechanism uses the property Culture of app's thread.
My app is the part ("plugin") of the bigger application and works in the same Thread.
The main application is multilingual too, but it doesn't use Culture to change interface's language. I can change the thread's culture globally, but it crushes the main app's interface.
So my question:
is it possible to manually set the resx-localizable resurce file that will be used? Not based on Culture, but, for example, on some variable in my app:
if (this.Language == "fr")
this.Resources.Add("Form1.fr.resx");
else
this.Resources.Add("Form1.en.resx");
Or something else.
Thank you!
My sandbox:
https://github.com/Tereami/WindowsFormsTestLanguage
The built resources file has a property ResourceManager that's used to return the desired content. This has an overload with a CultureInfo parameter. You can use it to request resources in individual languages:
var desiredCulture = new CultureInfo("en-us");
var text = MyStrings.ResourceManager.GetString(nameof(Resources.ExitMessage), desiredCulture);
If you want to set the culture globally for your resource file, you could also set it through the corresponding property:
var desiredCulture = new CultureInfo("en-us");
MyStrings.Culture = desiredCulture;
var text = MyStrings.ExitMessage;
I created two resource files, "SOSResources.en.resx" and "SOSResources.pt.resx".
In my aspx page, when I try to use a string from these files as below, I get it from the standard English language resx file.
asp:Literal ID="btnDelete_Text" Text="<%$Resources:SOSResources, String1%>"
However, if I try to get the same string on code-behind as below, I get it from the Portuguese language file (which is the user language).
btnEdit_Text.Text = Resources.SOSResources.String1
The following code is used to handle this process. It is supposed to select the User's language, or English in case this info is not available. However, only the example from code-behind gets the user language. The example from aspx page always gets the strings from the English resx file.
protected override void InitializeCulture()
{
string lang;
if (ActiveUser != null && ActiveUser.Language != null)
{
lang = ActiveUser.Language;
}
else
{
lang = "en";
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
base.InitializeCulture();
}
Can someone help me? How can I assure that in both cases the page will get the strings from the appropriate language? I'm not sure either if this is the correct way to perform globalization on .NET.
After many hours of research I found this example that worked for me:
https://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.85).aspx
In addition to that, there was another question which also helped me when trying to move the code to the Site:Master or to a PageBase.cs:
ASP.NET Web Page Globalization & Localization in Master Page C# 3.0
I am currently trying to understand how to get the name of the (xaml) page I am currently into, with my Xamarin Form app.
How am I supposed to do it? I tried a variety of cases, even looking around the Internet, but nothing actually worked for me so far :/
This is just C# reflection - the name of the XAML page should match the name of it's class
var name = this.GetType ().Name;
You would (commonly) use wither a one page app (master/detail, tabs page) or a set of navigable pages, managed by a NavigationPage.
App.Current.MainPage would (normally) contain the first page shown and if it has .Navigation that would be the NavigationPage that can give you the most recently shown page - last one in the stack. If it doesn't you could assume your app being one page app.
All of the above is just "common" and not necessarily true in all cases, but gives you a starting point where to look for your current page.
var actionPage = App.Current.MainPage;
if (actionPage.Navigation != null)
actionPage = actionPage.Navigation.NavigationStack.Last();
actionPage.DisplayActionSheet(...)
I know that this has been resolved and the scenario is somewhat different but in my specific case I needed to identify the current page in the navigation stack from platform specific code so just in case it may help someone else, this is the code I used:
I used the title property that was set in the constructor of the page.
public static string GetCurrentPage()
{
var page = App.Navigation.NavigationStack.Last();
return page.Title;
}
You can get current open page.
1) if it's MainPage.
-> var MainPage = App.Current.MainPage as PageName;
2) if it's Navigation page.
you can get count pages.
App.Current.MainPage.Navigation.ModalStack.Count
var page= App.Current.MainPage.Navigation.ModalStack.LastOrDefault() as
PageName;
App.Current.MainPage.Navigation.NavigationStack.Count
var page1=App.Current.MainPage.Navigation.NavigationStack.LastOrDefault() as
PageName;
Here's how you can get the current page type from anywhere in the app (Xamarin Forms).
This might be a more general answer to your question but I thought it's worth posting.
If you're using:
1.App with Tabs/One Page
var mainPage = App.Current.MainPage as Xamarin.Forms.Shell;
var currentPage = mainPage.CurrentPage;
var type = currentPage.GetType().Name;
Also, URL of the current page:
string location = mainPage.CurrentState.Location.ToString()
2.Navigation Based Apps
As others also mentioned:
var mainPage = App.Current.MainPage;
var currentPage = mainPage.Navigation.NavigationStack.Last();
Hope it helps.
I have a WPF application that I want it to be two languages. I duplicated my Resources.resx and built my two languages like this:
So when I first load my MainApplication I do this:
Properties.Resources.Culture = new CultureInfo("es-ES");
before the
InitializeComponent();
So everything is loaded in the desired language. Now I want to go the obvious step further, and I designed a Select language on my application:
Any idea on how to reload the interface for the different languages at execution time?
EDIT:
I found this link, and seems to work. But I have a problem. When I try to find the Resources x:key it launches an error... It says ResourceReferenceKeyNotFoundException. Go here to check my mistake.
You want to change the culture for the UI thread, this should work:
var culture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
I followed this interesting link.
I am using WIndows 8, and Visual Studio 2012 Metro. I need to add a DropDownList and make the user select languages. When the user selects a particular language i need all the Text values in the label to change to that particular language.
I followed this tutorial
I R-CLick project added `APPGlobal_Resource` folder
I R-Clicked and added `APPGlobal_Resource` and created a Resource file called rss.resx
Then i duplicated this file and named it rss.fr.resx
I gave 2 values `String1` and `Hello` and `Bonjour` as values (in both files)
R-Click `dropdownlist` and `edit item`, and `ADD` and then gave `en-US` as `Name` and `value`as `en-US` (same way i gave `en-fr`)
Then i set the Label property, `Data (Expression)` `Bindable property` to `Text` and `Expression Type` to `Resources`.
I gave the `Class Key` to `Res` and `Resource Key` to `String1`.
and i wrote the following code in Default.aspx.cs
protected override void InitializeCulture()
{
base.InitializeCulture();
String cult = Request["DropDownList1"];
if (cult != null)
{
Culture = cult;
UICulture = cult;
}
}
The drop down displays with en-US and en-fr but when i select it nothing changes. How can i resolve this ?
Not sure, but it can help:
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo( cult );