๐
Showcase
๐
Installation
โ๏ธ
ํจํค์ง ์ถ๊ฐ & ๋๋ ํ ๋ฆฌ ์ ์ธ (pubspec.yaml
)
dependencies :
easy_localization : ^3.0.0 # ํ์งํ
flutter_phoenix : ^1.0.0 # ์ฑ ์ฌ์์
flutter :
assets :
- assets/langs/
โ๏ธ
ํด๋์ ๋ฒ์ญ ํ์ผ ์ถ๊ฐ
assets
โโโ langs
โโโ ko.json
โโโ en.json
๊ตญ๊ฐ ์ฝ๋์ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ผ๋, ๋๋ฐ์ด์ค์ locale ์ ๋ณด์ ๋ฐ๋ผ ์ธ์์ ํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๋ ๋ฐ์ ํ๋ฏ๋ก ๋ณธ ์์ ์์๋ ์ธ์ด ์ฝ๋๋ง ์ฌ์ฉ
ko : ํ๊ตญ์ด
en : ์์ด
locale : ์ธ์ด ์ฝ๋ + ๊ตญ๊ฐ ์ฝ๋
ex) ๋๋ฐ์ด์ค์ ์ธ์ด๋ ์์ด, ์ง์ญ์ ๋ํ๋ฏผ๊ตญ์ผ๊ฒฝ์ฐ โ en_KR
๋ฒ์ญ ํ์ผ์ ko_KR๋ก ์ถ๊ฐํ ๊ฒฝ์ฐ ์ธ์ด ์ฝ๋๊ฐ ๋ง์ง ์์ผ๋ฏ๋ก ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋จ
โ๏ธ
iOS
ios/Runner/Info.plist
ํ์ผ์ ์ง์๋๋ locale ์ ๋ณด ์ถ๊ฐ
< key> CFBundleLocalizations</ key>
< array>
< string> en</ string>
< string> ko</ string>
</ array>
๐
Example
import 'package:flutter/material.dart' ;
import 'package:easy_localization/easy_localization.dart' ;
import 'package:flutter_phoenix/flutter_phoenix.dart' ;
import 'package:localization_example/screens/home_screen.dart' ;
import 'package:localization_example/widgets/language_button.dart' ;
import 'constants.dart' ;
void main () async {
// main ๋ฉ์๋์์ ๋น๋๊ธฐ ๋ฉ์๋ ์ฌ์ฉ์ ๋ฐ๋์ ์ถ๊ฐ
WidgetsFlutterBinding .ensureInitialized ();
// ํจํค์ง ์ด๊ธฐํ
await EasyLocalization .ensureInitialized ();
runApp (
Phoenix (
child: EasyLocalization (
supportedLocales: const [en, ko], // ์ง์ํ๋ ์ธ์ด ๋ฆฌ์คํธ
path: 'assets/langs' , // ์ธ์ด ํ์ผ์ด ์๋ ๊ฒฝ๋ก
fallbackLocale: en, // ๊ธฐ๋ณธ๊ฐ
child: const MyApp (),
),
),
);
}
class MyApp extends StatelessWidget {
const MyApp ({Key ? key}) : super (key: key);
@override
Widget build (BuildContext context) {
return MaterialApp (
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: const HomeScreen (),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen ({Key ? key}) : super (key: key);
@override
Widget build (BuildContext context) {
// ๊ธฐ๊ธฐ์ ์ธ์ด ์ค์ ์ด ํ๊ตญ์ด์ผ๊ฒฝ์ฐ ko, ์์ด์ผ ๊ฒฝ์ฐ en, ๊ทธ ์ธ์ผ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ธ en ์ถ๋ ฅ
debugPrint ('Locale : ${context .locale } ' );
return Scaffold (
appBar: AppBar (
title: const Text ('appBar' ).tr (),
),
body: Center (
child: Column (
mainAxisAlignment: MainAxisAlignment .center,
children: const [
LanguageButton (
text: 'ํ๊ตญ์ด' ,
locale: ko,
),
SizedBox (height: 12 ),
LanguageButton (
text: 'English' ,
locale: en,
),
],
),
),
);
}
}
class LanguageButton extends StatelessWidget {
const LanguageButton ({
Key ? key,
required this .text,
required this .locale,
}) : super (key: key);
final String text;
final Locale locale;
@override
Widget build (BuildContext context) {
return InkWell (
onTap: () async {
await context.setLocale (locale);
await EasyLocalization .ensureInitialized ();
Phoenix .rebirth (context);
},
child: Container (
padding: const EdgeInsets .symmetric (horizontal: 12 , vertical: 6 ),
decoration: BoxDecoration (
border: Border .all (color: Colors .grey),
borderRadius: const BorderRadius .all (Radius .circular (4 )),
),
child: Text (text),
),
);
}
}
โ๏ธ
locale ๋ณ๊ฒฝ
await context.setLocale (locale);
โ๏ธ
๋ฒ์ญ (tr()
)
{
"appBar" : " Localization Example"
}
{
"appBar" : " Localization ์์ "
}
// en : Localization Example
// ko : Localization ์์
const Text ('appBar' ).tr (),
โ๏ธ
์ฑ ์ฌ์์
Phoenix .rebirth (context);