I'm trying to figure out localization on Xamarin.iOS. I am new to localization in general, but the first language we want to do is Icelandic. If you look on the settings for the iOS device itself Icelandic is not an option. So this is a two part question.
How can I set up localization within my app? Do I just localize in the same manner as other .net apps..or is there something specific to iOS/Xamarin that I need to do.
Once I implement this, how do I get it to choose Icelandic as the language since iOS does not have it as an available language?
Documentation seems to be sparse on this topic.
How can I set up localization within my app?
iOS has its own way to handle localizations. For each language you need to create a folder in your project named language.lproj where language is ISO 639-1 or ISO 639-2 language code. Two character ISO 639-1 codes are preferred (e.g. en, de, fr, it, ...). You can find a table with ISO 639-1 and ISO 639-2 codes here.
In your newly created folder you need to create a file named Localizable.strings and here you can add your strings which you want to localize:
"stringToLocalize" = "This is the translation";
And to assign a localized string for example to a UILabel:
UILabel label = new UILabel();
...
label.Text = NSBundle.MainBundle.LocalizedString("stringToLocalize", null);
You can even create an extension method (credit to anotherlab):
public static class LocalizationExtensions
{
public static string t(this string translate)
{
return NSBundle.MainBundle.LocalizedString(translate, "", "");
}
}
How do I get it to choose Icelandic as the language since iOS does not have it as an available language?
I don't think there is a way to choose Icelandic as a language in iOS. One of our apps uses German, Italian and French localizations. Since we don't need English we created en.lproj folder with German localizations in it. This way even if the language of the device is set to English, German texts will appear. Maybe you can do something like this too in your app.
Related
I would like to localize the VirtualKey for Control.
Currently there is code like this:
using Windows.System;
var message = "Press " + VirtualKey.Control.ToString() + " + D for deletion!";
Is there an API which can be used to have the following?
Press Control + D for deletion! (on an English system)
Press Steuerung + D for deletion! (on a German system)
As VirtualKey is an enumeration, you cannot translate a member of an enumeration directly, but you can use it as the key for resource files, building up a localization system:
I followed this tutorial to build up a simple localization system on a Windows 8.1 Application; these are the steps I followed (summed-up)
Create a folder called "Strings"
Inside the folder, create a folder for the default language you want to support and name it accordigly (see a full list of possible codes here)
Create a Resources.resw file in that folder
Add the strings you want to add - If you want to support the use of VirtualKey.xxx.ToString(), I strongly suggest you to use the same identifiers of the enumeration; for instance: if you want to translate the control character, call the new resource "Control" - (this is what I did):
Copy and paste that folder for a number of times equal to the number of languages you want to support (I did it twice):
Edit the Resource.resx file accordingly to the language you're translating to.
In code, refer to each translated string with
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
loader.GetString(VirtualKey.xxx.ToString());
That's what I did on a TextBlock in MainPage.xaml:
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
this.txbCtrl.Text = String.Format("Press {0}-Z to undo", loader.GetString(VirtualKey.Control.ToString()));
And that's the result for the language it-IT:
To test it for other languages, I followed the tutorial, even if I don't like it:
Open the Control Panel and go to Clock, Language, and Region > Language
Note that the language that was displayed when the app was ran is the top language listed that is, on my system, Italian.
To test the app with another language, select the language in the list and click Move up until it is at the top. Then run the app.
NB: If you do not have all three of these languages on your machine, add the missing ones by clicking Add a language and adding them to the list.
In my case I have italian listed for first and English (UK) as the second one, but if I swap them:
and run again the application, this is the result
and if I add de-DE:
with these resource files:
en-GB:
and it-IT:
and de-DE:
Notice that I've called the English Control key "Control" and the Italian one "Ctrl"; it works perfectly
I Hope this helped.
If you have more questions, just ask!
LuxGiammi
EDIT: this is a solution, even though I recognize this is not a good one (anyway, it's the best I could think of). Anyway, as stated here for WinForm applications, it is not necessary to do it because evrybody would understand you if you use the defualt names for the keys (i.e., the one in the enumeration, just like you're doing now).
EDIT2: this solution, however, sets everything up for a future "full" localization for your application. This way half of the effort is made at the beginning of the developemnt process.
Hi I want to create a small program to change windows default language while spell check the user input in any text area in windows. I know too many programs like MS Word or Word Pad, specially these online text editors have spell check so no need to write it from 0 to 100 right?
My primary language for this project is English but I don't know can I use spell check for other languages like French, Arabic, Turkish etc.
I have this for changing language :
public struct LanguageHelper
{
public static void SetLanguage2English()
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en-US"));
}
public static void SetLanguage2French()
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("Fr-FR"));
}
}
I don't any new lead to use it for this project. Thanks in Advance for every hint or new lead that can help me to complete my project.
Is there a solution for xamarin-ios to get the current culture in the format like (en_US, de_CH, ..)? I read so many articles in the web and here on stackoverflow. But I can't find a nice solution for this.
Following code snippet returns not this format everytime. Sometimes I get "gsw_DE" and so on (ISO 639-2 instead of 639-1)
NSLocale.CurrentLocale.Identifier;
My current solution is to do it by myself:
var languagecode = NSLocale.PreferredLanguages[0];
var result = String.Format("{0}_{1}", languagecode.Substring(0, 2), NSLocale.CurrentLocale.CountryCode);
Above workaround is from: Get iOS current language (including country code)
I need to make sure that I get the value in this format.
Is there another way? Thanks
We are still in the C# world, aren't we?
You can use CultureInfo.CurrentUICulture.Name and it'll give you "en-US" for "default" English and "en-GB" for English UK. Language settings are in General - Language & Region - iPhone Language.
I'm having some problems in Windows 8 Metro apps (XAML & C#) regarding the user's regional settings. It seems that the apps won't respect user's regional settings, so even if your Windows 8 is set to display dates and times in Finnish format, the apps will still display them using US-formatting. But this is such a big problem that there must be something I'm missing?
To test this I started by creating a WPF-application. The application just prints out the CurrentCulture and the formatted DateTime.Now:
private void Culture_Loaded_1(object sender, RoutedEventArgs e)
{
this.Culture.Text = System.Globalization.CultureInfo.CurrentCulture.DisplayName;
}
private void Date_Loaded_1(object sender, RoutedEventArgs e)
{
this.Date.Text = DateTime.Now.ToString();
}
Here's my default regional settings:
When run, the app displayed the date in Finnish format:
Then I changed the regional settings to US:
And when the app was run again, the culture and formatting changed:
This is as I expected everything to work and this is also how I expected WinRT apps to work.
So as a next step, I created a WinRT (XAML & C#) app with the same code and reverted the regional settings back to Finnish. The problem:
Even when I've defined through regional settings that the formatting should be "Finnish", the WinRT app displays the datetime with US-formatting. I then modified the app's project file and made fi-FI the default language:
This change also modified the app's culture:
Strange. I changed the Default Language back to its default value and the formatting was restored to US. I then created folders "Strings - fi-FI" inside the project and added an empty "Resources.resw" to the project. This empty file seems to be enough, as I was now getting the Finnish formatting:
As soon as I remove the empty resource file, the formattings reverts back to US:
Very strange.
This leads to few questions, but the main one I think is: Is it intentional that the WinRT-apps don't follow the user's regional settings like the WPF apps do?
It's been a while, but the question is not fully answered, so let me share my little research. Depechie is mostly right, but he provided only a link and wasn't really sure.
Yes, this unexpected change is intentional. We shouldn't use CultureInfo anymore as it contains legacy codes and Microsoft want us to use Windows.Globalization APIs instead.
To obtain current region we can use:
GeographicRegion userRegion = new GeographicRegion();
string regionCode = userRegion.CodeTwoLetter;
But as I noticed it contains only region information, there's no language code. To obtain language we can use:
string langRegionCode = Windows.Globalization.Language.CurrentInputMethodLanguageTag; // depends on keyboard settings
List<string> langs = Windows.System.UserProfile.GlobalizationPreferences.Languages; // all user languages, like in languages control panel
List<string> applicationlangs = Windows.Globalization.ApplicationLanguages.Languages; // application languages (user languages resolved against languages declared as supported by application)
They return BCP47 language tags in format language-REGION like "en-US" if language has dialects or just language like "pl" if the language doesn't have major dialects.
We can also set one primary language which will override all the rest:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US";
(This is a persisted setting and is supposed to be used at user request)
There is also new API for date, time and numbers:
Windows.Globalization.DateTimeFormatting.DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "en-US" }, "US", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour);
string longDate = dtf.Format(DateTime.Now);
Windows.Globalization.NumberFormatting.DecimalFormatter deciamlFormatter = new DecimalFormatter(new string[] { "PL" }, "PL");
double d1 = (double)deciamlFormatter.ParseDouble("2,5"); // ParseDouble returns double?, not double
There's really a lot more in Windows.Globalization APIs, but I think that this gives us the general idea. For further reading:
date & time formatting sample:
http://code.msdn.microsoft.com/windowsapps/Date-and-time-formatting-2361f348/sourcecode?fileId=52070&pathId=561085805
number formatting & parsing sample:
http://code.msdn.microsoft.com/windowsapps/Number-formatting-and-bb10ba3d/sourcecode?fileId=52249&pathId=1462911094
there's also a nice article titled "How to use patterns to format dates and times" on msdn, but I can add only 2 links
You can also find some topics about the issue on windows 8 dev center forum with some Microsoft employee answers, but they mainly send you to the documentation.
It is intentional. Microsoft is moving away from forcing applications to be in the language of the OS. Instead, each application uses information declared by the application (manifest languages, observable at Windows.Globalization.ApplicationLanguages.ManifestLanguages) and declared by the user (user languages, observable at Windows.System.UserProfile.GlobalizationPreferences.Languages) to determine how to display resources and globalized dates and times. This set of languages is called the application languages (observable at Windows.Globalization.ApplicationLanguages.Languages). The behavior you are seeing is because you are fiddling with the user languages and the manifest languages and you will get different application languages.
Could it be we now need to query other classes? Like the example given here: http://code.msdn.microsoft.com/windowsapps/Globalization-preferences-6654eb36/sourcecode?fileId=52104&pathId=236099476
This post still seems to be relevant even though it was asked two years ago.
I just came across it as I was looking for an answer to about the same thing.
I also wanted to display dates in the regional format in my WP8.1 WinRT app.
The information posted here helps, but it was a bit hard to piece it together.
This is what I came up with and it seems to work for me as the answer I needed:
using Windows.Globalization;
using Windows.Globalization.DateTimeFormatting;
private string FormatDate(int year, int month, int day)
{
GeographicRegion userRegion = new GeographicRegion();
string regionCode = userRegion.CodeTwoLetter;
var formatter = new DateTimeFormatter("year month day", new[] { regionCode });
DateTime dateToFormat = new DateTime(year, month, day);
var formattedDate = formatter.Format(dateToFormat);
return formattedDate;
}
I need to make a form that have to suport two languages (for now).
the two languages dont have the same look and also half of the form is not a like but it still have some similiarity beetwen them.
what is the best way to deal with this problem in a case of two different languages and above?
by the way, the program language that I use is C#.
10x
First, configure your project default language, you do this in the Project Properties, in the Assembly Information dialog, there's a "Neutral Language" setting at the bottom.
Set this to be your "default" language, the "main" language if you wish.
Then, make sure the form as it is now, is in that language.
To start translating and changing the form to comply with a different language, first set the "Localizable" property of the form to true, and then change the Language property to your second (or third, fourth, etc.) language.
Once you have changed that, you can start making changes. Make sure you don't delete items on the form, instead just set them invisible. Deletion is done for all languages, but invisible will thus only be set for the current language.
Keep switching back and forth between the languages to make adjustments.
To test your program in a specific language, execute this at the start of your Main method:
Thread.CurrentThread.CurrentCulture = new CultureInfo("code of that other language");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("code of that other language");
For instance, to test it with "Norwegian, Bokmål" language, which is my main language, the code you would use would be "nb-NO". To find the code you need to use, once you've changed the language of your form to the language you want to localize for, and saved, a new file will be added to the solution explorer with the right name.
For instance, for Form1, the following files will be present:
Form1.cs
Form1.designer.cs
Form1.nb-NO.resx <-- here's the localized content
Form1.resx
Now, having done this, there's plenty of other things you need to be aware of when making a localized application, I suggest you go read other questions on SO and on the web with more information, like these:
Best practice to make a multi language application in C#/WinForms?
Parsing DateTime on Localized systems
Pluralising and Localizing strings in C#
How do I best localize an entire app to many different languages?
I think a single global resource file for each language is better than having a resource file for each form for each language.
I recently did globalization of a winforms app with the help of the following link "Globalization of Windows Applications in 20 Minutes". Here are the steps for simplicity :
Create a text file say "data.en-US.txt" in the solution (<filename>.<culture string>.txt) in the format below :
Hello=Hello
Welcome=Welcome
This file "data.en-US.txt" is a resource text file in the English language.
Create a similar file in your desired language say German, so its filename will be data.de-DE.txt
Hello=Halo
Welcome=Willikomen
The first column are the labels which will be used as keys for referring the text.
Create a "data-en-US.resource" file out of the text files by using the command "resgen.exe data.en-US.txt",
Do this for all language files. Now in your solution you have 2 (or more, depending on the languages you want to support) data.<culture string>.resource files
Click all the .resource files and set "Build Action" property to "Content".
Click all the .resource files and set "Copy to Output Directory" property to "Copy Always / Copy If new".
Get the localized string using the code below in any of the forms :
string label = "Hello";
string cultureString = "de-DE" // [or "en-US"]
string strResourcesPath = Application.StartupPath;
CultureInfo ci = new CultureInfo(cultureString); ['cultureString' represents your desired language]
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("data", strResourcesPath , null); ["data" is the first part of the filename]
var answer = rm.GetString(label, ci); // here ans="Halo" as we selected de-DE