Skip to content

Commit

Permalink
feat: add easy localization package to project #38
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
PouriaMoradi021 committed Dec 12, 2024
1 parent f551393 commit 4ee1611
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 9 deletions.
6 changes: 6 additions & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "Hello, World!",
"subtitle": "Welcome to Localization",
"description": "You have pushed the button this many times:",
"switchLanguage": "Switch language"
}
6 changes: 6 additions & 0 deletions assets/translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "¡Hola, Mundo!",
"subtitle": "Bienvenido a Localización",
"description": "Has pulsado el botón tantas veces:",
"switchLanguage": "Cambiar idioma"
}
6 changes: 6 additions & 0 deletions assets/translations/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "Bonjour",
"subtitle": "Bienvenue dans Localisation",
"description": "Vous avez appuyé sur le bouton autant de fois :",
"switchLanguage": "Changer de langue"
}
62 changes: 62 additions & 0 deletions build_runner.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/common/widgets/theme_switcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/common/widgets/toolbar_logo.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
37 changes: 37 additions & 0 deletions lib/src/core/utils/gen/localization/codegen_loader.g.dart
Original file line number Diff line number Diff line change
@@ -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<Map<String, dynamic>?> load(String path, Locale locale) {
return Future.value(mapLocales[locale.toString()]);
}

static const Map<String,dynamic> en = {
"title": "Hello, World!",
"subtitle": "Welcome to Localization",
"description": "You have pushed the button this many times:",
"switchLanguage": "Switch language",
};
static const Map<String,dynamic> es = {
"title": "¡Hola, Mundo!",
"subtitle": "Bienvenido a Localización",
"description": "Has pulsado el botón tantas veces:",
"switchLanguage": "Cambiar idioma",
};
static const Map<String,dynamic> fr = {
"title": "Bonjour",
"subtitle": "Bienvenue dans Localisation",
"description": "Vous avez appuyé sur le bouton autant de fois :",
"switchLanguage": "Changer de langue",
};
static const Map<String, Map<String,dynamic>> mapLocales = {"en": en, "es": es,
"fr": fr,};
}
9 changes: 9 additions & 0 deletions lib/src/core/utils/gen/localization/locale_keys.g.dart
Original file line number Diff line number Diff line change
@@ -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';

}
2 changes: 1 addition & 1 deletion lib/src/features/main/theme/theme_data/app_theme_data.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -27,14 +28,14 @@ class _MyHomePageState extends State<MyHomePage> {
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: <Widget>[
Text(
AppLocalizations.of(context)!.subtitle,
LocaleKeys.subtitle.tr(),
style: theme.textTheme.titleMedium!.copyWith(
color: theme.extension<OnSurfacePallet>()!.onSurface3,
),
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import FlutterMacOS
import Foundation

import shared_preferences_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,7 @@ dependencies:

dev_dependencies:
build_runner:
easy_localization_generator: ^0.3.3
flutter_lints: ^5.0.0
flutter_test:
sdk: flutter
Expand All @@ -42,6 +44,7 @@ flutter:
assets:
- assets/images/
- assets/icons/
- assets/translations/
fonts:
- family: Inter
fonts:
Expand Down
2 changes: 1 addition & 1 deletion test/src/core/common/widgets/theme_switcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/src/core/common/widgets/toolbar_logo_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4ee1611

Please sign in to comment.