localizationsDelegates

localizationsDelegates

MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})

açıklama

Localizations widgeti tarafından yüklenecek, T türü bir dizi yerelleştirilmiş kaynak için bir üreteç.

konum

 
MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})

parametreler

    
MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})

                      //localizasyon.........................................................................................                                 
                                          locale: Localizations.localeOf(context); //Başlangıç yerel ayarı.Eğer 'yerel' boşsa o zaman sistemin yerel değeri kullanılır.
                          localizationsDelegates:   [
                                                        AppLocalizationsDelegate(),  // bizim delegemiz
                                                        GlobalMaterialLocalizations.delegate,
                                                        GlobalWidgetsLocalizations.delegate
                                                     ],
                                  supportedLocales:   const [Locale('en', 'US'), Locale('es', 'ES'), Locale('tr', 'TR')],
                                  onGenerateTitle:  (BuildContext context) => AppLocalizations.of(context).title, // bizim localizasyon sınıfımız ve kullanımı

örnek uygulama

anilcanlocalization-orn1
anilcanlocalization-orn2
 
dosya adı: main.dart

kaynak: Anılcan Çakır
GitHub: GitHub
medium: Makale

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

class DemoLocalizations {
  DemoLocalizations(this.locale);

  final Locale locale;

  static DemoLocalizations of(BuildContext context) {
    return Localizations.of(context, DemoLocalizations);
  }

  Map<String, String> _sentences;

  Future load() async {
    String data = await rootBundle.loadString('resources/lang/${this.locale.languageCode}.json');
    Map<String, dynamic> _result = json.decode(data);

    this._sentences = new Map();
    _result.forEach((String key, dynamic value) {
      this._sentences[key] = value.toString();
    });

    return true;
  }

  String trans(String key) {
    return this._sentences[key];
  }
}

class DemoLocalizationsDelegate extends LocalizationsDelegate {
  const DemoLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) => ['tr', 'en'].contains(locale.languageCode);

  @override
  Future load(Locale locale) async {
    DemoLocalizations localizations = new DemoLocalizations(locale);
    await localizations.load();

    print("Load ${locale.languageCode}");

    return localizations;
  }

  @override
  bool shouldReload(DemoLocalizationsDelegate old) => false;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      supportedLocales: [
        const Locale('tr', 'TR'),
        const Locale('en', 'US')
      ],
      localizationsDelegates: [
        const DemoLocalizationsDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      localeResolutionCallback: (Locale locale, Iterable supportedLocales) {
        for (Locale supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode || supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }

        return supportedLocales.first;
      },
      title: 'Flutter Internationalization',
      home: new MyPage(),
    );
  }
}

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Text(
          DemoLocalizations.of(context).trans('hello_world')
        ),
      ),
    );
  }
}

void main() {
  runApp(new MyApp());
}
 
dosya adı: en.json

{
  "hello_world": "Hello world"
}

 
dosya adı: tr.json

{
  "hello_world": "Merhaba dünya"
}

 
dosya adı: pubspec.yaml


name: internationalization
description: A new Flutter project with internationalization by json.

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - resources/lang/tr.json
    - resources/lang/en.json

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies, 
  # see https://flutter.io/custom-fonts/#from-packages

Bir cevap yazın

KAPAT
%d blogcu bunu beğendi: