1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-25 12:28:50 +00:00

Implement translations system

This commit is contained in:
Pierre HUBERT 2020-03-28 17:11:11 +01:00
parent 96a3c05497
commit 8655d20234
5 changed files with 55 additions and 3 deletions

3
assets/langs.json Normal file
View File

@ -0,0 +1,3 @@
{
"fr_FR": "fr"
}

View File

@ -3,6 +3,7 @@ import 'package:comunic/helpers/database/database_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/routes/home_route.dart';
import 'package:comunic/ui/routes/login_route.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
@ -17,6 +18,9 @@ void subMain() async {
// Connect to database
await DatabaseHelper.open();
// Get current system language
await initTranslations();
runApp(ComunicApplication(
darkMode: (await PreferencesHelper.getInstance())
.getBool(PreferencesKeyList.ENABLE_DARK_THEME),

View File

@ -1,7 +1,32 @@
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:intl/intl_standalone.dart';
/// Internationalization utilities
///
/// @author Pierre HUBERT
String _currLang;
Map<String, Map<String, String>> translations;
/// Initialize translations system
///
/// This method currently fetch the current user language
Future<void> initTranslations() async {
_currLang = await findSystemLocale();
// Load the list of translations
final list = Map<String, String>.from(
jsonDecode(await rootBundle.loadString("assets/langs.json")));
translations = new Map();
for (final entry in list.entries) {
translations[entry.key] = Map<String, String>.from(jsonDecode(
await rootBundle.loadString("assets/langs/${entry.value}.json")));
}
}
/// Translate a string
///
/// Translate a given [string] into the current language, if available
@ -9,10 +34,14 @@
/// Then apply the list of [args] to the string, each argument name is
/// surrounded by '%'
String tr(String string, {Map<String, String> args}) {
//TODO : create translation system
// Check if a translation is available
if (_currLang != null &&
translations.containsKey(_currLang) &&
translations[_currLang].containsKey(string))
string = translations[_currLang][string];
//Apply arguments
if(args != null)
if (args != null)
args.forEach((key, value) => string = string.replaceAll("%$key%", value));
return string;

View File

@ -149,6 +149,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3+4"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
matcher:
dependency: transitive
description:

View File

@ -48,6 +48,9 @@ dependencies:
# Module that display the charts for the surveys
pie_chart: ^3.1.1
# Get current system language
intl: ^0.16.1
dev_dependencies:
flutter_test:
sdk: flutter
@ -65,7 +68,13 @@ flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
assets:
# Translations
- assets/langs.json
- assets/langs/
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg