flutter i18n, l10n

오픈소스·2025년 3월 1일
  • i18n (Internationalization): 다국어/다지역 지원을 위한 전체 시스템 설계
  • l10n (Localization): 특정 언어/지역에 맞춘 리소스 제공

pubspec.yaml

$ flutter pub add <dependency> 로 설치되지 않습니다.
pubspec.yaml에 아래와 같이 직접 입력하셔야 합니다.

Index: pubspec.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/pubspec.yaml b/pubspec.yaml
--- a/pubspec.yaml	(revision HEAD)
+++ b/pubspec.yaml	(revision Staged)
@@ -30,6 +30,10 @@
 dependencies:
   flutter:
     sdk: flutter
+  flutter_localizations:
+    sdk: flutter
+  intl: 0.19.0
+  table_calendar: ^3.1.3
 
 
   # The following adds the Cupertino Icons font to your application.
@@ -41,7 +45,6 @@
   google_sign_in: ^6.2.2
   googleapis: ^13.2.0
   googleapis_auth: ^1.6.0
-  table_calendar: ^3.2.0
   flutter_riverpod: ^2.6.1
   shared_preferences: ^2.5.2
   http: ^1.3.0
@@ -98,3 +101,6 @@
   #
   # For details regarding fonts from package dependencies,
   # see https://flutter.dev/to/font-from-package
+
+  # 국제화 파일 자동 생성을 위한 설정
+  generate: true

$ flutter pub get

l10n.yaml

l10n.yaml 파일은 ARB 파일이 위치한 디렉토리가 지정되어 있습니다.

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-class: AppLocalizations

lib/l10n/

ARB 파일 UTF-8로 인코딩

app_en.arb
{
  "helloWorld": "Hello World",
  "@helloWorld": {
    "description": "The conventional newborn programmer greeting"
  },
  "welcome": "Welcome {name}",
  "@welcome": {
    "description": "Welcome message",
    "placeholders": {
      "name": {
        "type": "String",
        "example": "John"
      }
    }
  }
}
app_ko.arb
{
  "helloWorld": "안녕 세상",
  "welcome": "{name}님 환영합니다"
}

main.dart

Index: lib/main.dart
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/main.dart b/lib/main.dart
--- a/lib/main.dart	(revision HEAD)
+++ b/lib/main.dart	(revision Staged)
@@ -1,6 +1,8 @@
 import 'package:firebase_auth/firebase_auth.dart';
 import 'package:firebase_core/firebase_core.dart';
 import 'package:flutter/material.dart';
+import 'package:flutter_gen/gen_l10n/app_localizations.dart';
+import 'package:flutter_localizations/flutter_localizations.dart';
 import 'package:flutter_riverpod/flutter_riverpod.dart';
 import 'package:pilltime/screen/home_screen.dart';
 import 'package:pilltime/screen/login_screen.dart';
@@ -145,6 +149,23 @@
       themeMode: ThemeMode.system,
       // 디버그 배너 제거
       debugShowCheckedModeBanner: false,
+
+      // 국제화 설정 추가
+      localizationsDelegates: const [
+        AppLocalizations.delegate,
+        GlobalMaterialLocalizations.delegate,
+        GlobalWidgetsLocalizations.delegate,
+        GlobalCupertinoLocalizations.delegate,
+      ],
+      supportedLocales: const [
+        Locale('en', ''), // 영어
+        Locale('ko', ''), // 한국어
+        // 추가 언어는 여기에 추가
+      ],
+      // 사용자 지역 설정에 따라 언어 결정 (선택 사항)
+      locale: Locale('ko', ''),
+      // 영어 강제 설정
+
       // 초기 라우트 설정
       initialRoute: '/',
       // 라우트 정의

$ flutter gen-l10n

생성된 dart 파일은 일반적으로 .dart_tool/flutter_gen/gen_l10n 디렉토리에 있습니다.

$ find .dart_tool -name "app_localizations_**.dart"
.dart_tool/flutter_gen/gen_l10n/app_localizations_ko.dart
.dart_tool/flutter_gen/gen_l10n/app_localizations_en.dart

텍스트 국제화 사용하기

// 텍스트 위젯에서 사용
Text(AppLocalizations.of(context)!.helloWorld)

// 변수로 사용
final localizations = AppLocalizations.of(context)!;
Text(localizations.welcome('사용자명'))

or

// 변경 전:
Text('인증 상태 확인 중 오류가 발생했습니다.')

// 변경 후 (app_en.arb와 app_ko.arb에 해당 키 추가 필요):
Text(AppLocalizations.of(context)!.authError)

0개의 댓글