From 4ee1611ac61caddb902fcb76d173af256828fd33 Mon Sep 17 00:00:00 2001 From: PouriaMoradi021 Date: Thu, 12 Dec 2024 16:29:43 +0330 Subject: [PATCH] feat: add easy localization package to project #38 - add en/es/fr.json translation files - generate codegen_loader.g & locale_keys.g files - update pubspec.yaml file - update text variables - add `build_runner.sh` file - update `app theme data` - update test files --- assets/translations/en.json | 6 ++ assets/translations/es.json | 6 ++ assets/translations/fr.json | 6 ++ build_runner.sh | 62 +++++++++++++++++++ lib/main.dart | 4 +- .../core/common/widgets/theme_switcher.dart | 2 +- lib/src/core/common/widgets/toolbar_logo.dart | 2 +- .../utils/{ => gen}/assets/assets.gen.dart | 17 +++++ .../utils/{ => gen}/assets/fonts.gen.dart | 0 .../gen/localization/codegen_loader.g.dart | 37 +++++++++++ .../utils/gen/localization/locale_keys.g.dart | 9 +++ .../main/theme/theme_data/app_theme_data.dart | 2 +- .../presentation/screen/home_page.dart | 7 ++- macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.yaml | 3 + .../common/widgets/theme_switcher_test.dart | 2 +- .../widgets/toolbar_logo_widget_test.dart | 2 +- 17 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 assets/translations/en.json create mode 100644 assets/translations/es.json create mode 100644 assets/translations/fr.json create mode 100644 build_runner.sh rename lib/src/core/utils/{ => gen}/assets/assets.gen.dart (88%) rename lib/src/core/utils/{ => gen}/assets/fonts.gen.dart (100%) create mode 100644 lib/src/core/utils/gen/localization/codegen_loader.g.dart create mode 100644 lib/src/core/utils/gen/localization/locale_keys.g.dart diff --git a/assets/translations/en.json b/assets/translations/en.json new file mode 100644 index 0000000..7b18e4c --- /dev/null +++ b/assets/translations/en.json @@ -0,0 +1,6 @@ +{ + "title": "Hello, World!", + "subtitle": "Welcome to Localization", + "description": "You have pushed the button this many times:", + "switchLanguage": "Switch language" +} \ No newline at end of file diff --git a/assets/translations/es.json b/assets/translations/es.json new file mode 100644 index 0000000..0e721ee --- /dev/null +++ b/assets/translations/es.json @@ -0,0 +1,6 @@ +{ + "title": "¡Hola, Mundo!", + "subtitle": "Bienvenido a Localización", + "description": "Has pulsado el botón tantas veces:", + "switchLanguage": "Cambiar idioma" +} \ No newline at end of file diff --git a/assets/translations/fr.json b/assets/translations/fr.json new file mode 100644 index 0000000..1bfb3d8 --- /dev/null +++ b/assets/translations/fr.json @@ -0,0 +1,6 @@ +{ + "title": "Bonjour", + "subtitle": "Bienvenue dans Localisation", + "description": "Vous avez appuyé sur le bouton autant de fois :", + "switchLanguage": "Changer de langue" +} \ No newline at end of file diff --git a/build_runner.sh b/build_runner.sh new file mode 100644 index 0000000..3f69600 --- /dev/null +++ b/build_runner.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Welcome message +echo "Welcome to the Build Runner Wizard!" + +# Display options to the user +echo "Please select an option:" +echo "1. Automatic Execution" +echo "2. Manual Execution" +echo "3. Re-Build Translation Files" +echo "4. Re-Generate Asset Files" + +# Read user input +read -p "Enter your choice (1, 2, 3, or 4): " choice + +# Check selected option and execute corresponding commands +case $choice in + 1) + echo "* You selected Automatic Execution." + # Automatic execution + + # Rebuild translation files + dart run easy_localization:generate --source-dir=assets/translations --output-dir=lib/src/core/utils/gen/localization + dart run easy_localization:generate -f keys -o locale_keys.g.dart --source-dir=assets/translations --output-dir=lib/src/core/utils/gen/localization + dart run build_runner build --delete-conflicting-outputs --build-filter="lib\src\core\constants\localization\*.dart" + + # Execute automatic build runner command + dart run build_runner build --delete-conflicting-outputs + ;; + 2) + echo "* You selected Manual Execution." + # Read user input text + read -p "Enter your text (comma-separated list for multiple items): " text + + # Convert input text to an array using ',' as a delimiter + IFS=',' read -r -a text_array <<< "$text" + + # Loop through each item in the array and execute the command + for item in "${text_array[@]}" + do + # Replace '/' with '\' in the input text + text_with_backslashes=$(echo "$item" | sed 's/\//\\/g') + # Replace the desired text in the command and execute it + dart run build_runner build --delete-conflicting-outputs --build-filter="$text_with_backslashes\*.dart" + done + ;; + 3) + echo "* You selected Re-Build Translation Files." + # Execute commands for rebuilding translation files + dart run easy_localization:generate --source-dir=assets/translations --output-dir=lib/src/core/utils/gen/localization + dart run easy_localization:generate -f keys -o locale_keys.g.dart --source-dir=assets/translations --output-dir=lib/src/core/utils/gen/localization + dart run build_runner build --delete-conflicting-outputs --build-filter="lib\src\core\constants\localization\*.dart" + ;; + 4) + echo "* You selected Re-Generate Asset Files." + # Execute commands for rebuilding asset files + dart run build_runner build --delete-conflicting-outputs --build-filter="lib\src\core\utils\gen\assets\*.dart" + ;; + *) + echo "* Invalid choice. Please enter 1, 2, 3, or 4." + ;; +esac diff --git a/lib/main.dart b/lib/main.dart index fe2a75c..2614d40 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -6,7 +7,8 @@ import 'package:gui/src/features/main/theme/bloc/theme_bloc.dart'; import 'src/features/main/language/presentation/bloc/language_bloc.dart'; import 'src/features/splash_screen/presentation/screen/home_page.dart'; -void main() { +void main() async { + await EasyLocalization.ensureInitialized(); runApp( MultiBlocProvider( providers: [ diff --git a/lib/src/core/common/widgets/theme_switcher.dart b/lib/src/core/common/widgets/theme_switcher.dart index 98c452e..c94ff74 100644 --- a/lib/src/core/common/widgets/theme_switcher.dart +++ b/lib/src/core/common/widgets/theme_switcher.dart @@ -3,7 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:gui/src/core/common/colors/app_colors.dart'; import 'package:gui/src/core/enums/theme_modes.dart'; -import 'package:gui/src/core/utils/assets/assets.gen.dart'; +import 'package:gui/src/core/utils/gen/assets/assets.gen.dart'; import 'package:gui/src/features/main/theme/bloc/theme_bloc.dart'; /// ### [ThemeSwitcher] Documentation diff --git a/lib/src/core/common/widgets/toolbar_logo.dart b/lib/src/core/common/widgets/toolbar_logo.dart index e61c255..60a00f0 100644 --- a/lib/src/core/common/widgets/toolbar_logo.dart +++ b/lib/src/core/common/widgets/toolbar_logo.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; -import 'package:gui/src/core/utils/assets/assets.gen.dart'; +import 'package:gui/src/core/utils/gen/assets/assets.gen.dart'; /// # [ToolbarLogo] Documentation /// A stateless widget that displays the app's toolbar logo. diff --git a/lib/src/core/utils/assets/assets.gen.dart b/lib/src/core/utils/gen/assets/assets.gen.dart similarity index 88% rename from lib/src/core/utils/assets/assets.gen.dart rename to lib/src/core/utils/gen/assets/assets.gen.dart index c0636b9..fe09c2e 100644 --- a/lib/src/core/utils/assets/assets.gen.dart +++ b/lib/src/core/utils/gen/assets/assets.gen.dart @@ -59,11 +59,28 @@ class $AssetsImagesGen { List get values => [gears, gift, masterPassword, splash]; } +class $AssetsTranslationsGen { + const $AssetsTranslationsGen(); + + /// File path: assets/translations/en.json + String get en => 'assets/translations/en.json'; + + /// File path: assets/translations/es.json + String get es => 'assets/translations/es.json'; + + /// File path: assets/translations/fr.json + String get fr => 'assets/translations/fr.json'; + + /// List of all assets + List get values => [en, es, fr]; +} + class Assets { Assets._(); static const $AssetsIconsGen icons = $AssetsIconsGen(); static const $AssetsImagesGen images = $AssetsImagesGen(); + static const $AssetsTranslationsGen translations = $AssetsTranslationsGen(); } class AssetGenImage { diff --git a/lib/src/core/utils/assets/fonts.gen.dart b/lib/src/core/utils/gen/assets/fonts.gen.dart similarity index 100% rename from lib/src/core/utils/assets/fonts.gen.dart rename to lib/src/core/utils/gen/assets/fonts.gen.dart diff --git a/lib/src/core/utils/gen/localization/codegen_loader.g.dart b/lib/src/core/utils/gen/localization/codegen_loader.g.dart new file mode 100644 index 0000000..b167421 --- /dev/null +++ b/lib/src/core/utils/gen/localization/codegen_loader.g.dart @@ -0,0 +1,37 @@ +// DO NOT EDIT. This is code generated via package:easy_localization/generate.dart + +// ignore_for_file: prefer_single_quotes, avoid_renaming_method_parameters + +import 'dart:ui'; + +import 'package:easy_localization/easy_localization.dart' show AssetLoader; + +class CodegenLoader extends AssetLoader{ + const CodegenLoader(); + + @override + Future?> load(String path, Locale locale) { + return Future.value(mapLocales[locale.toString()]); + } + + static const Map en = { + "title": "Hello, World!", + "subtitle": "Welcome to Localization", + "description": "You have pushed the button this many times:", + "switchLanguage": "Switch language", +}; +static const Map es = { + "title": "¡Hola, Mundo!", + "subtitle": "Bienvenido a Localización", + "description": "Has pulsado el botón tantas veces:", + "switchLanguage": "Cambiar idioma", +}; +static const Map fr = { + "title": "Bonjour", + "subtitle": "Bienvenue dans Localisation", + "description": "Vous avez appuyé sur le bouton autant de fois :", + "switchLanguage": "Changer de langue", +}; +static const Map> mapLocales = {"en": en, "es": es, + "fr": fr,}; +} diff --git a/lib/src/core/utils/gen/localization/locale_keys.g.dart b/lib/src/core/utils/gen/localization/locale_keys.g.dart new file mode 100644 index 0000000..fc99f86 --- /dev/null +++ b/lib/src/core/utils/gen/localization/locale_keys.g.dart @@ -0,0 +1,9 @@ +// DO NOT EDIT. This is code generated via package:easy_localization/generate.dart + +abstract class LocaleKeys { + static const title = 'title'; + static const subtitle = 'subtitle'; + static const description = 'description'; + static const switchLanguage = 'switchLanguage'; + +} diff --git a/lib/src/features/main/theme/theme_data/app_theme_data.dart b/lib/src/features/main/theme/theme_data/app_theme_data.dart index f704767..acd4b36 100644 --- a/lib/src/features/main/theme/theme_data/app_theme_data.dart +++ b/lib/src/features/main/theme/theme_data/app_theme_data.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:gui/src/core/enums/theme_modes.dart'; -import 'package:gui/src/core/utils/assets/fonts.gen.dart'; +import 'package:gui/src/core/utils/gen/assets/fonts.gen.dart'; import 'package:gui/src/features/main/theme/theme_data/pallets/on_surface_pallet.dart'; import 'package:gui/src/features/main/theme/theme_data/pallets/surface_pallet.dart'; diff --git a/lib/src/features/splash_screen/presentation/screen/home_page.dart b/lib/src/features/splash_screen/presentation/screen/home_page.dart index acde13e..6c7fdd8 100644 --- a/lib/src/features/splash_screen/presentation/screen/home_page.dart +++ b/lib/src/features/splash_screen/presentation/screen/home_page.dart @@ -1,7 +1,8 @@ +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:gui/src/core/common/widgets/theme_switcher.dart'; import 'package:gui/src/core/common/widgets/toolbar_logo.dart'; +import 'package:gui/src/core/utils/gen/localization/locale_keys.g.dart'; import 'package:gui/src/features/main/language/presentation/widget/language_widget.dart'; import 'package:gui/src/features/main/theme/presentation/widgets/theme_selector.dart'; import 'package:gui/src/features/main/theme/theme_data/pallets/on_surface_pallet.dart'; @@ -27,14 +28,14 @@ class _MyHomePageState extends State { appBar: AppBar( actions: [ToolbarLogo(), ThemeSwitcher()], backgroundColor: theme.colorScheme.inversePrimary, - title: Text(AppLocalizations.of(context)!.title), + title: Text(LocaleKeys.title.tr()), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( - AppLocalizations.of(context)!.subtitle, + LocaleKeys.subtitle.tr(), style: theme.textTheme.titleMedium!.copyWith( color: theme.extension()!.onSurface3, ), diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817..724bb2a 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.yaml b/pubspec.yaml index 8fb1a08..0bd0d0e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ dependencies: bloc: ^8.1.0 bloc_test: ^9.1.7 cupertino_icons: ^1.0.8 + easy_localization: ^3.0.7 equatable: ^2.0.7 flutter: sdk: flutter @@ -26,6 +27,7 @@ dependencies: dev_dependencies: build_runner: + easy_localization_generator: ^0.3.3 flutter_lints: ^5.0.0 flutter_test: sdk: flutter @@ -42,6 +44,7 @@ flutter: assets: - assets/images/ - assets/icons/ + - assets/translations/ fonts: - family: Inter fonts: diff --git a/test/src/core/common/widgets/theme_switcher_test.dart b/test/src/core/common/widgets/theme_switcher_test.dart index 845a4c2..7c9c313 100644 --- a/test/src/core/common/widgets/theme_switcher_test.dart +++ b/test/src/core/common/widgets/theme_switcher_test.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_svg/svg.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:gui/src/core/common/widgets/theme_switcher.dart'; -import 'package:gui/src/core/utils/assets/assets.gen.dart'; +import 'package:gui/src/core/utils/gen/assets/assets.gen.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); diff --git a/test/src/core/common/widgets/toolbar_logo_widget_test.dart b/test/src/core/common/widgets/toolbar_logo_widget_test.dart index 10c6b60..ca7381d 100644 --- a/test/src/core/common/widgets/toolbar_logo_widget_test.dart +++ b/test/src/core/common/widgets/toolbar_logo_widget_test.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_svg/svg.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:gui/src/core/common/widgets/toolbar_logo.dart'; -import 'package:gui/src/core/utils/assets/assets.gen.dart'; +import 'package:gui/src/core/utils/gen/assets/assets.gen.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized();