From 6a72f4c41fb896e9ab6ef5c42304a615d5e8d9bc Mon Sep 17 00:00:00 2001 From: SpiralArm Consulting Ltd Date: Mon, 4 Feb 2019 16:12:10 +0000 Subject: [PATCH] updates for new release --- CHANGELOG.md | 4 ++ example/lib/main.dart | 109 +++++++++++++++++----------------------- lib/polylingual.dart | 2 +- lib/src/translator.dart | 52 +++++++++---------- pubspec.yaml | 4 +- 5 files changed, 78 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9668c6..b3968bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [0.0.2] - 4 Feb 2019. + +* Updated documents and app description. + ## [0.0.1] - 4 Feb 2019. * Initial release. diff --git a/example/lib/main.dart b/example/lib/main.dart index 70f52e8..290f38b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -4,106 +4,91 @@ import 'package:polylingual/polylingual.dart'; /// Load the First Widget and initialise PolyLingual void main() async { - // set our required default(fallback) language and initialise PolyLingual PolyLingual.defaultLanguageCode = "es"; await PolyLingual.initialise("res/strings.json"); // run your app here runApp(DemoApp()); - } /// Simple Demo of using PolyLingual to set up app translations /// and then update the translation file -/// +/// /// for the demo we support 2 languages English and Spanish class DemoApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'DemoApp', - home: LanguageScreen(), - localizationsDelegates: [ + title: 'DemoApp', + home: LanguageScreen(), + localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], - supportedLocales: [ - const Locale("es"), - const Locale("en"), - ], - - ); + supportedLocales: [ + const Locale("es"), + const Locale("en"), + ], + ); } } - // Simple Screen Widget to show the use of PolyLingual class LanguageScreen extends StatefulWidget { @override _LanguageScreenState createState() => _LanguageScreenState(); } -class _LanguageScreenState extends State{ +class _LanguageScreenState extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar(title: Text("PolyLingual Demo")), - body: ListView( - padding: EdgeInsets.all(30.0), - children: [ - - Text( - PolyLingual.of(context).string("greeting"), - style: TextStyle(color: Colors.blue), - ), - - Text( - PolyLingual.of(context).string("good"), - style: TextStyle(color: Colors.orange), - ), - - Text( - PolyLingual.of(context).string("thankyou"), - style: TextStyle(color: Colors.blue), - ), - - Text( - PolyLingual.of(context).string("welcome"), - style: TextStyle(color: Colors.orange), - ), - - RaisedButton( - - /// The example uses a defined map to update translations, but this could also come from a downloaded file - onPressed: () async { - Map update = { - "en": { + appBar: AppBar(title: Text("PolyLingual Demo")), + body: ListView( + padding: EdgeInsets.all(30.0), + children: [ + Text( + PolyLingual.of(context).string("greeting"), + style: TextStyle(color: Colors.blue), + ), + Text( + PolyLingual.of(context).string("good"), + style: TextStyle(color: Colors.orange), + ), + Text( + PolyLingual.of(context).string("thankyou"), + style: TextStyle(color: Colors.blue), + ), + Text( + PolyLingual.of(context).string("welcome"), + style: TextStyle(color: Colors.orange), + ), + RaisedButton( + /// The example uses a defined map to update translations, but this could also come from a downloaded file + onPressed: () async { + Map update = { + "en": { "greeting": "Hi, how are you?", "welcome": "Thanks very much", "thankyou": "cheers mate!", "good": "Not So bad", "about": "Information" - - }, - "es": { + }, + "es": { "greeting": "¿Hola como estas?", "welcome": "de nada amiga", "thankyou": "muchas gracias", "good": "muy bien", "about": "Información" - - } - }; - await PolyLingual.updateLanguageStrings(update); - setState(() {}); - }, - child: Text("Update EN translation data"), - ), - - - ], - ) - ); + } + }; + await PolyLingual.updateLanguageStrings(update); + setState(() {}); + }, + child: Text("Update EN translation data"), + ), + ], + )); } -} \ No newline at end of file +} diff --git a/lib/polylingual.dart b/lib/polylingual.dart index 41bf8d1..07304ce 100644 --- a/lib/polylingual.dart +++ b/lib/polylingual.dart @@ -4,7 +4,7 @@ library polylingual; /// [PolyLingual] is a plug-in that can be used to provide multiple language translation strings for an app /// It is a static class with the following methods -/// +/// /// [initialise] /// [of] /// [string] diff --git a/lib/src/translator.dart b/lib/src/translator.dart index 284633c..d1ee4f9 100644 --- a/lib/src/translator.dart +++ b/lib/src/translator.dart @@ -5,17 +5,16 @@ import 'package:path_provider/path_provider.dart'; import 'package:flutter/services.dart' show rootBundle; /// PolyLingual is an easy way of adding translation strings to your app -/// It detects the current phone language and provides a mechanism to return +/// It detects the current phone language and provides a mechanism to return /// a localised string for that language, if available. /// Otherwise it will return the string in the selected default. -/// -/// [defautLanguage] is used as the fallback language if no translation is provided. +/// +/// [defautLanguage] is used as the fallback language if no translation is provided. /// The default is 'en' but this can be set as required. class PolyLingual { - Locale locale; - static Map _values; - static String defaultLanguageCode="en"; + static Map _values; + static String defaultLanguageCode = "en"; PolyLingual(this.locale); @@ -24,47 +23,47 @@ class PolyLingual { return PolyLingual(Localizations.localeOf(context)); } - /// Get the required string in the current phone language + /// Get the required string in the current phone language /// [id] is the key value of the required string from the resource file String string(id) { String result; - try{ - result = _values[locale.languageCode][id]; - }catch(e) { - result = _values[defaultLanguageCode][id]; + try { + result = _values[locale.languageCode][id]; + } catch (e) { + result = _values[defaultLanguageCode][id]; } return result; } /// This will initialise [PolyLingual] by loading in a user defined language translation map /// This method should always be run first, usually in the main() function (see the example app for details), so that the strings are available to the rest of the app - /// + /// /// The translation map comes either from the latest stored data OR from the specified initial asset map if there is no stored data /// This way the app can have seed translations built in, that can be updated without re-building or re-issung the app (as long as there are no new strings) - /// + /// /// [filename] is the default name of the file. This *MUST* exist and be declared as an asset in pubspec.yaml. This data is used to seed the PolyLingual translation map. - /// - /// for example: + /// + /// for example: /// assets: /// - res/strings.json static initialise(String filename) async { - var map = await _loadTranslationMapAsString(); - if(map != null){ + var map = await _loadTranslationMapAsString(); + if (map != null) { _values = jsonDecode(map); - } else { + } else { var strings = await rootBundle.loadString(filename); await updateLanguageStrings(jsonDecode(strings)); } } - /// Update the PolyLingual translation map + /// Update the PolyLingual translation map /// This method can be used to correct and update the currently stored translation map - /// + /// /// [data] is the Translation Map similar to the asset supplied original. - /// + /// static updateLanguageStrings(Map data) async { - await _saveTranslationMap(jsonEncode(data)); - _values = data; + await _saveTranslationMap(jsonEncode(data)); + _values = data; } /// Store the supplied data as the current Translation Map @@ -79,12 +78,9 @@ class PolyLingual { String result; final directory = await getApplicationDocumentsDirectory(); File tmap = File("${directory.path}/translator.json"); - if(tmap.existsSync()){ + if (tmap.existsSync()) { result = tmap.readAsStringSync(); } return result; } - - - -} \ No newline at end of file +} diff --git a/pubspec.yaml b/pubspec.yaml index 6278f92..a6749e1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: polylingual -description: A new Flutter package project. -version: 0.0.1 +description: PolyLingual is a plug-in that provides a means of storing and updating String translations for a Flutter app via a simple JSON file. +version: 0.0.2 author: Steve Rogers homepage: https://github.com/magnatronus/polylingual-package