change textbox input language on change of app language - c#

I am working on a metro app which provides a change language option in app. I want that on change of language textbox input language also get change. and it should not depends on system language.

I use these codes:
First of all you must find the name of the culture language you want.
method "GetInutLanguageByName" will return the language that you requested
Then you will check whether you installed the requested language or not, if yes then return the requested language.
Then change the input language very easy...
private static InputLanguage GetInutLanguageByName(string layOut)
{
foreach (InputLanguage lng in InputLanguage.InstalledInputLanguages)
{
if (lng.Culture.DisplayName == layOut)
{
return lng;
}
}
return null;
}
private void SetKeyboardLayout(InputLanguage Layout)
{
InputLanguage.CurrentInputLanguage = Layout;
}
private void FirstNameTextBox_Enter(object sender, EventArgs e)
{
SetKeyboardLayout(GetInutLanguageByName("Persian"));
}

Firstly, you need to make sure that the language you want is installed in OS, and it is in the list of INSTALLED INPUT LANGUAGES (check the language bar in control panel under Language and Regional Settings).. If its not in language bar, add it..
e.g. you want to change app language to "FRENCH".. you would need to create a new resource file for every language you want to change in app, and then change the current thread's Culture Property..
Are you familiar with resources file (.resx) and Culture Info Class??

Related

Localization with C# Windows Forms, cannot access my .resx File

Hi I created this WinForm Program. It has several Forms.
I looked for a tutorial or someting to help and found this on StackoverFlow.
How to use Localization in C#
It was really helpful but..
I created a ResourceString.de-DE.resx File and added it to the Properties Folder.
Added some Strings so i can test it. And changed the Access Modifier to Public.
Then i wanted to access the Properties Folder to Use the ResourceString.de-De.resx File.
But it doesn't get suggested.
The Code has to look like this in the end :
private void setLanguage()
{
btnSwitchLanguage.Text = Properties.ResourceString.de-DE.btnSwitchLanguage;
}
Am I missing something ?
Any Help is appreciated. :)
Thanks
You don't need to add the Strings.resx file, Your project already has the resource file Resources.resx. Therefore, you should add a new resource file with Resources.de.resx name, if required localization for the "de-DE". So, you can put into this resource file not only strings, but images, icons etc.
There is no need to change the Access Modifier to Public unless you are not going to access this resources from another assembly.
NOTE: When you are working in the Visual Studio the Visual Assist
suggestion will be the same for all languages, starting from
Properties.Resources.
You need to create a default 'ResourceString.resx' file along with al your language specific resx files. Make sure to add the same resources in all resx files. (btnSwitchLanguage, ...).
Well i found my mistake, after reading the Thread again, that i linked in my Post.
There should be a File called Strings.resx (or whatever Name you Choose), which contains the original strings.
And the File which contains another language. (German in my Example).
Should have the same name, except the language comes at the end.
Like this :
Strings.de-DE.resx
After that i just had to change my Code to :
private void btnSwitchLanguage_Click(object sender, EventArgs e)
{
if (Thread.CurrentThread.CurrentUICulture.ToString().Equals("de-DE"))
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
}
else
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
setLanguage();
}
}
private void setLanguage()
{
btnSwitchLanguage.Text = Properties.Strings.btnSwitchLanguage;
}

Update input language of on screen keyboard

I have an Windows Forms application written in C# .NET 5 with a webview2 control which hosts a kiosk based application which will be run full screen on a touchscreen.
The application will use the on screen keyboard for user input. The application is hosted on our own machines, which will only be used for our application so changing the system input language is exactly what we want to achieve.
I need to allow the users to pick their own input language. In 4.7.2 I can achieve this by setting InputLanguage.CurrentInputLanguage, which dynamically updates the on screen keyboard input language, you also see the system input language update in the task bar (which in production will not be visible to users).
In .NET 5, setting the same value does not have the desired effect, the on screen keyboard does not reflect the changed input language, not does the input language update in the taskbar.
I notice the libraries behind the scenes have changed which is clearly why I am having this issue.
I have tried the following without success in a simple dummy app:
private void btnEnglish_Click(object sender, EventArgs e)
{
var language = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-GB"));
if (InputLanguage.InstalledInputLanguages.IndexOf(language) >= 0)
InputLanguage.CurrentInputLanguage = language;
ChangeSystemInputLanguage(language);
}
private static void ChangeSystemInputLanguage(InputLanguage language)
{
Application.OleRequired();
IntPtr handleOld = ActivateKeyboardLayout(new HandleRef(language, language.Handle), 0);
if (handleOld == IntPtr.Zero)
{
throw new ArgumentException("ErrorBadInputLanguage", nameof(language));
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ActivateKeyboardLayout(HandleRef hkl, int uFlags);
Short of downgrading to 4.7.2 or re-writting in WPF (and such using the InputLanguageManager), can anyone make any recommendations how I might achieve the desired effect in .NET 5.0?
For anyone having this issue, the answer was incredibly simple...
before updating the CurrentInputLanguage, set focus on a control on in your form!

Default Resource.resx file not used

I have a WPF application which is localized.
When I set Format to Hindi(India) from ControlPanel -> Region -> Formats, Following lines of code in my WPF application at the beginning of launching of my WPF Application is not reading CultureInfo.CurrentCulture(hi-IN) instead it uses en-US.
Application.Current.MainWindow = new MainWindow();
Application.Current.MainWindow.Show();
Because of this, My WPF Application is not using greeting message from Resources.resx file. Instead, it is use greeting message from in Resources.en.resx
I am getting proper value in CultureInfo.CurrentCulture.
Any idea why above lines of code are not picking proper value?
The ControlPanel->Region->Formats setting doesn't apply to .resx files. It is in ControlPanel->Region->Language that you specify the default language.
What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET?
Alternatively you could specify the default language of your resources in your App class (App.xaml.cs):
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Resources.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;
}
}
Please refer to the following link for more information: https://social.msdn.microsoft.com/Forums/vstudio/en-US/6bfb8d13-3a86-4c10-a632-bb20c99d0535/localization-in-wpf-using-resx-files-for-different-languages?forum=wpf.

Change input language doesn't work

I need to change the keyboard input language and set it to English from C# (I need an .exe for do it), the problem is that the program works in Visual Studio, but when I launch the .exe it doesn't change the keyboard.
I have this in the main method on a winforms app
static void Main(){
InputLanguage englishLayout = GetInputLanguageByName("english");
if (englishLayout!=null) {
InputLanguage.CurrentInputLanguage = englishLayout;
}
else
{
Console.WriteLine("nulo.");
}
This method searches the installed languages
public static InputLanguage GetInputLanguageByName(string inputName){
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages){
Console.WriteLine(lang.LayoutName);
if (lang.Culture.EnglishName.ToLower().StartsWith(inputName))
return lang;
}
return null;
}
Just need that from the application (do not need forms, just change the keyboard language), but when I launch the .exe outside Visual Studio it doesn't work.

Windows Phone 8.1 manually change resource file in code behind

I'm developing application which requires for me to handle multiculture and resources in Windows Phone 8.1.
Currently it loads en language if my phone language is set to english, if I change it to french or whatever it also works. What i would like to do is for a user to be able to change the language in the app while not changing the language in the phone. I would like to have a settings page when user can pick language from a list of possibilities. Save it in some storage settings and after application start to load appropriate resources.
What I can't figure out is how to load specific resources based on some settings in the storage.
Use IsolatedStorage to store the user selected language string.
When the app is loaded you can change the language to that selected by user using the
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride
Property.
Let me give you some details.
Create a class as LacalizedStrings.cs in your project.
And let's put that the resource file name is AppResources
public class LocalizedStrings
{
private static AppResources _localizedResources = new AppResources();
public AppResources LocalizedResources { get { return _localizedResources; } }
}
in your AppResources put one key as ResourceLanguage and value as en-US.
Now this value you can save in IsolatedStorage and the time of
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}
or
private void Application_Activated(object sender, ActivatedEventArgs e)
{
}
you can check this value from IsolatedStorage and use it.
Hope it helps.

Categories