I have my Iphone set to English (UK) and I am trying to get the UK part in my flutter ap by doing this.
final languageCode = Platform.localeName.split('_')[0]; //prints en
final countryCode = Platform.localeName.split('_')[1]; //prints NL where I want UK
But my country code is alway NL which is my region. How can I get UK?
Its look like you want to access ISO Country Code
You can acces your device language country code with :
//Your device's all locales
final List<Locale> systemLocales = WidgetsBinding.instance!.window.locales;
//Your device's first(current) locale country code.
String? isoCountryCode = systemLocales.first.countryCode;
Following are the 3 ways I know to get locales:
Localizations.localeOf(context);
WidgetsBinding.instance.window.locale;
Platform.localeName;
Make sure to have the supportedLocales correctly setup in your MaterialApp which is only en_US by default, in order to have all available locale.
supportedLocales: [
const Locale('en', 'UK'),
const Locale('fr', 'CA'),
],
You may need to import and add to your pubspec.yaml 'package:flutter_localizations/flutter_localizations.dart';
Related
The current language code can be retrieved via Locale.current.languageCode but what if I need a more human-friendly name like "English" or "Japanese"?
Does the developer must maintain a mapping in the app or there is a native way to get the language name?
You can use localizedString(forLanguageCode:) method on Locale object.
let locale: Locale = .current
locale.localizedString(forLanguageCode: "pl_PL") // "Polish"
along with other useful functions that can help you get localized region or currency names from codes.
locale.localizedString(forRegionCode: "pl") // "Poland"
locale.localizedString(forCurrencyCode: "PLN") // "Polish Zloty"
and to use different locale than the .current you can easily initialize it with one of the available identifiers.
let japanese = Locale(identifier: "ja_JP")
japanese.localizedString(forLanguageCode: "pl_PL") // "ポーランド語"
japanese.localizedString(forRegionCode: "pl") // "ポーランド"
japanese.localizedString(forCurrencyCode: "PLN") // "ポーランド ズウォティ"
I'm trying to print some basic infos from NSLocale, but I'm not able to get a value back from the .scriptCode property.
Currently, the relevant bits are
let localeIdent = NSLocale.autoupdatingCurrent.identifier
let userLocale = NSLocale(localeIdentifier: localeIdent)
let languageScript = userLocale.scriptCode //not sure why this doesn't seem to return anything.
print("Language script code: \(languageScript)")
the print always returns 'nil'.
The locale returns the rest of the set of information for me, region and language and such, so I'm not sure why this wouldn't be stored / returning.
Not all locales have a script code. See the Language and Locale IDs section of the Internationalization and Localization Guide.
Locale identifiers can contain various parts such as the language code, script code, and region code. The script and region codes are optional.
Look at the documentation for Locale scriptCode for an example:
For example, for the locale “zh-Hant-HK”, returns “Hant”.
Simpler locales such as en_US or de_DE don't have a script code.
I have a service which gives the language display name as part of response. I want to retrieve the language code so that I can apply the internationalization for output file.
Expected input : English - United States
Output : en_US
You might use something like that:
Optional<Locale> locale = Arrays.stream(Locale.getAvailableLocales())
.filter(l ->
l.getDisplayLanguage().equals("English") &&
l.getDisplayCountry().equals("United States")
).findAny();
locale.ifPresent(System.out::println);
Given a country, how do I get the countryCode? The following method is not working
NSNumber* code = [phoneUtil getCountryCodeForRegion:country];
It is working, I used it like so:
[phoneUtil getCountryCodeForRegion:#"US"]
and the output was 1.
Note that you have to use the ISO Country Code, 2 Digit: "US", (you can find a list here).
//....
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
directionsService = new google.maps.DirectionsService();
var request = {
origin : new google.maps.LatLng(origin.lat, origin.lng),
destination : new google.maps.LatLng(destination.lat, destination.lng),
travelMode : google.maps.DirectionsTravelMode.DRIVING,
unitSystem : google.maps.DirectionsUnitSystem.METRIC,
region: 'de'
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
//....
As a result I get something like this
Head southwest on 吳江路/吴江路 toward 泰兴路/泰興路
Turn left at 茂名北路
Continue onto 茂名南路
Turn right at 淮海中路
Slight left to stay on 淮海中路
Turn left at 华山路/華山路
The instructions are English on my browser, and French on my French colleagues French Firefox, the street names are Chinese, I thought I requested information in German region: 'de'
Now ok, maybe the Chinese streets are not available in German, but setting region to gb, en, even zh seems to do nothing.
I really would like the text just to be one language, preferably English.
edit I am quite sure the street names are available in English, because when I use the Geocoder results are in English e.g Shimen Road (No.1)
edit2 with http://maps.google.com/maps/api/js?sensor=false&language=cs I am able to force the instructions into a language, but still the street names are stuck in Chinese. Using the geocoder api i can receive chinese street names that are Chinese translated into English/German/French (with fallback to english when german/french translations are missing) so why the directions street names is stuck on Chinese does not make sense. It could be just a flaw/deliberate on google's side, but I kind of doubt it.
Is there a reason
DirectionRequest doesn't have a parameter to specify language. The language is distinguished according to the language used for the map. The language is either specified as an optional language parameter in the <script> tag e.g.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=cs">
or if the parameter is not present the browser's preferred language is used.
If you want to use different language for the direction results and the map, you can use Google Directions API:
http://code.google.com/apis/maps/documentation/directions/
The result is JSON text. To use it easily, it should be sufficient just to convert it to an object.
The region parameter (both in the maps' DirectionRequest and Directions API) doesn't change the language, it serves other purpose. It affects results to be biased towards some region (e.g. the default result for 'Toledo' is the city in Ohio USA, if you want the one in Spain, use region=es to bias the results).