Flutter internation­alized 설정 하는 법 Flutter_localizations

2024-03-20


사진: Unsplash 의 Travis Leery


1. 방법

 

pubspec.yml의 아래와 같이 의존관계 추가

 

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: any

 


main.dart 에 아래와 같이 localizations 설정 정보를 작성

 

return const MaterialApp(
  title: 'Localizations Sample App',
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: [
    Locale('en'), // English
    Locale('es'), // Spanish
  ],
  home: MyHomePage(),
);

이후 아래와 같이 현재 위젯 기준으로  현재의 언어 정보 확인이 가능해진다. ex) en / ja / ko

 

    final locale = Localizations.localeOf(context);
    final languageCode = locale.languageCode;
    print(languageCode)

2. 참고

 

https://flutter-ko.dev/development/accessibility-and-localization/internationalization

 

Internationalizing Flutter apps

How to internationalize your Flutter app.

docs.flutter.dev

https://api.flutter.dev/flutter/widgets/Localizations/localeOf.html\

 

localeOf method - Localizations class - widgets library - Dart API

Locale localeOf(BuildContext context ) The locale of the Localizations widget for the widget tree that corresponds to BuildContext context. If no Localizations widget is in scope then the Localizations.localeOf method will throw an exception. Implementatio

api.flutter.dev


메인 이미지 출처 : 사진: UnsplashTravis Leery