Skip to content

Commit

Permalink
Fixed packaging issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ymauray committed Nov 14, 2021
1 parent 22edebc commit a5883d9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 11 deletions.
40 changes: 40 additions & 0 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:gettext_i18n/gettext_i18n.dart';

import 'home_page.dart';

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: const HomePage(),
supportedLocales: ['en', 'fr', 'fr_CH'].map((l) => Locale(l)),
localizationsDelegates: [
GettextLocalizationsDelegate(defaultLanguage: 'en'),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
localeListResolutionCallback: (locales, supportedLocales) {
if (locales != null) {
for (var locale in locales) {
var supportedLocale = supportedLocales.where((element) =>
element.languageCode == locale.languageCode &&
element.countryCode == locale.countryCode);
if (supportedLocale.isNotEmpty) {
return supportedLocale.first;
}
supportedLocale = supportedLocales.where(
(element) => element.languageCode == locale.languageCode);
if (supportedLocale.isNotEmpty) {
return supportedLocale.first;
}
}
}
return null;
},
);
}
}
13 changes: 13 additions & 0 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';
import 'package:gettext_i18n/gettext_i18n.dart';

class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Center(
child: Text(context.t('Hello, world !')),
);
}
}
7 changes: 7 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:flutter/material.dart';

import 'app.dart';

void main() {
runApp(const MyApp());
}
3 changes: 2 additions & 1 deletion lib/gettext_i18n.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library gettext_i18n;

export 'src/context_ext.dart' show ContextExt;
export 'src/gettext_localizations_delegate.dart' show GettextLocalizationsDelegate;
export 'src/gettext_localizations_delegate.dart'
show GettextLocalizationsDelegate;
3 changes: 2 additions & 1 deletion lib/src/context_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import 'package:flutter/material.dart';
import 'gettext_localizations.dart';

extension ContextExt on BuildContext {
t(String key, {List<Object>? args}) => GettextLocalizations.of(this).t(key, args);
t(String key, {List<Object>? args}) =>
GettextLocalizations.of(this).t(key, args);
}
6 changes: 4 additions & 2 deletions lib/src/gettext_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class GettextLocalizations {
final _gt = Gettext(
onWarning: ((message) {
if (kDebugMode) {
final r = RegExp(r'^No translation was found for msgid "(.*)" in msgctxt "(.*)" and domain "(.*)"$');
final r = RegExp(
r'^No translation was found for msgid "(.*)" in msgctxt "(.*)" and domain "(.*)"$');
final matches = r.firstMatch(message);
var msgid = matches!.group(1);
// ignore: avoid_print
Expand All @@ -20,7 +21,8 @@ class GettextLocalizations {
_gt.addLocale(gettext_parser.po.parse(poContent));
}

static GettextLocalizations of(BuildContext context) => Localizations.of<GettextLocalizations>(context, GettextLocalizations)!;
static GettextLocalizations of(BuildContext context) =>
Localizations.of<GettextLocalizations>(context, GettextLocalizations)!;

String t(String key, List<Object>? args) {
var message = _gt.gettext(key);
Expand Down
16 changes: 11 additions & 5 deletions lib/src/gettext_localizations_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'package:flutter/services.dart';

import 'gettext_localizations.dart';

class GettextLocalizationsDelegate extends LocalizationsDelegate<GettextLocalizations> {
class GettextLocalizationsDelegate
extends LocalizationsDelegate<GettextLocalizations> {
GettextLocalizationsDelegate({this.defaultLanguage = "en"});

final String defaultLanguage;
Expand All @@ -15,18 +16,23 @@ class GettextLocalizationsDelegate extends LocalizationsDelegate<GettextLocaliza
Future<GettextLocalizations> load(Locale locale) async {
var poContent = '';
try {
poContent = await rootBundle.loadString('assets/i18n/${locale.languageCode}_${locale.countryCode}.po');
poContent = await rootBundle.loadString(
'assets/i18n/${locale.languageCode}_${locale.countryCode}.po');
} catch (e) {
try {
poContent = await rootBundle.loadString('assets/i18n/${locale.languageCode}.po');
poContent = await rootBundle
.loadString('assets/i18n/${locale.languageCode}.po');
} catch (e) {
poContent = await rootBundle.loadString('assets/i18n/$defaultLanguage.po');
poContent =
await rootBundle.loadString('assets/i18n/$defaultLanguage.po');
}
}

return GettextLocalizations.fromPO(poContent);
}

@override
bool shouldReload(covariant LocalizationsDelegate<GettextLocalizations> old) => true;
bool shouldReload(
covariant LocalizationsDelegate<GettextLocalizations> old) =>
true;
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: gettext_i18n
description: This package helps internationalizing a Flutter application.
version: 1.0.0
homepage:
version: 1.0.1
repository: https://github.com/frenchguych/gettext_i18n

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit a5883d9

Please sign in to comment.