parent
a43ccca81f
commit
c53e74a50e
@ -0,0 +1,31 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart' show rootBundle;
|
||||||
|
|
||||||
|
class LanguageTranslation {
|
||||||
|
late Locale locale;
|
||||||
|
static Map<dynamic, dynamic> _localizedValues = {};
|
||||||
|
|
||||||
|
LanguageTranslation(this.locale) {
|
||||||
|
_localizedValues = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
static LanguageTranslation? of(BuildContext context) {
|
||||||
|
return Localizations.of<LanguageTranslation>(context, LanguageTranslation);
|
||||||
|
}
|
||||||
|
|
||||||
|
String text(String key) {
|
||||||
|
return _localizedValues[key] ?? '** $key not found';
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<LanguageTranslation> load(Locale locale) async {
|
||||||
|
LanguageTranslation translations = LanguageTranslation(locale);
|
||||||
|
String jsonContent =
|
||||||
|
await rootBundle.loadString("locales/i18n_${locale.languageCode}.json");
|
||||||
|
_localizedValues = json.decode(jsonContent);
|
||||||
|
return translations;
|
||||||
|
}
|
||||||
|
|
||||||
|
get currentLanguage => locale.languageCode;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'translation.dart';
|
||||||
|
|
||||||
|
class TranslationsDelegate extends LocalizationsDelegate<LanguageTranslation> {
|
||||||
|
const TranslationsDelegate();
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool isSupported(Locale locale) => ['en', 'ar'].contains(locale.languageCode);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<LanguageTranslation> load(Locale locale) =>
|
||||||
|
LanguageTranslation.load(locale);
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool shouldReload(TranslationsDelegate old) => false;
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../../Classes/localization/translation.dart';
|
||||||
|
|
||||||
|
languageSelector(BuildContext context) {
|
||||||
|
// set up the button
|
||||||
|
|
||||||
|
// set up the AlertDialog
|
||||||
|
AlertDialog alert = AlertDialog(
|
||||||
|
title: const Text("Choisissez une langue"),
|
||||||
|
actions: [
|
||||||
|
InkWell(
|
||||||
|
onTap: () {
|
||||||
|
LanguageTranslation.load(const Locale("fr"));
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text("FR"),
|
||||||
|
),
|
||||||
|
InkWell(
|
||||||
|
onTap: () {
|
||||||
|
LanguageTranslation.load(const Locale("en"));
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text("EN"),
|
||||||
|
),
|
||||||
|
InkWell(
|
||||||
|
onTap: () {
|
||||||
|
LanguageTranslation.load(const Locale("pt"));
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text("PT"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
// show the dialog
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return alert;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"home_title" : "Home",
|
||||||
|
"search" : "search"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"home_title" : "Accueil",
|
||||||
|
"search" : "rechercher"
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"home_title" : "Inicio",
|
||||||
|
"search" : "busca"
|
||||||
|
}
|
Loading…
Reference in new issue