Recently, I decided to add 4 languages to my application.
I read about how to do it and I succeed.
But there are two problems/questions I would like to ask.
First question: Is there a better way to change the text of each control instead
private System.Resources.ResourceManager rm;
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
rm = new System.Resources.ResourceManager(typeof(MainForm));
and then for each control to write this line:
aboutToolStripMenuItem.Text = rm.GetString("aboutToolStripMenuItem.Text");
addTaskToolStripMenuItem.Text = rm.GetString("addTaskToolStripMenuItem.Text");
addTaskToolStripMenuItem1.Text = rm.GetString("addTaskToolStripMenuItem1.Text");
...
Second Question: lets say the text of label1 is "test" and in other language its "testest" then the size of the label will change, which is ok. but If I got label2 that his location is near label1, label1 might be on the top of label2. How can I move the label1 compare with label2 so no matter how long the text in label1 will be, label2's location will be relative to label1. I dont want to use calculations in the program, I want to know if there's other way like property in one of the controls.
EDIT:
after long thinking, I decided to use XML for my multilanguage. this way I can let the people translate it and upload it for me plus I can use it on runtime instead or reload the programs.
About the relative pos of controls I will use FlowLayoutPanel or TableLayoutPanel I will check further which is better.
Satellite assemblies is what you are looking for.
Q1: In VS, set your form's Localizable property to true. Then select the language property accordingly and type your translations in the designer. That way, you simply need to set the thread culture at start-up and .NET will load the correct language for you. No need to add extra code.
Q2: Again, after you selected the language in the designer, just move the controls around: Their new location/size is part of the translation and will be handled automatically by .NET.
It's not a very good idea change the GUI to a different culture while it's running, it's better to say something like you need to restart the application to see changes.
Although if you need to do it, you need to reload all the resources from the new culture (more or less the same than the InitializeComponen does), not only the text, because the location, size and so on, may be changed too. Also you need to change the thread culture in order to the errors, message and the new controls have the correct culture too (to show them in the correct language too).
You can set your application culture with:
CultureInfo appCulture = CultureInfo.CreateSpecificCulture("fr");
Thread.CurrentThread.CurrentCulture = appCulture;
Thread.CurrentThread.CurrentUICulture = appCulture;
You need an specific culture to use it on formating and parsing.
In answer to your first question:
If you really want to follow that scheme maybe using Reflection or automatic code generation is an alternative for easier management. I usually write my own GetString method that takes a default english string as an argument (used if no resource can be loaded dynamically for the current language). But I am not sure if it is the best solution either...
In answer to your second question:
In Winforms use a TableLayoutPanel or FlowLayoutPanel or another layout component to relatively position the controls. It is possible to specify if the Label fits to its content (AutoSize) for example or if it shall Dock and if yes with what Alignment. There is nearly no use case that would require a tedious self management or computation.
Link: http://msdn.microsoft.com/en-us/library/z9w7ek2f(v=VS.100).aspx
Related
I'm getting into globalization and localization. I'm aware that I can set the localizable property of my form to true and that I'm able to edit the form for that specific culture. That's great. My question is am I able to, at the designer, load my resx values into their assigned winforms? My goal is to edit the size of my winform assets around the translated languages. This is, obviously, easier if I can see the translations in the designer. Attached is the code that change the text of my buttons at runtime, but my goal is have this take place during the design phase.
buttonTelescopeUp.Text = Resource.TeleUp;
buttonTelescopeDown.Text = Resource.TeleDown;
buttonTelescopeLeft.Text = Resource.TeleLeft;
buttonTelescopeRight.Text = Resource.TeleRight;
Form at runtime
Formin design
Having issues using a custom font in a WPF app.
To use the font, I set FontFamily="pack://application:,,,/Resources/#iconfont" on my label. However, whenever I go to edit any of the properties of the label in xaml, Visual Studio is automatically changing the font family pathway to FontFamily="iconfont", and the custom font no longer works. Any way to fix this? A setting I need to change? It's infuriating!
Edit: As long as I'm switching between properties of labels, FontFamily stays the same. But if I go to edit the properties of another type of control, and then come back to the label, FontFamily resets to "iconfont".
For starters let's make life easier and get your clutter cleaner by taking that long pack string and making it a defined resource in your resource dict like app.xaml, window.resources, wherever we can hit it from wherever we need.
So instead of putting FontFamily="pack://application:,,,/Resources/#iconfont" on every single instance it's used (which can be a performance hit by the way) we do;
<FontFamily x:Key="IconFont">
pack://application:,,,/Resources/#iconfont
</FontFamily>
We take that and plop it in your resource dictionary or wherever you want so it's referenced and loaded once from one place and made available to wherever else. Then at your instance you just do;
<Label FontFamily="{StaticResource IconFont}"/>
Now your editor should not only stop screwing with you but you have a cleaner way of maintaining that sucker. I mean imagine if you had to change your path string, or you decide to use a different font pack, would you rather do it on every instance, or in one spot that inherits to everywhere it's used?
Hope this helps, cheers.
I've recently been tasked with localizing a small piece of software that my company created. I've been able to create the resx file for the particular culture, and things are well and good. Thing is, that there are roughly 50 or so labels/buttons/etc, that need to be changed. My question is, do I need to write in the code to set the text, or is there an easier way? For instance,
lblText.text = Resources.labelText
for every single button/label. Or is there a property in the button/label that I'm missing that would set it. Thanks.
You can change the text directly with the form designer of visual studio. Just set the form's property Localizable to true and change the Language property to the one you want to translate. Then, every change you make will only be reflected in the language you selected
If you want more help, you can take a look at this walkthrough
We are developing a localized English/Arabic application, and I need to set the System.Threading.Thread.CurrentThread.CurrentUiCulture to either of both cultures during design time to a different value than the value set at run time.
How can I accomplish this?
Thanks in advance.
Set the xml:lang attribute.
This attribute describes the culture of a specific element and its child elements. The value of this property changes the behavior of several features in WPF. For example, it changes the behavior of hyphenation, spell checking, number substitution, complex script shaping, and font fallback. See Globalization for WPF for more information about setting the xml:lang Handling in XAML.
http://msdn.microsoft.com/en-us/library/ms788718.aspx
Check this out http://geekswithblogs.net/lbugnion/archive/2009/09/05/detecting-design-time-mode-in-wpf-and-silverlight.aspx, you can then set the Language accordingly with something like
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name)));
The WPF Localization Extension allows you to set design time language (and it has other features, like allowing you to change languages at runtime through WPF with no code-behind etc.).
Check it out here: http://wpflocalizeextension.codeplex.com/
I've just started developing in Silverlight, and I have a calendar control which shows details for each day.
The text within the calendar is held within lots of textblocks, for some browsers the text size might be too big, so I want to have a slider control on the 'usercontrol' which allows the user to adjust the font size.
I'm building the calendar through c# code, so my question is.. what is the best way to 'wire' this up. I'm guessing it would be one of these options.
Add an event to the slider control for ValueChanged, then iterate through all TextBlocks setting the fontsize to the new size. This seems long-winded.
Maybe using a 'Style', which is attached to each TextBlock, then just changing the FontSize of the 'Style'.. ?? maybe ?
Using 'binding' & 'Dependancy Property'. I've looked into this, and it seems to be the way to do it, but I can't find an example where you're passing a value from one control to multiple other ones. Maybe I'm missing something.
I can do option 1 quite easily, but I want to learn about alternative methods of doing this.
Thanks
Rich.
A way to handle this is to not use fixed sizes but to size everything to its content. that way the control will get bigger when a larger font size is selected. Just like the TextBlock.
I've actually got this working now using option 3 which I'm happy with, it was very simple, but there seemed to be little documentation on it (maybe I was searching for the wrong thing).
It was as simple as adding a few lines...
Binding bind = new Binding("Value");
bind.Source = FontSlider;
The for every control that I want to apply the FontSlider's 'Value' to.. I do this...
MyTextBlock.SetBinding(TextBlock.FontSizeProperty,bind);
Simples.
Cheers
Rich.