Skip to content

Commit a23ef4c

Browse files
author
Tensor-Programming
authored
added localization
1 parent f309890 commit a23ef4c

15 files changed

+355
-19
lines changed

Diff for: lib/const.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const API_KEY = '[API KEY HERE]';
1+
const API_KEY = '78b288f2b2d9382cb2cc9436e08cd916';

Diff for: lib/l10n/intl_en.arb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"title": "Weather Application",
3+
"@title": {
4+
"description": "Title for the Weather application",
5+
"type": "text"
6+
},
7+
8+
"button": "Get the Weather",
9+
"@button": {
10+
"description": "get weather button",
11+
"type": "text",
12+
"placeholders": {}
13+
}
14+
}

Diff for: lib/l10n/intl_es.arb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"title": "Aplicación Meteorológica",
3+
"@title": {
4+
"description": "Title for the Weather application",
5+
"type": "text"
6+
},
7+
8+
"button": "obtener el clima",
9+
"@button": {
10+
"description": "get weather button",
11+
"type": "text",
12+
"placeholders": {}
13+
}
14+
}

Diff for: lib/l10n/intl_ja.arb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"title": "天気アプリケーション",
3+
"@title": {
4+
"description": "Title for the Weather application",
5+
"type": "text"
6+
},
7+
"button": "天気を取る",
8+
"@button": {
9+
"description": "get weather button",
10+
"type": "text",
11+
"placeholders": {}
12+
}
13+
14+
}

Diff for: lib/l10n/intl_messages.arb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"@@last_modified": "2018-04-27T17:16:00.180810",
3+
"title": "Weather Application",
4+
"@title": {
5+
"description": "Title for the Weather Application",
6+
"type": "text",
7+
"placeholders": {}
8+
},
9+
"button": "Get the Weather",
10+
"@button": {
11+
"description": "get weather button",
12+
"type": "text",
13+
"placeholders": {}
14+
}
15+
}

Diff for: lib/l10n/messages_all.dart

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that looks up messages for specific locales by
3+
// delegating to the appropriate library.
4+
5+
import 'dart:async';
6+
7+
import 'package:intl/intl.dart';
8+
import 'package:intl/message_lookup_by_library.dart';
9+
// ignore: implementation_imports
10+
import 'package:intl/src/intl_helpers.dart';
11+
12+
import 'messages_es.dart' as messages_es;
13+
import 'messages_en.dart' as messages_en;
14+
import 'messages_messages.dart' as messages_messages;
15+
import 'messages_ja.dart' as messages_ja;
16+
17+
typedef Future<dynamic> LibraryLoader();
18+
Map<String, LibraryLoader> _deferredLibraries = {
19+
'es': () => new Future.value(null),
20+
'en': () => new Future.value(null),
21+
'messages': () => new Future.value(null),
22+
'ja': () => new Future.value(null),
23+
};
24+
25+
MessageLookupByLibrary _findExact(localeName) {
26+
switch (localeName) {
27+
case 'es':
28+
return messages_es.messages;
29+
case 'en':
30+
return messages_en.messages;
31+
case 'messages':
32+
return messages_messages.messages;
33+
case 'ja':
34+
return messages_ja.messages;
35+
default:
36+
return null;
37+
}
38+
}
39+
40+
/// User programs should call this before using [localeName] for messages.
41+
Future<bool> initializeMessages(String localeName) async {
42+
var availableLocale = Intl.verifiedLocale(
43+
localeName,
44+
(locale) => _deferredLibraries[locale] != null,
45+
onFailure: (_) => null);
46+
if (availableLocale == null) {
47+
return new Future.value(false);
48+
}
49+
var lib = _deferredLibraries[availableLocale];
50+
await (lib == null ? new Future.value(false) : lib());
51+
initializeInternalMessageLookup(() => new CompositeMessageLookup());
52+
messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
53+
return new Future.value(true);
54+
}
55+
56+
bool _messagesExistFor(String locale) {
57+
try {
58+
return _findExact(locale) != null;
59+
} catch (e) {
60+
return false;
61+
}
62+
}
63+
64+
MessageLookupByLibrary _findGeneratedMessagesFor(locale) {
65+
var actualLocale = Intl.verifiedLocale(locale, _messagesExistFor,
66+
onFailure: (_) => null);
67+
if (actualLocale == null) return null;
68+
return _findExact(actualLocale);
69+
}

Diff for: lib/l10n/messages_en.dart

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a en locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
import 'package:intl/intl.dart';
7+
import 'package:intl/message_lookup_by_library.dart';
8+
9+
final messages = new MessageLookup();
10+
11+
// ignore: unused_element
12+
final _keepAnalysisHappy = Intl.defaultLocale;
13+
14+
// ignore: non_constant_identifier_names
15+
typedef MessageIfAbsent(String message_str, List args);
16+
17+
class MessageLookup extends MessageLookupByLibrary {
18+
get localeName => 'en';
19+
20+
final messages = _notInlinedMessages(_notInlinedMessages);
21+
static _notInlinedMessages(_) => <String, Function>{
22+
"title": MessageLookupByLibrary.simpleMessage("Weather Application"),
23+
"button": MessageLookupByLibrary.simpleMessage("Get the Weather")
24+
};
25+
}

Diff for: lib/l10n/messages_es.dart

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a es locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
import 'package:intl/intl.dart';
7+
import 'package:intl/message_lookup_by_library.dart';
8+
9+
final messages = new MessageLookup();
10+
11+
// ignore: unused_element
12+
final _keepAnalysisHappy = Intl.defaultLocale;
13+
14+
// ignore: non_constant_identifier_names
15+
typedef MessageIfAbsent(String message_str, List args);
16+
17+
class MessageLookup extends MessageLookupByLibrary {
18+
get localeName => 'es';
19+
20+
final messages = _notInlinedMessages(_notInlinedMessages);
21+
static _notInlinedMessages(_) => <String, Function>{
22+
"title":
23+
MessageLookupByLibrary.simpleMessage("Aplicación Meteorológica"),
24+
"button": MessageLookupByLibrary.simpleMessage("Obtener el Clima")
25+
};
26+
}

Diff for: lib/l10n/messages_ja.dart

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a ja locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
import 'package:intl/intl.dart';
7+
import 'package:intl/message_lookup_by_library.dart';
8+
9+
final messages = new MessageLookup();
10+
11+
// ignore: unused_element
12+
final _keepAnalysisHappy = Intl.defaultLocale;
13+
14+
// ignore: non_constant_identifier_names
15+
typedef MessageIfAbsent(String message_str, List args);
16+
17+
class MessageLookup extends MessageLookupByLibrary {
18+
get localeName => 'ja';
19+
20+
final messages = _notInlinedMessages(_notInlinedMessages);
21+
static _notInlinedMessages(_) => <String, Function>{
22+
"title": MessageLookupByLibrary.simpleMessage("天気アプリケーション"),
23+
"button": MessageLookupByLibrary.simpleMessage("天気を取る")
24+
};
25+
}

Diff for: lib/l10n/messages_messages.dart

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a messages locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
import 'package:intl/intl.dart';
7+
import 'package:intl/message_lookup_by_library.dart';
8+
9+
final messages = new MessageLookup();
10+
11+
// ignore: unused_element
12+
final _keepAnalysisHappy = Intl.defaultLocale;
13+
14+
// ignore: non_constant_identifier_names
15+
typedef MessageIfAbsent(String message_str, List args);
16+
17+
class MessageLookup extends MessageLookupByLibrary {
18+
get localeName => 'messages';
19+
20+
final messages = _notInlinedMessages(_notInlinedMessages);
21+
static _notInlinedMessages(_) => <String, Function>{
22+
"title": MessageLookupByLibrary.simpleMessage("Weather Application"),
23+
"button": MessageLookupByLibrary.simpleMessage("Get the Weather")
24+
};
25+
}

Diff for: lib/localization/localizations.dart

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:intl/intl.dart';
2+
import 'package:flutter/material.dart';
3+
4+
import 'package:weather/l10n/messages_all.dart';
5+
6+
import 'dart:async';
7+
8+
class AppLocalizations {
9+
static Future<AppLocalizations> load(Locale locale) {
10+
final String name =
11+
locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
12+
final localeName = Intl.canonicalizedLocale(name);
13+
14+
return initializeMessages(localeName).then((bool _) {
15+
Intl.defaultLocale = localeName;
16+
return AppLocalizations();
17+
});
18+
}
19+
20+
static AppLocalizations of(BuildContext context) {
21+
return Localizations.of<AppLocalizations>(context, AppLocalizations);
22+
}
23+
24+
String get title {
25+
return Intl.message(
26+
'Weather Application',
27+
name: 'title',
28+
desc: 'Title for the Weather Application',
29+
);
30+
}
31+
32+
String get button {
33+
return Intl.message(
34+
'Get the Weather',
35+
name: 'button',
36+
desc: 'get weather button',
37+
);
38+
}
39+
}
40+
41+
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
42+
const AppLocalizationsDelegate();
43+
44+
@override
45+
bool isSupported(Locale locale) {
46+
return ['en', 'es', 'ja'].contains(locale.languageCode);
47+
}
48+
49+
@override
50+
Future<AppLocalizations> load(Locale locale) {
51+
return AppLocalizations.load(locale);
52+
}
53+
54+
@override
55+
bool shouldReload(AppLocalizationsDelegate old) {
56+
return false;
57+
}
58+
}

Diff for: lib/main.dart

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_localizations/flutter_localizations.dart';
23

34
import 'package:weather/model/weather_repo.dart';
45
import 'package:weather/model/model_command.dart';
56
import 'package:weather/model/model.dart';
67
import 'package:weather/model/model_provider.dart';
78

9+
import 'package:weather/localization/localizations.dart';
810
//unimport if you need the initState function stuff.
911

10-
// import 'package:geolocation/geolocation.dart';
12+
import 'package:geolocation/geolocation.dart';
1113

1214
import 'package:rx_widgets/rx_widgets.dart';
1315

@@ -28,7 +30,18 @@ class MyApp extends StatelessWidget {
2830
@override
2931
Widget build(BuildContext context) {
3032
return MaterialApp(
31-
title: 'Weather Demo',
33+
localizationsDelegates: [
34+
AppLocalizationsDelegate(),
35+
GlobalMaterialLocalizations.delegate,
36+
GlobalWidgetsLocalizations.delegate,
37+
],
38+
supportedLocales: [
39+
Locale('en', ""),
40+
Locale("es", ""),
41+
Locale('ja', ''),
42+
],
43+
onGenerateTitle: (BuildContext context) =>
44+
AppLocalizations.of(context).title,
3245
theme: ThemeData.dark(),
3346
home: MyHomePage(),
3447
);
@@ -44,20 +57,36 @@ class _MyHomePageState extends State<MyHomePage> {
4457
@override
4558
void initState() {
4659
super.initState();
47-
//If Geolocation is unable to get location in emulator, uncomment this and then restart the program.
48-
//This tends to fix the error and you can see if the GPS is actually getting the location.
49-
// var x = Geolocation.locationUpdates(
50-
// accuracy: LocationAccuracy.best, inBackground: false);
51-
// x.listen((d) => print(d.isSuccessful));
60+
61+
// If Geolocation is unable to get location in emulator, uncomment this and then restart the program.
62+
// This tends to fix the error and you can see if the GPS is actually getting the location.
63+
var x = Geolocation.locationUpdates(
64+
accuracy: LocationAccuracy.best, inBackground: false);
65+
x.listen((d) => print(d.isSuccessful));
5266
}
5367

5468
@override
5569
Widget build(BuildContext context) {
70+
Locale myLocale = Localizations.localeOf(context);
71+
5672
//call to getGpsCommand handler on build. If GPS is active comes back with true, else false.
73+
ModelProvider
74+
.of(context)
75+
.changeLocaleCommand
76+
.call(myLocale.languageCode.toString());
5777
ModelProvider.of(context).getGpsCommand.call();
78+
5879
return Scaffold(
5980
appBar: AppBar(
60-
title: Text('Weather App'),
81+
title: Text(
82+
AppLocalizations.of(context).title.toString(),
83+
style: TextStyle(
84+
fontSize: myLocale.languageCode.contains("es") ||
85+
myLocale.languageCode.contains("ja")
86+
? 15.0
87+
: 20.0,
88+
),
89+
),
6190
actions: <Widget>[
6291
Container(
6392
child: Center(
@@ -126,7 +155,7 @@ class _MyHomePageState extends State<MyHomePage> {
126155
onTrue: MaterialButton(
127156
elevation: 5.0,
128157
color: Colors.blueGrey,
129-
child: Text("Get the Weather"),
158+
child: Text(AppLocalizations.of(context).button),
130159
onPressed: ModelProvider
131160
.of(context)
132161
.updateLocationStreamCommand

0 commit comments

Comments
 (0)