feat: change language

This commit is contained in:
akhdanre 2025-05-07 22:26:49 +07:00
parent 7dc0994162
commit da6597f42b
6 changed files with 61 additions and 9 deletions

View File

@ -79,5 +79,8 @@
"quiz_description_label": "Short Description",
"quiz_subject_label": "Subject",
"make_quiz_public": "Make Quiz Public",
"save_quiz": "Save Quiz"
"save_quiz": "Save Quiz",
"select_language": "Select Language",
"change_language": "Change Language"
}

View File

@ -2,7 +2,7 @@
"greeting_time": "Selamat Siang",
"greeting_user": "Halo {user}",
"create_room": "Buat Ruangan",
"join_room": "Gabung Ruangan",
"join_room": "Gabung Ruang",
"create_quiz": "Buat Kuis",
"ready_new_challenge": "Siap untuk tantangan baru?",
"search_or_select_category": "Cari atau pilih berdasarkan kategori",
@ -79,5 +79,8 @@
"quiz_description_label": "Deskripsi Singkat",
"quiz_subject_label": "Mata Pelajaran",
"make_quiz_public": "Jadikan Kuis Publik",
"save_quiz": "Simpan Kuis"
"save_quiz": "Simpan Kuis",
"select_language": "Pilih Bahasa",
"change_language": "Ganti Bahasa"
}

View File

@ -1,6 +1,6 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get/get.dart';
import 'package:quiz_app/app/bindings/initial_bindings.dart';
import 'package:quiz_app/app/routes/app_pages.dart';
@ -10,11 +10,11 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
localizationsDelegates: context.localizationDelegates,
debugShowCheckedModeBanner: false,
supportedLocales: context.supportedLocales,
locale: context.locale,
title: 'Quiz App',
locale: Get.locale ?? context.locale, // 🔁 This ensures GetX reacts to locale changes
fallbackLocale: const Locale('en', 'US'),
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
initialBinding: InitialBindings(),
initialRoute: AppRoutes.splashScreen,
getPages: AppPages.routes,

View File

@ -1,3 +1,6 @@
import 'dart:ui';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:quiz_app/app/routes/app_pages.dart';
import 'package:quiz_app/core/utils/logger.dart';
@ -38,4 +41,10 @@ class ProfileController extends GetxController {
void editProfile() {
logC.i("Edit profile pressed");
}
void changeLanguage(BuildContext context, String languageCode, String countryCode) async {
final locale = Locale(languageCode, countryCode);
await context.setLocale(locale);
Get.updateLocale(locale);
}
}

View File

@ -37,6 +37,12 @@ class ProfileView extends GetView<ProfileController> {
controller.editProfile,
),
const SizedBox(height: 12),
_buildActionButton(
context.tr("change_language"),
Icons.language,
() => _showLanguageDialog(context),
),
const SizedBox(height: 12),
_buildActionButton(
context.tr("logout"),
Icons.logout,
@ -126,4 +132,34 @@ class ProfileView extends GetView<ProfileController> {
),
);
}
void _showLanguageDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(context.tr("select_language")),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: const Icon(Icons.language),
title: const Text("English"),
onTap: () {
controller.changeLanguage(context, 'en', 'US');
Navigator.of(context).pop();
},
),
ListTile(
leading: const Icon(Icons.language),
title: const Text("Bahasa Indonesia"),
onTap: () {
controller.changeLanguage(context, 'id', "ID");
Navigator.of(context).pop();
},
),
],
),
),
);
}
}

View File

@ -16,7 +16,6 @@ void main() {
]);
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp(
@ -24,9 +23,11 @@ void main() {
supportedLocales: [
Locale('en', 'US'),
Locale('id', 'ID'),
Locale('ms', 'MY'),
],
path: 'assets/translations',
fallbackLocale: Locale('en', 'US'),
useOnlyLangCode: false,
child: MyApp(),
),
);