İçeriğe git
Flutter i18n

Flutter i18n: Tek Kod Tabanı, Her Dil, Her Platform

Flutter, tek bir kod tabanından iOS, Android, web ve masaüstü genelinde lokalizasyonu yönetmek için ARB (Application Resource Bundle) dosyalarını ve intl paketini kullanır. gen-l10n aracı, ARB şablonlarınızdan tür açısından güvenli Dart kodu oluşturarak her çeviri anahtarı için derleme zamanı güvenliği ve IDE otomatik tamamlama imkânı sunar.

Get started in 4 steps

1

Enable generation

Add the generate flag to your pubspec.yaml to enable localization code generation.

pubspec.yaml
# pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: any

flutter:
  generate: true
2

Configure l10n.yaml

Create l10n.yaml in your project root to configure the localization generator.

l10n.yaml
# l10n.yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
3

Create ARB template

Add your source language ARB file with translation keys and metadata.

lib/l10n/app_en.arb
{
  "@@locale": "en",
  "welcome": "Welcome to {appName}",
  "@welcome": {
    "placeholders": {
      "appName": { "type": "String" }
    }
  },
  "itemCount": "{count, plural, =0{No items} =1{1 item} other{{count} items}}",
  "@itemCount": {
    "placeholders": {
      "count": { "type": "int" }
    }
  }
}
4

Use in your app

Wrap your app with localization delegates and use AppLocalizations.of(context) in widgets.

lib/main.dart
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    return Text(l10n.welcome('Flutter'));
  }
}

Flutter Lokalizasyon Özellikleri

ICU MessageFormat söz dizimi ve meta veri desteğiyle ARB dosya biçimi
Tür açısından güvenli çeviriler için gen-l10n aracılığıyla otomatik Dart kod üretimi
Derleme zamanı doğrulamalı ICU çoğullama ve select kuralları
iOS, Android, web ve masaüstü için tek kod tabanıyla lokalizasyon
Geliştirme sırasında anlık çeviri önizlemesi için hot reload desteği
Tarihler, metin yönü ve erişilebilirlik için Material ve Cupertino widget lokalizasyonları
Otomatik metin ve widget hizalamasıyla sağdan sola (RTL) düzen desteği
intl paketi aracılığıyla yerel ayara duyarlı tarih, sayı ve para birimi biçimlendirmesi
AppLocalizations.of(context) ile BuildContext tabanlı yerel ayar erişimi

Flutter Yerelleştirme Uygulamaları

Widget'larınızda oluşturulan çeviri yöntemlerine erişmek için AppLocalizations.of(context) kullanın; çoğullama ve tarih biçimlendirme için tam destek mevcuttur.

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class ProductPage extends StatelessWidget {
  final int itemCount;
  const ProductPage({required this.itemCount});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    return Column(
      children: [
        Text(l10n.welcome('My Store')),
        Text(l10n.itemCount(itemCount)),
        // Date formatting respects locale
        Text(DateFormat.yMMMd(
          Localizations.localeOf(context).toString()
        ).format(DateTime.now())),
      ],
    );
  }
}

Flutter Lokalizasyonunu Bugün Basitleştirin

ARB çevirilerinizi yapay zeka destekli iş akışları, Flutter projenize CLI senkronizasyonu ve 50 ms altında CDN dağıtımıyla yönetin.