Skip to content

Commit

Permalink
updates for new release
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiralArm Consulting Ltd committed Feb 4, 2019
1 parent 45eb407 commit 6a72f4c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 93 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.0.2] - 4 Feb 2019.

* Updated documents and app description.

## [0.0.1] - 4 Feb 2019.

* Initial release.
109 changes: 47 additions & 62 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LanguageScreen>{
class _LanguageScreenState extends State<LanguageScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("PolyLingual Demo")),
body: ListView(
padding: EdgeInsets.all(30.0),
children: <Widget>[

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: <Widget>[
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"),
),
],
));
}
}
}
2 changes: 1 addition & 1 deletion lib/polylingual.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
52 changes: 24 additions & 28 deletions lib/src/translator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dynamic, dynamic> _values;
static String defaultLanguageCode="en";
static Map<dynamic, dynamic> _values;
static String defaultLanguageCode = "en";

PolyLingual(this.locale);

Expand All @@ -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
Expand All @@ -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;
}



}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/magnatronus/polylingual-package

Expand Down

0 comments on commit 6a72f4c

Please sign in to comment.