mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Implement translations system
This commit is contained in:
		@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user