-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from defint/namespace-file-loader
Namespace file loader
- Loading branch information
Showing
13 changed files
with
394 additions
and
33 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"title": "Namespace example" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
label: | ||
main: "Label from another namespace" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label": { | ||
"main": "Напис з іншого простору імен" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_i18n/flutter_i18n.dart'; | ||
import 'package:flutter_i18n/widgets/I18nText.dart'; | ||
import 'package:flutter_localizations/flutter_localizations.dart'; | ||
|
||
Future main() async { | ||
final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate( | ||
translationLoader: NamespaceFileTranslationLoader( | ||
namespaces: ["common", "home"], | ||
useCountryCode: false, | ||
fallbackDir: 'en', | ||
basePath: 'assets/i18n_namespace', | ||
forcedLocale: Locale('it')), | ||
); | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
runApp(MyApp(flutterI18nDelegate)); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
final FlutterI18nDelegate flutterI18nDelegate; | ||
|
||
MyApp(this.flutterI18nDelegate); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Flutter Demo', | ||
theme: ThemeData( | ||
primarySwatch: Colors.deepPurple, | ||
), | ||
home: MyHomePage(), | ||
localizationsDelegates: [ | ||
flutterI18nDelegate, | ||
GlobalMaterialLocalizations.delegate, | ||
GlobalWidgetsLocalizations.delegate | ||
], | ||
); | ||
} | ||
} | ||
|
||
class MyHomePage extends StatefulWidget { | ||
@override | ||
MyHomeState createState() => MyHomeState(); | ||
} | ||
|
||
class MyHomeState extends State<MyHomePage> { | ||
changeLanguage() async { | ||
final currentLang = FlutterI18n.currentLocale(context); | ||
final nextLang = | ||
currentLang.languageCode == 'ua' ? Locale('en') : Locale('ua'); | ||
await FlutterI18n.refresh(context, nextLang); | ||
setState(() {}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: | ||
AppBar(title: Text(FlutterI18n.translate(context, "common.title"))), | ||
body: Builder(builder: (BuildContext context) { | ||
return Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
I18nText("home.label.main", Text("")), | ||
RaisedButton( | ||
onPressed: () async { | ||
await changeLanguage(); | ||
}, | ||
child: Text("Change language")) | ||
], | ||
), | ||
); | ||
}), | ||
); | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter/services.dart' show rootBundle; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_i18n/loaders/file_translation_loader.dart'; | ||
import 'package:flutter_i18n/utils/message_printer.dart'; | ||
|
||
class NamespaceFileTranslationLoader extends FileTranslationLoader { | ||
final String fallbackDir; | ||
final String basePath; | ||
final bool useCountryCode; | ||
final Locale forcedLocale; | ||
final List<String> namespaces; | ||
AssetBundle assetBundle; | ||
|
||
Map<dynamic, dynamic> _decodedMap = {}; | ||
|
||
NamespaceFileTranslationLoader( | ||
{@required this.namespaces, | ||
this.fallbackDir = "en", | ||
this.basePath = "assets/flutter_i18n", | ||
this.useCountryCode = false, | ||
this.forcedLocale}) { | ||
assert(namespaces != null); | ||
assert(namespaces.length > 0); | ||
|
||
assetBundle = rootBundle; | ||
} | ||
|
||
Future<Map> load() async { | ||
this.locale = locale ?? await findCurrentLocale(); | ||
MessagePrinter.info("The current locale is ${this.locale}"); | ||
|
||
await Future.wait( | ||
namespaces.map((namespace) => _loadTranslation(namespace))); | ||
|
||
return _decodedMap; | ||
} | ||
|
||
Future<void> _loadTranslation(String namespace) async { | ||
_decodedMap[namespace] = Map(); | ||
|
||
try { | ||
_decodedMap[namespace] = | ||
await loadFile("${composeFileName()}/$namespace"); | ||
} catch (e) { | ||
MessagePrinter.debug('Error loading translation $e'); | ||
await _loadTranslationFallback(namespace); | ||
} | ||
} | ||
|
||
Future<void> _loadTranslationFallback(String namespace) async { | ||
try { | ||
_decodedMap[namespace] = await loadFile("$fallbackDir/$namespace"); | ||
} catch (e) { | ||
MessagePrinter.debug('Error loading translation fallback $e'); | ||
} | ||
} | ||
} |
Oops, something went wrong.