Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve internationalization #3228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class DashboardView extends GetView<DashboardController> {
() => Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'DashboardView is working',
style: TextStyle(fontSize: 20),
Text(
"PAGES.DASHBOARD.TITLE".tr,
style: const TextStyle(fontSize: 20),
),
Text('Time: ${controller.now.value.toString()}'),
],
Expand Down
11 changes: 11 additions & 0 deletions example_nav2/lib/lang/languages/en_US.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Map<String, dynamic> get enUs => {
"PAGES": {
"DASHBOARD": {
"TITLE": "Dashboard",
"BUTTONS": {
"CHANGE_LANGUAGE": "Change language",
"GO_TO_HOME": "Go to home",
},
},
},
};
11 changes: 11 additions & 0 deletions example_nav2/lib/lang/languages/es_ES.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Map<String, dynamic> get esEs => {
"PAGES": {
"DASHBOARD": {
"TITLE": "Tablero mejorado",
"BUTTONS": {
"CHANGE_LANGUAGE": "Cambiar idioma",
"GO_TO_HOME": "Ir a inicio",
},
},
},
};
11 changes: 11 additions & 0 deletions example_nav2/lib/lang/languages/pt_BR.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Map<String, dynamic> get ptBr => {
"PAGES": {
"DASHBOARD": {
"TITLE": "Painel",
"BUTTONS": {
"CHANGE_LANGUAGE": "Mudar idioma",
"GO_TO_HOME": "Ir para casa",
},
},
},
};
19 changes: 19 additions & 0 deletions example_nav2/lib/lang/translation_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:ui';

import 'package:example_nav2/lang/languages/en_us.dart';
import 'package:example_nav2/lang/languages/es_es.dart';
import 'package:example_nav2/lang/languages/pt_br.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';


class TranslationService extends Translations {
static Locale? get locale => Get.deviceLocale;
static const fallbackLocale = Locale('en', 'US');
@override
Map<String, Map<String, dynamic>> get keys => {
'en_US': enUs,
'pt_BR': ptBr,
'es_ES': esEs,
};
}
3 changes: 3 additions & 0 deletions example_nav2/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:example_nav2/lang/translation_service.dart';
import 'package:example_nav2/services/auth_service.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
Expand All @@ -13,6 +14,8 @@ void main() {
],
getPages: AppPages.routes,
initialRoute: AppPages.initial,
translations: TranslationService(),
locale: TranslationService.locale,
// builder: (context, child) {
// return FutureBuilder<void>(
// key: ValueKey('initFuture'),
Expand Down
2 changes: 1 addition & 1 deletion lib/get_navigation/src/root/internacionalization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const List<String> rtlLanguages = <String>[
];

abstract class Translations {
Map<String, Map<String, String>> get keys;
Map<String, Map<String, dynamic>> get keys;
}
22 changes: 20 additions & 2 deletions lib/get_utils/src/extensions/internacionalization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ extension LocalesIntl on GetInterface {

Map<String, Map<String, String>> get translations => _intlHost.translations;

void addTranslations(Map<String, Map<String, String>> tr) {
translations.addAll(tr);
void addTranslations(Map<String, Map<String, dynamic>> tr) {
Map<String, Map<String, String>> translationsX = {};
tr.forEach((key, value) {
translationsX[key] = flattenMap(value);
});
translations.addAll(translationsX);
}

void clearTranslations() {
Expand All @@ -50,6 +54,20 @@ extension LocalesIntl on GetInterface {
}
});
}

Map<String, String> flattenMap(Map<String, dynamic> map,
[String prefix = '']) {
Map<String, String> flattenedMap = {};
map.forEach((key, value) {
if (value is Map) {
flattenedMap.addAll(flattenMap(value.cast<String, dynamic>(),
prefix.isEmpty ? key : '$prefix.$key'));
} else {
flattenedMap[prefix.isEmpty ? key : '$prefix.$key'] = value;
}
});
return flattenedMap;
}
}

extension Trans on String {
Expand Down