이 포스팅은 Flutter 3.24 버전 기준으로 작성되었습니다. 기본적인 flutter 프로젝트가 생성되어있는 상태를 가정하고 진행합니다.
flutter pub add easy_localization
pubspec.yaml
파일에 다음 코드가 추가됩니다.dependencies:
easy_localization: version
📦 assets
┣ 📂 translations
┃ ┣ 📜 en_US.json
┗ ┗ 📜 ko_KR.json
📦 lib
┣ 📜 home_page.dart
┗ 📜 main.dart
en_US.json
, 한국어는 ko_KR.json
파일을 만듭니다. 추가로, 지역 코드를 사용하는 경우 지역 코드를 파일 명에 포함시킵니다. 예를 들어 미국 영어는 en_US.json
파일을 만듭니다.
확장자는 JSON,CSV,HTTP,XML,Yaml 등을 지원한다고 합니다. 자세한 내용은 공식 문서 - Easy Localization Loader↗를 참고해 주세요.pubspec.yaml
파일에 다음 코드를 추가합니다.assets:
- assets/translations/
<dict>
태그 내에 다음 코드를 추가합니다....
<key>CFBundleLocalizations</key>
<array>
<string>en_US</string>
<string>ko_KR</string>
</array>
...
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:example_project/home_page.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized(); // 언어 팩 초기화
runApp(
EasyLocalization(
// 언어 팩 설정
supportedLocales: const [Locale('en', 'US'), Locale('ko', 'KR')],
path: 'assets/translations',
fallbackLocale: const Locale('en', 'US'), // 휴대폰 설정이 없을 시 기본 언어
child: const MyApp()),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: const HomePage());
}
}
{
"hello": "Hello, world!"
}
{
"hello": "안녕하세요!"
}
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
context.tr('hello'), // 언어 팩 데이터 사용
style: const TextStyle(fontSize: 20),
),
ElevatedButton( // 한국어 버튼
onPressed: () {
context.setLocale(const Locale('ko', 'KR'));
},
child: const Text('한국어'),
),
ElevatedButton( // 영어 버튼
onPressed: () {
context.setLocale(const Locale('en', 'US'));
},
child: const Text('영어'),
),
],
),
),
);
}
}
{
"hello": "Hello, {}!",
"hello_friends": "Hello, {}, {}!",
"hello_name": "Hello, {name}!",
"hello_name_age": "Hello, {name}! You are {age} years old.",
"hello_friends_name": "Hello, {}, {name}!",
}
// ...
context.tr('hello', args: ['John']);
context.tr('hello_friends', args: ['John', 'Jane']);
context.tr('hello_name', namedArgs: {'name': 'John'});
context.tr('hello_name_age', namedArgs: {'name': 'John', 'age': 20});
context.tr('hello_friends_name', args: ['John'], namedArgs: {'name': 'Jane'});
// ...
{
"gender":{
"male":"Hi man ;) {}",
"female":"Hello girl :) {}",
"other":"Hello {}"
}
}
// ...
context.tr("gender", gender: _gender ? "female" : "male");
// ...
{
"money": {
"zero": "You not have money",
"one": "You have {} dollar",
"many": "You have {} dollars",
"other": "You have {} dollars"
},
"money_named_args": { // 이름 지정
"zero": "{name} has no money",
"one": "{name} has {money} dollar",
"many": "{name} has {money} dollars",
"other": "{name} has {money} dollars"
}
}
// ...
context.plural('money', 0);
context.plural('money', 1);
context.plural('money', 2);
context.plural('money_named_args', 10.23, namedArgs: {'name': 'Jane'}, name: 'money') // 이름 지정
// ...
.
을 붙이고 하위 계층의 key를 추가하면 됩니다. 또한 동일 계층에 있는 데이터를 사용하고 싶다면 언어 팩 안에서 @:
을 사용하면 됩니다.{
"example": {
"hello": "Hello",
"world": "World!",
"helloWorld": "@:example.hello @:example.world" // 이렇게 사용합니다.
}
}
// ...
context.tr('example.hello'); // 결과: Hello
context.tr('example.world'); // 결과: World!
context.tr('example.helloWorld'); // 결과: Hello World!
// ...
// ...
context.resetLocale();
// ...
// ...
context.deviceLocale.toString() // 결과: en_US
// ...
// ...
context.setLocale(Locale('ko', 'KR')); // 한국어로 설정
// ...
Note: intl is pinned to version 0.19.0 by flutter_localizations from the flutter SDK.
See https://dart.dev/go/sdk-version-pinning for details.
The current Dart SDK version is 3.5.4.
Because easy_localization >=2.3.4-nullsafety <3.0.0-dev.0 depends on flutter_localizations from sdk and easy_localization >=3.0.0-dev.0 <3.0.0-nullsafety doesn't support null safety,
easy_localization >=2.3.4-nullsafety <3.0.0-nullsafety requires flutter_localizations from sdk.
And because easy_localization <2.3.4-nullsafety doesn't support null safety, easy_localization <3.0.0-nullsafety requires flutter_localizations from sdk.
And because easy_localization >=3.0.0-nullsafety depends on flutter_localizations from sdk which depends on intl 0.19.0, every version of easy_localization requires intl 0.19.0.
So, because nftcam_flutter depends on both intl ^0.20.1 and easy_localization any, version solving failed.
The lower bound of "sdk: '>=2.0.0-dev.68.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.
For details, see https://dart.dev/null-safety
The lower bound of "sdk: '>=2.7.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.
For details, see https://dart.dev/null-safety
flutter channel master
flutter upgrade
// ...
const newLocale = Locale('ko', 'KR');
await context.setLocale(newLocale);
Get.updateLocale(newLocale);
// ...