Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add options to set default Locale #40

Closed
wants to merge 1 commit into from

Conversation

juncaixinchi
Copy link

Example to set default locale

import 'package:flutter/material.dart';
import 'package:intl/intl_standalone.dart';

import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() async {
  // set default language or load from configs
  String lan = 'en';
  if (lan == null) {
    // load system locale if lan == null
    final String systemLocale = await findSystemLocale();
    final List<String> systemLocaleSplitted = systemLocale.split('_');
    lan = systemLocaleSplitted[0];
  }

  final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
    useCountryCode: false,
    fallbackFile: 'en',
    path: 'assets/locales',
    defaultLocale: Locale(lan),
  );

  // preload default locale to avoid black screen
  await flutterI18nDelegate.load(null);

  runApp(MyApp(flutterI18nDelegate));
}

class MyApp extends StatelessWidget {
  final FlutterI18nDelegate flutterI18nDelegate;

  MyApp(this.flutterI18nDelegate);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Test'),
        ),
      ),
      localizationsDelegates: [
        flutterI18nDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant