feat: native language names for locale picker

This commit is contained in:
Aliaksei Tratseuski 2024-05-18 02:45:05 +04:00
parent c70edb957d
commit 0ee46e1c1e
4 changed files with 66 additions and 28 deletions

View file

@ -63,7 +63,7 @@
"change_application_theme": "Ihr Anwendungsdesign wechseln", "change_application_theme": "Ihr Anwendungsdesign wechseln",
"dangerous_settings": "Gefährliche Einstellungen", "dangerous_settings": "Gefährliche Einstellungen",
"reset_config_title": "Anwendungseinstellungen zurücksetzen", "reset_config_title": "Anwendungseinstellungen zurücksetzen",
"reset_config_description": "API Sclüssel und root Benutzer zurücksetzen.", "reset_config_description": "API Sclüssel und root Benutzer zurücksetzen."
}, },
"ssh": { "ssh": {
"title": "SSH Schlüssel", "title": "SSH Schlüssel",

View file

@ -7,11 +7,9 @@ class Localization extends StatelessWidget {
super.key, super.key,
}); });
final Widget child; // when adding new locale, add corresponding native language name to mapper
// below
@override static const supportedLocales = [
Widget build(final BuildContext context) => EasyLocalization(
supportedLocales: const [
Locale('ar'), Locale('ar'),
Locale('az'), Locale('az'),
Locale('be'), Locale('be'),
@ -32,7 +30,40 @@ class Localization extends StatelessWidget {
Locale('th'), Locale('th'),
Locale('uk'), Locale('uk'),
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'),
], ];
// https://en.wikipedia.org/wiki/IETF_language_tag#List_of_common_primary_language_subtags
static final _languageNames = {
const Locale('ar'): 'العربية',
const Locale('az'): 'Azərbaycan',
const Locale('be'): 'беларуская',
const Locale('cs'): 'čeština',
const Locale('de'): 'Deutsch',
const Locale('en'): 'English',
const Locale('es'): 'español',
const Locale('et'): 'eesti',
const Locale('fr'): 'français',
const Locale('he'): 'עברית',
const Locale('kk'): 'Қазақша',
const Locale('lv'): 'latviešu',
const Locale('mk'): 'македонски јазик',
const Locale('pl'): 'polski',
const Locale('ru'): 'русский',
const Locale('sk'): 'slovenčina',
const Locale('sl'): 'slovenski',
const Locale('th'): 'ไทย',
const Locale('uk'): 'Українська',
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'): '中文',
};
static String getLanguageName(final Locale locale) =>
_languageNames[locale] ?? locale.languageCode;
final Widget child;
@override
Widget build(final BuildContext context) => EasyLocalization(
supportedLocales: supportedLocales,
path: 'assets/translations', path: 'assets/translations',
fallbackLocale: const Locale('en'), fallbackLocale: const Locale('en'),
useFallbackTranslations: true, useFallbackTranslations: true,

View file

@ -3,6 +3,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:selfprivacy/config/app_controller/inherited_app_controller.dart'; import 'package:selfprivacy/config/app_controller/inherited_app_controller.dart';
import 'package:selfprivacy/config/localization.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart'; import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/ui/components/buttons/dialog_action_button.dart'; import 'package:selfprivacy/ui/components/buttons/dialog_action_button.dart';
import 'package:selfprivacy/ui/layouts/brand_hero_screen.dart'; import 'package:selfprivacy/ui/layouts/brand_hero_screen.dart';

View file

@ -12,7 +12,7 @@ class _LanguagePicker extends StatelessWidget {
trailing: Padding( trailing: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0), padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text( child: Text(
context.locale.toString(), Localization.getLanguageName(context.locale),
style: Theme.of(context).textTheme.headlineSmall, style: Theme.of(context).textTheme.headlineSmall,
), ),
), ),
@ -42,12 +42,18 @@ class _LanguagePickerDialog extends StatelessWidget {
for (final locale for (final locale
in InheritedAppController.of(context).supportedLocales) in InheritedAppController.of(context).supportedLocales)
ListTile( ListTile(
// TODO: add locale to language name matcher title: Text(
title: Text(locale.toString()), Localization.getLanguageName(locale),
style: TextStyle(
fontWeight: locale == context.locale
? FontWeight.w800
: FontWeight.w400,
),
),
onTap: () { onTap: () {
Navigator.of(context).pop(locale); Navigator.of(context).pop(locale);
}, },
) ),
], ],
); );
} }