0Pricing
Flutter Mobile Development · บทเรียน

ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n

ตั้งค่า flutter_localizations และกระบวนการ gen_l10n เพื่อสร้างคำแปลที่มีชนิดข้อมูลกำกับ

ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flutter Mobile Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Step 1: Why gen_l10n + dependencies

Hard-coding strings like Text('Welcome') makes an app impossible to translate. Flutter's official solution is gen_l10n: you write translations in ARB (Application Resource Bundle, a JSON format) files, and the build tool generates a typed Dart class so typos become compile-time errors.

Two pieces are required in pubspec.yaml. The flutter_localizations SDK package supplies Material/Cupertino/Widgets translations, and the generate: true flag turns on the gen_l10n build step.

  • intl is pulled in because generated code uses it for plurals and dates.
  • After editing, run flutter pub get.
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: any

flutter:
  generate: true

Step 2: l10n.yaml config

Create an l10n.yaml file at the project root. It tells gen_l10n where your ARB files live and what to name the generated class.

  • arb-dir — folder holding the .arb files.
  • template-arb-file — the source-of-truth locale that defines keys and metadata.
  • output-localization-file — name of the generated Dart file.
  • output-class — the class name you import in code.
# l10n.yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-class: AppLocalizations

Step 3: The template ARB file

The template locale (here app_en.arb) defines every key. Each key maps to a translated value. Keys starting with @ are metadata: they describe the entry but produce no string.

  • @@locale declares which locale this file is.
  • @welcome can carry a description to help translators.

The filename pattern is app_<localeCode>.arb.

{
  "@@locale": "en",
  "welcome": "Welcome",
  "@welcome": {
    "description": "Greeting shown on the home screen"
  },
  "settings": "Settings"
}

Step 4: A translated ARB file

For each additional language, add a sibling file with the same keys but translated values. Metadata (the @ keys) is only required in the template; translations may omit it.

  • Here app_tr.arb provides Turkish strings.
  • Missing keys fall back to the template locale, so keep the template complete.
{
  "@@locale": "tr",
  "welcome": "Hoş geldiniz",
  "settings": "Ayarlar"
}

Step 5: Generate the code

Code generation runs automatically during flutter run or flutter build when generate: true is set. You can also force it with flutter gen-l10n.

  • Output lands in .dart_tool/flutter_gen/gen_l10n/ by default.
  • The generated AppLocalizations class exposes one getter per key.
  • Add the generated path to your IDE's import suggestions; do not commit generated files.

Step 6: Wire into MaterialApp

Register the generated delegates and supported locales on MaterialApp. AppLocalizations.localizationsDelegates bundles your delegate plus the Material/Widgets/Cupertino ones, and supportedLocales lists what you ship.

  • Flutter picks the best match between the device locale and supportedLocales.
  • If none match, the first entry in supportedLocales is used.
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      home: const HomeScreen(),
    );
  }
}

Step 7: Read a string

Inside a widget you fetch the localized instance with AppLocalizations.of(context) and read a getter. The call is null only if the delegates are missing, so the common idiom uses !.

  • Each key from the ARB becomes a strongly-typed getter.
  • Rename a key in the ARB and every wrong usage fails to compile.
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    return Scaffold(
      appBar: AppBar(title: Text(l10n.settings)),
      body: Center(child: Text(l10n.welcome)),
    );
  }
}

Step 8: Placeholders

To inject values, declare placeholders in the template metadata. The value uses {name} syntax, and gen_l10n turns the getter into a method.

  • Each placeholder needs a type (e.g. String, int, DateTime).
  • The generated method signature follows the placeholder order.
{
  "@@locale": "en",
  "greeting": "Hello, {name}!",
  "@greeting": {
    "description": "Personalized greeting",
    "placeholders": {
      "name": { "type": "String" }
    }
  }
}

Step 9: Call a placeholder method

Because greeting takes an argument, the generated member is a method, not a getter. You pass the value at the call site.

  • l10n.greeting('Ada') returns "Hello, Ada!".
  • Types are enforced: passing an int where a String is expected fails to compile.
Widget buildGreeting(BuildContext context, String userName) {
  final l10n = AppLocalizations.of(context)!;
  return Text(l10n.greeting(userName));
}

Step 10: Plurals with ICU

ARB supports ICU message syntax for plurals. A {count, plural, ...} block selects the right wording per language. Declare the placeholder as num (or int).

  • =0, one, and other are common categories.
  • # is replaced by the formatted number.
  • Different locales have different plural rules — ICU handles them automatically.
{
  "itemCount": "{count, plural, =0{No items} one{1 item} other{{count} items}}",
  "@itemCount": {
    "placeholders": {
      "count": { "type": "int" }
    }
  }
}

Step 11: A plain-Dart formatter

The selection logic ICU performs is just rules over a number. Here is a tiny standalone Dart program that mimics English plural selection — useful for understanding what gen_l10n generates under the hood.

  • Real apps use the generated method; this is only for intuition.
String itemCount(int count) {
  if (count == 0) return 'No items';
  if (count == 1) return '1 item';
  return '$count items';
}

void main() {
  for (final n in [0, 1, 5]) {
    print(itemCount(n));
  }
}

Quick Check

You added a new key logout only to app_en.arb but forgot it in app_tr.arb. A Turkish-locale user opens the screen. What happens?

Recap

You built the full Flutter localization pipeline:

  • Added flutter_localizations + intl and set generate: true.
  • Configured l10n.yaml (arb-dir, template, output class).
  • Wrote a template ARB plus per-locale translations.
  • Let gen_l10n create the typed AppLocalizations class.
  • Registered localizationsDelegates and supportedLocales, then read strings via AppLocalizations.of(context)!.
  • Used placeholders for dynamic values and ICU plurals for count-aware text.

The payoff: translations are type-checked, missing template keys are caught early, and translators work in a clean JSON format.

คำถามที่พบบ่อย

บทเรียน “ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n”

ตั้งค่า flutter_localizations และกระบวนการ gen_l10n เพื่อสร้างคำแปลที่มีชนิดข้อมูลกำกับ คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม

ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n
  2. พหูพจน์ เพศ และการจัดรูปแบบข้อความ ICU
  3. เลย์เอาต์ RTL และการจัดการทิศทาง
  4. ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้
← กลับไปที่ Flutter Mobile Development