๐Ÿ“Œย 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())

  • ../en.json

{
  "appBar" : "Localization Example"
}
  • ../ko.json

{
  "appBar" : "Localization ์˜ˆ์ œ"
}

// en : Localization Example
// ko : Localization ์˜ˆ์ œ
const Text('appBar').tr(),

โœ”๏ธย ์•ฑ ์žฌ์‹œ์ž‘

Phoenix.rebirth(context);

GitHub

View Github