Set a language
On Android, you can use the createConfigurationContext) method to load resources in the correct locale. This is especially important for users of screen readers.
kotlin
val locales = LocaleList.forLanguageTags("nl-NL")
val configuration = baseContext.resources.configuration
configuration.setLocales(locales)
val context = createConfigurationContext(configuration)
element.text = context.resources.getString(R.string.appt) In Jetpack Compose, you can use the createConfigurationContext) method to load resources in the correct locale. This is especially important for users of screen readers.
kotlin
val context = LocalContext.current
val localizedContext = remember {
val locales = LocaleList.forLanguageTags("nl-NL")
val configuration = context.resources.configuration
configuration.setLocales(locales)
context.createConfigurationContext(configuration)
}
val localizedString = localizedContext.resources.getString(R.string.appt)
Text(text = localizedString) On iOS, you can set the locale of an app via the CFBundleDevelopmentRegion property. We suggest using Base internationalization to separate user-facing strings from .storyboard and .xib files. You can load a specific Bundle to load assets in the desired language.
For more information, see Adding Support for Languages and Regions.
swift
extension String {
func localized(_ language: String) -> String {
guard let path = Bundle.main.path(forResource: language, ofType: "lproj"),
let bundle = Bundle(path: path) else {
return localized(Bundle.main)
}
return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
}
} In SwiftUI, you can set the app's default locale using the CFBundleDevelopmentRegion property. You can also access and modify the locale value dynamically within the app via SwiftUI's locale environment property.
To retrieve a localized string from a .strings file, use the LocalizedStringKey struct. SwiftUI simplifies localization by automatically handling string literals for specific types such as Text, Toggle, Picker and more. When you initialize a view with a string literal, SwiftUI converts the string into a LocalizedStringKey and looks up its localized version based on the current locale settings.
For more detailed information, refer to Adding Support for Languages and Regions on Apple's Developer site.
swift
// Get current locale
@Environment(\.locale) var locale: Locale
// Set the locale dynamically
WindowGroup {
ContentView()
.environment(\.locale, .init(identifier: "en"))
}
// Looks up localized string in .strings file
let welcomeText = LocalizedStringKey("appt_welcome_text")
// Initialises a view directly with LocalizedStringKey value
Text("appt_welcome_text") With Flutter, you can use the various methods to load resources in your desired locale. This is especially important for users of screen readers. The example below shows a simple implementation to handle this without using any dependencies.
dart
/// This is the main widget of the application
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
static _MyAppState of(BuildContext context) => context.findAncestorStateOfType<_MyAppState>();
}
class _MyAppState extends State<MyApp> {
/// Private variable holding the current localization
Locale _locale;
/// Calling this method will initialize a re-draw of the entire widget tree.
void setLocale(Locale value) {
setState(() {
_locale = value;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: _locale,
home: LanguageButtons(),
);
}
}
class LanguageButtons extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
TextButton(
child: Text("Set locale to Dutch"),
onPressed: () => MyApp.of(context).setLocale(Locale.fromSubtags(languageCode: 'nl')),
),
TextButton(
child: Text("Set locale to English"),
onPressed: () => MyApp.of(context).setLocale(Locale.fromSubtags(languageCode: 'en')),
),
],
);
}
} In React Native, there is no standard for loading resources in your desired language. Various packages are available to help you achieve this. You can use expo-localization in combination with i18n-js to get the device locale and set translations for your app.
jsx
import * as Localization from 'expo-localization';
import i18n from 'i18n-js';
// Set the key-value pairs for the different languages you want to support.
i18n.translations = {
en: { welcome: 'Appt accessibility' },
nl: { welcome: 'Appt toegankelijkheid' },
};
// Set the locale once at the beginning of your app.
i18n.locale = Localization.locale; In MAUI, there are several ways to handle localization. You can achieve this using the native approach via app resources, but it is recommended to use the .NET approach via RESX files. Make sure you follow all the instructions in the documentation to ensure it works correctly. After adding the strings into your RESX files, you can simply use them as follows:
Usage (XAML)
xml
<Label
Text="{Static resources:Strings.HelloMessage}" /> csharp
var label = new Label();
label.Text = Strings.HelloMessage; With Xamarin, you can use CultureInfo to set a language. For more information, see String and Image localization in Xamarin Forms.
csharp
using System.Globalization;
using System.Threading;
CultureInfo ci = CultureInfo.GetCultureInfo(DependencyService.Get<IGeneralPreferences>().Language);
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
AppResources.Culture = ci;
CultureInfo.CurrentUICulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;