Skip to content

Commit a5883d9

Browse files
committed
Fixed packaging issues
1 parent 22edebc commit a5883d9

File tree

8 files changed

+81
-11
lines changed

8 files changed

+81
-11
lines changed

example/lib/app.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_localizations/flutter_localizations.dart';
3+
import 'package:gettext_i18n/gettext_i18n.dart';
4+
5+
import 'home_page.dart';
6+
7+
class MyApp extends StatelessWidget {
8+
const MyApp({Key? key}) : super(key: key);
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return MaterialApp(
13+
home: const HomePage(),
14+
supportedLocales: ['en', 'fr', 'fr_CH'].map((l) => Locale(l)),
15+
localizationsDelegates: [
16+
GettextLocalizationsDelegate(defaultLanguage: 'en'),
17+
GlobalMaterialLocalizations.delegate,
18+
GlobalWidgetsLocalizations.delegate,
19+
],
20+
localeListResolutionCallback: (locales, supportedLocales) {
21+
if (locales != null) {
22+
for (var locale in locales) {
23+
var supportedLocale = supportedLocales.where((element) =>
24+
element.languageCode == locale.languageCode &&
25+
element.countryCode == locale.countryCode);
26+
if (supportedLocale.isNotEmpty) {
27+
return supportedLocale.first;
28+
}
29+
supportedLocale = supportedLocales.where(
30+
(element) => element.languageCode == locale.languageCode);
31+
if (supportedLocale.isNotEmpty) {
32+
return supportedLocale.first;
33+
}
34+
}
35+
}
36+
return null;
37+
},
38+
);
39+
}
40+
}

example/lib/home_page.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:gettext_i18n/gettext_i18n.dart';
3+
4+
class HomePage extends StatelessWidget {
5+
const HomePage({Key? key}) : super(key: key);
6+
7+
@override
8+
Widget build(BuildContext context) {
9+
return Center(
10+
child: Text(context.t('Hello, world !')),
11+
);
12+
}
13+
}

example/lib/main.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'app.dart';
4+
5+
void main() {
6+
runApp(const MyApp());
7+
}

lib/gettext_i18n.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
library gettext_i18n;
22

33
export 'src/context_ext.dart' show ContextExt;
4-
export 'src/gettext_localizations_delegate.dart' show GettextLocalizationsDelegate;
4+
export 'src/gettext_localizations_delegate.dart'
5+
show GettextLocalizationsDelegate;

lib/src/context_ext.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import 'package:flutter/material.dart';
22
import 'gettext_localizations.dart';
33

44
extension ContextExt on BuildContext {
5-
t(String key, {List<Object>? args}) => GettextLocalizations.of(this).t(key, args);
5+
t(String key, {List<Object>? args}) =>
6+
GettextLocalizations.of(this).t(key, args);
67
}

lib/src/gettext_localizations.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class GettextLocalizations {
77
final _gt = Gettext(
88
onWarning: ((message) {
99
if (kDebugMode) {
10-
final r = RegExp(r'^No translation was found for msgid "(.*)" in msgctxt "(.*)" and domain "(.*)"$');
10+
final r = RegExp(
11+
r'^No translation was found for msgid "(.*)" in msgctxt "(.*)" and domain "(.*)"$');
1112
final matches = r.firstMatch(message);
1213
var msgid = matches!.group(1);
1314
// ignore: avoid_print
@@ -20,7 +21,8 @@ class GettextLocalizations {
2021
_gt.addLocale(gettext_parser.po.parse(poContent));
2122
}
2223

23-
static GettextLocalizations of(BuildContext context) => Localizations.of<GettextLocalizations>(context, GettextLocalizations)!;
24+
static GettextLocalizations of(BuildContext context) =>
25+
Localizations.of<GettextLocalizations>(context, GettextLocalizations)!;
2426

2527
String t(String key, List<Object>? args) {
2628
var message = _gt.gettext(key);

lib/src/gettext_localizations_delegate.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'package:flutter/services.dart';
33

44
import 'gettext_localizations.dart';
55

6-
class GettextLocalizationsDelegate extends LocalizationsDelegate<GettextLocalizations> {
6+
class GettextLocalizationsDelegate
7+
extends LocalizationsDelegate<GettextLocalizations> {
78
GettextLocalizationsDelegate({this.defaultLanguage = "en"});
89

910
final String defaultLanguage;
@@ -15,18 +16,23 @@ class GettextLocalizationsDelegate extends LocalizationsDelegate<GettextLocaliza
1516
Future<GettextLocalizations> load(Locale locale) async {
1617
var poContent = '';
1718
try {
18-
poContent = await rootBundle.loadString('assets/i18n/${locale.languageCode}_${locale.countryCode}.po');
19+
poContent = await rootBundle.loadString(
20+
'assets/i18n/${locale.languageCode}_${locale.countryCode}.po');
1921
} catch (e) {
2022
try {
21-
poContent = await rootBundle.loadString('assets/i18n/${locale.languageCode}.po');
23+
poContent = await rootBundle
24+
.loadString('assets/i18n/${locale.languageCode}.po');
2225
} catch (e) {
23-
poContent = await rootBundle.loadString('assets/i18n/$defaultLanguage.po');
26+
poContent =
27+
await rootBundle.loadString('assets/i18n/$defaultLanguage.po');
2428
}
2529
}
2630

2731
return GettextLocalizations.fromPO(poContent);
2832
}
2933

3034
@override
31-
bool shouldReload(covariant LocalizationsDelegate<GettextLocalizations> old) => true;
35+
bool shouldReload(
36+
covariant LocalizationsDelegate<GettextLocalizations> old) =>
37+
true;
3238
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: gettext_i18n
22
description: This package helps internationalizing a Flutter application.
3-
version: 1.0.0
4-
homepage:
3+
version: 1.0.1
4+
repository: https://github.com/frenchguych/gettext_i18n
55

66
environment:
77
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)