I want to use localization in my project so I'm using ".resx" files.
I have two files "StringRes.resx" and "StringRes.fr.resx". As you can guess, I want that all the messages of my app change according to the CultureInfo of the user.
But when I do this :
public MainWindow()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
InitializeComponent();
}
It doesn't change anything when I do :
Console.WriteLine(StringRes.FirstName);
Indeed, it's always the string in StringRes which is displayed and not the string from StringRes.fr
The both are in the same namespace.
Thank you for any help.
I have created my own test projected an I used "fr-FR" as a culture tag. The test project can be found on my skydrive.
Works perfect.
In short:
I set the culture in the Properties of the project to be en-US as that is what I would want my Resource.resx to be.
Then I created Resource.resx, Resource.fr-FR.resx and Resource.nl-NL.resx.
I filled them with the TestData string. And the created a window with 3 buttons with events hooked to them. Next to the buttons I created a textblock, the textblock is binded to a Text string field.
In the button events I change the culture and the Text field. And you can then see the change happen.
You first need to create your Resource.resx then in the same folder you need to create Resource.fr-FR.resx. The Resource.resx will get a codebehind file, all the cultured resource files should not. If your Resource.fr-FR.resx does have a culture file you did something wrong and it is best to delete that resources file and recreate it so it loses it's codebehind.
Related
How can I extend the .text and .caption property of ALL controls. I want to create a multilanguage App, which load the text from a custom file.
I want to do this:
button1.Text.LoadLocalizedText()
label1.Caption.LoadLocalizedText()
Now, I need the following text result, to get the correct text.
I need "class.button1.Text" as string and for the label "class.label1.Caption" as string.
After I load the custom text, I must set the text dynamically to the "source" property like text or caption.
I hope, someone could help me.
Thanks.
If you want to write a method you can call on all controls, write a extension method for control (see #Adriani6's answer)
public static void LoadLocalizedText(this Control ctrl) {...}
If you want to load a resource localized by your culture, you should use your resource files and create files for each culture.
Create 2 resource files like Resource1.resx and Resource1.de.resx and save your corresponding values to the resource files.
Then, you can load your resources like
[ProjectNamespace.]Resources.Resource1.ResourceManager.GetString("myResource");
or, alternatively with an overload to specify the culture yourself
Resources.Resource1.ResourceManager.GetString("myResource", System.Globalization.CultureInfo.GetCultureInfo("de"));
See Microsoft Docs
If this is WinForms, you can write an extension method for the Control class.
public static class LocalizationExtension
{
public static void LoadLocalizedText(this Control ctrl)
{
ctrl.Text = "My loaded string";
}
}
This way you can target all controls that inherit from Control such as Button and Label.
You just call it using Button.LoadLocalizedText() and Label.LocdLocalizedText() etc..
I have several resource files built for a multilingual MVC 5 site I am working on.
I am using a CMS and the user can select the button text on certain content segments, like "Download" or "View More".
Rendering a value from my resource file using #Resources.Resource.ViewMore in my view works fine, but since the content is dynamic depending on what button text the user selects in the CMS, I need to use something like #Resources.Resource.#Model.getButtonText() but obviously that will not work.
Any suggestions on how to handle this?
You can use ResourceManager class for this
#{
ResourceManager rm = new ResourceManager(typeof(Resources.Resource));
#rm.GetString(Model.getButtonText());
}
I have following:
Code in Window's XAML
<Grid>
<Label Content="{x:Static properties:Resources.Caption}"/>
</Grid>
I have Resources.resx, Resources.cs.resx. In run time text in label is changed according to language on client's machine. I would like to see different localization in Visual Studio during design time.
During my investigation I even changed getter for Caption property in Resources.resx to following:
public static string Caption {
get {
return ResourceManager.GetString("Caption", new CultureInfo("cs-CZ"));
}
}
This of course hard-code localization in runtime, but still has no effect in design time. In design time I am still getting original Resources.resx value (I suppose it is result of fallback strategy). It seems that during design time localized resources cannot be accessed and therefore values from default one is used.
I would like to avoid using localization frameworks as WPFLocalizationExtension because I believe there should be some "native" way in Visual Studio.
Is there something I am missing that I am unable to get localized versions of strings in design time?
I'm localizing the app I've built, and I have a problem here.
I added all the strings I use in my xaml in the AppResources.resx file, and I have replaced all the hard-coded strings in the xaml with a binding to the right strings in the .resx file, like this:
Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource LocalizedStrings}}"
The problem is that I need to localize a couple of App Bar buttons and a few Messabe Box, so I need a way to get those strings via C#.
I read a couple of tutorials and I came up with this, as for the localized App Bar button:
ApplicationBar = new ApplicationBar();
ResourceManager rm = new ResourceManager("AppResources", typeof(CreditiInfo).Assembly);
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/back.png", UriKind.Relative));
appBarButton.Text = rm.GetString("BackAppBar");
ApplicationBar.Buttons.Add(appBarButton);
I get a System.Resources.MissingManifestResourceException when I try to assign to the AppBar button Text the String I retrieve from the GetString method.
What am I missing?
Also, I read that when you check a new language in the WMAppManifest.xml file, Visual Studio should automatically copy the actual AppResources.resx file and rename the copy with the new current culture selected, but when I try to add a new language it doesn't work.
Do I have to copy and rename the file on my own? Will it recognize the new file when running on a phone that is set on that language?
Thank you for your help!
Sergio
Simply use the auto generated AppResources class
string resourceValue = AppResources.NameOfYourLocalizedResource;
I'm creating a WP7 application using C#, and I require to pass data from one page to the other.
I found solutions on SO, but I'm still running into problems.
On 'Page 1', I wish to display a list, that can be populated by the user, using input from 'Page 2'.
I used the following statement in 'Page 2' while navigating back to 'Page 1': NavigationService.Navigate(new Uri("/MainPage.xaml?text="+WhoBox.Text, UriKind.Relative));
WhoBox is a Text Box.
On 'Page 1', I have the following:
protected override void OnNavigateTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("text"))
ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"];
}
Now, this works, but in a limited fashion. If I try adding something from 'Page 2' for a second time, it replaces what is present in ListBlock (which is a Text Block) with the newly added text instead of appending it.
Shouldn't ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"]; cause the new text to be appended, rather than to entirely replace the older text?
EDIT: I may have found the solution. For whatever reason, no changes in the XAML or .cs file are reflected when I run the program using F5. Am I doing something wrong? For example, even if I delete a button, it still appears when I Debug (F5) the program. Is there some setting I need to change? Or am I supposed to use some other command? I'm relatively new to Visual Studio, so please excuse me.
The problem is that the moment you again leave your page 1 it is basically disposed of. Meaning any text that was set in the Listbox is also removed. You will , in other words, need to save the state of that page before leaving it.
There several possibilites here:
Use AppSettings (see Windows phone 7 config / appSettings? )
Write the state to a local database
Do a quick'n dirty fix by saving the Text in the App.xaml.cs which all pages can work with: First you need to create the application-wide variable (and initialize if needed) inside the app.xaml.cs file. For example:
public partial class App : Application
{ public string myText;
From now on you can reach any App-variable through the Application.Current object. So if you need to access bigVar from some page in you application (e.g. MainPage) you simply type:
string Text = (Application.Current as App).myText;
Consider using sessions and datatable: Storing and retrieving datatable from session