Refactored Language file

This commit is contained in:
Harinandan 2022-01-28 22:08:57 +05:30
parent 578bf963f7
commit 95247bc5fb

View File

@ -1,62 +1,47 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
enum Language { enum AppLanguage {
english, english,
chinese, chinese,
italian, italian,
french, french,
} }
String stringFromLanguageName(Language language) { String stringFromLanguageName(AppLanguage language) {
switch (language) { switch (language) {
case Language.english: case AppLanguage.english:
return "en"; return "en";
case Language.chinese: case AppLanguage.chinese:
return "ch"; return "ch";
case Language.italian: case AppLanguage.italian:
return "it"; return "it";
case Language.french: case AppLanguage.french:
return "fr"; return "fr";
} }
} }
Language languageFromString(String name) { AppLanguage languageFromString(String name) {
Language language = Language.english; AppLanguage language = AppLanguage.english;
if (name == "ch") { if (name == "ch") {
language = Language.chinese; language = AppLanguage.chinese;
} else if (name == "it") { } else if (name == "it") {
language = Language.italian; language = AppLanguage.italian;
} else if (name == "fr") { } else if (name == "fr") {
language = Language.french; language = AppLanguage.french;
} }
return language; return language;
} }
Locale localeFromLanguageName(Language language) { Locale localeFromLanguageName(AppLanguage language) {
switch (language) { switch (language) {
case Language.english: case AppLanguage.english:
return const Locale('en'); return const Locale('en');
case Language.chinese: case AppLanguage.chinese:
return const Locale('zh', 'CN'); return const Locale('zh', 'CN');
case Language.italian: case AppLanguage.italian:
return const Locale('it', 'IT'); return const Locale('it', 'IT');
case Language.french: case AppLanguage.french:
return const Locale('fr', 'CA'); return const Locale('fr', 'CA');
} }
} }
class AppLanguage {
Locale locale;
//Default Constructor
AppLanguage({required this.locale});
factory AppLanguage.fromLanguage({required Language language}) {
return AppLanguage(locale: localeFromLanguageName(language));
}
factory AppLanguage.fromName({required String name}) {
return AppLanguage.fromLanguage(language: languageFromString(name));
}
}