mirror of
https://github.com/inventree/inventree-app.git
synced 2025-06-12 02:05:29 +00:00
Locale switch (#200)
* Add function to set app locale * Setting for selecting app language - Adds requirement for "flutter_localized_locales" - Change main app to stateless * Reload entire app tree when language is changed * Update release notes * linting
This commit is contained in:
@ -1,8 +1,12 @@
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||
import "package:flutter_localized_locales/flutter_localized_locales.dart";
|
||||
|
||||
import "package:inventree/api_form.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/l10n/supported_locales.dart";
|
||||
import "package:inventree/main.dart";
|
||||
import "package:inventree/preferences.dart";
|
||||
|
||||
|
||||
@ -27,6 +31,8 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
|
||||
bool reportErrors = true;
|
||||
bool strictHttps = false;
|
||||
|
||||
Locale? locale;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@ -46,14 +52,78 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
|
||||
reportErrors = await InvenTreeSettingsManager().getValue(INV_REPORT_ERRORS, true) as bool;
|
||||
strictHttps = await InvenTreeSettingsManager().getValue(INV_STRICT_HTTPS, false) as bool;
|
||||
|
||||
locale = await InvenTreeSettingsManager().getSelectedLocale();
|
||||
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _selectLocale(BuildContext context) async {
|
||||
|
||||
List<Map<String, dynamic>> options = [
|
||||
{
|
||||
"display_name": L10().languageDefault,
|
||||
"value": null,
|
||||
}
|
||||
];
|
||||
|
||||
// Construct a list of available locales
|
||||
for (var locale in supported_locales) {
|
||||
options.add({
|
||||
"display_name": LocaleNames.of(context)!.nameOf(locale.toString()),
|
||||
"value": locale.toString()
|
||||
});
|
||||
}
|
||||
|
||||
Map<String, dynamic> fields = {
|
||||
"locale": {
|
||||
"label": L10().language,
|
||||
"type": "choice",
|
||||
"choices": options,
|
||||
"value": locale?.toString(),
|
||||
}
|
||||
};
|
||||
|
||||
launchApiForm(
|
||||
context,
|
||||
L10().languageSelect,
|
||||
"",
|
||||
fields,
|
||||
icon: FontAwesomeIcons.checkCircle,
|
||||
onSuccess: (Map<String, dynamic> data) async {
|
||||
|
||||
String locale_name = (data["locale"] ?? "") as String;
|
||||
Locale? selected_locale;
|
||||
|
||||
for (var locale in supported_locales) {
|
||||
if (locale.toString() == locale_name) {
|
||||
selected_locale = locale;
|
||||
}
|
||||
}
|
||||
|
||||
await InvenTreeSettingsManager().setSelectedLocale(selected_locale);
|
||||
|
||||
setState(() {
|
||||
locale = selected_locale;
|
||||
});
|
||||
|
||||
// Refresh the entire app locale
|
||||
InvenTreeApp.of(context)?.setLocale(locale);
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
String languageName = L10().languageDefault;
|
||||
|
||||
if (locale != null) {
|
||||
languageName = LocaleNames.of(context)!.nameOf(locale.toString()) ?? L10().languageDefault;
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
key: _settingsKey,
|
||||
appBar: AppBar(
|
||||
@ -142,6 +212,14 @@ class _InvenTreeAppSettingsState extends State<InvenTreeAppSettingsWidget> {
|
||||
},
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
title: Text(L10().language),
|
||||
subtitle: Text(languageName),
|
||||
leading: FaIcon(FontAwesomeIcons.language),
|
||||
onTap: () async {
|
||||
_selectLocale(context);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text(L10().errorReportUpload),
|
||||
subtitle: Text(L10().errorReportUploadDetails),
|
||||
|
Reference in New Issue
Block a user