Internationalization (i18n) with Flutter
What is Internationalization with Flutter?
Flutter is a popular framework for mobile application development and, with internationalization (i18n) support, enables you to develop applications for users in different languages. This helps make your application more accessible by providing content appropriate to the user's language and locale. Implementing i18n is an important step in enhancing the user experience and making your application competitive in the global market.
How to Implement Internationalization in the Application?
Adding Languages
To implement i18n in Flutter, you need to use the "flutter_localizations" package. The first step is to add this package to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutterAfter that, you need to set the localizationsDelegates and supportedLocales properties in your application's MaterialApp widget.
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English
const Locale('tr', ''), // Turkish
],
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Internationalization (i18n) with Flutter')),
body: Center(child: Text('Hello World')),
);
}
}Creating Language Files
You should create language files for all the languages you want to use in your application. These files are usually in JSON format and contain language keys and their corresponding values:
{
"hello": "Hello",
"welcome": "Welcome"
}Conclusion
Applying Internationalization (i18n) with Flutter is an effective way to enhance user experience and present your application to the global market. By following the steps mentioned above, you can provide content in different languages and appeal to a wide user base. As you develop your application, it is easy to increase the number of supported languages.


Yorum Gönder