assets
โโโ langs
โโโ ko.json
โโโ en.json
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),
),
);
}
}