Last active
July 29, 2021 08:05
-
-
Save sethladd/4344b9cc570fc59539fe83d72510d544 to your computer and use it in GitHub Desktop.
Flutter example of a localized title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017 The Chromium Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style license that can be | |
// found in the LICENSE file. | |
// Note: I'm not the original author, I'm sharing via Gist to make | |
// it easy for folks to check it out. Please email | |
// [email protected] if you have questions about that. | |
// A simple "rough and ready" example of localizing a Flutter app. | |
// Spanish and English (locale language codes 'en' and 'es') are | |
// supported. | |
// The pubspec.yaml file must include flutter_localizations in its | |
// dependencies section. For example: | |
// | |
// dependencies: | |
// flutter: | |
// sdk: flutter | |
// flutter_localizations: | |
// sdk: flutter | |
// If you run this app with the device's locale set to anything but | |
// English or Spanish, the app's locale will be English. If you | |
// set the device's locale to Spanish, the app's locale will be | |
// Spanish. | |
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/foundation.dart' show SynchronousFuture; | |
import 'package:flutter_localizations/flutter_localizations.dart'; | |
class DemoLocalizations { | |
DemoLocalizations(this.locale); | |
final Locale locale; | |
static DemoLocalizations of(BuildContext context) { | |
return Localizations.of<DemoLocalizations>(context, DemoLocalizations); | |
} | |
static Map<String, Map<String, String>> _localizedValues = { | |
'en': { | |
'title': 'Hello World', | |
}, | |
'es': { | |
'title': 'Hola Mundo', | |
}, | |
}; | |
String get title { | |
return _localizedValues[locale.languageCode]['title']; | |
} | |
} | |
class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> { | |
const DemoLocalizationsDelegate(); | |
@override | |
bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode); | |
@override | |
Future<DemoLocalizations> load(Locale locale) { | |
return new SynchronousFuture<DemoLocalizations>(new DemoLocalizations(locale)); | |
} | |
@override | |
bool shouldReload(DemoLocalizationsDelegate old) => false; | |
} | |
class DemoApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(DemoLocalizations.of(context).title), | |
), | |
body: new Center( | |
child: new Text(DemoLocalizations.of(context).title), | |
), | |
); | |
} | |
} | |
class Demo extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
onGenerateTitle: (BuildContext context) => DemoLocalizations.of(context).title, | |
localizationsDelegates: [ | |
const DemoLocalizationsDelegate(), | |
GlobalMaterialLocalizations.delegate, | |
GlobalWidgetsLocalizations.delegate, | |
], | |
supportedLocales: [ | |
const Locale('en', ''), | |
const Locale('es', ''), | |
], | |
// Watch out: MaterialApp creates a Localizations widget | |
// with the specified delegates. DemoLocalizations.of() | |
// will only find the app's Localizations widget if its | |
// context is a child of the app. | |
home: new DemoApp(), | |
); | |
} | |
} | |
void main() { | |
runApp(new Demo()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment