Skip to content

Commit

Permalink
fix: slangを使った多言語化 (#285)
Browse files Browse the repository at this point in the history
* Internationalization using slang
Fixes #283

* slangを使った多言語化

* fix: slangを使った多言語化

* fix: slangを使った多言語化

* fix: slangを使った多言語化
  • Loading branch information
k-nkmr authored Dec 18, 2023
1 parent 4c2c2ef commit ce6bb80
Show file tree
Hide file tree
Showing 32 changed files with 1,271 additions and 206 deletions.
12 changes: 6 additions & 6 deletions .vscode/page.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
"body": [
"import 'package:flutter/material.dart';",
"import 'package:flutter_hooks/flutter_hooks.dart';",
"import 'package:themes/themes.dart';",
"",
"import '../../../../commons/hooks/hooks.dart';",
"import '../../../style/style.dart';",
"import '../../../gen/strings.g.dart';",
"",
"class ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}${2}/} extends HookWidget implements PreferredSizeWidget {",
" const ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}${2}/}({super.key});",
"",
" @override",
" Size get preferredSize => appbarSize;",
" Size get preferredSize => appBarDefaultSize;",
"",
" @override",
" Widget build(BuildContext context) {",
" final l10n = useL10n();",
" final t = Translations.of(context);",
" return AppBar(",
" title: Text(l10n.${TM_FILENAME_BASE/(.*)/${1:/camelcase}${2}/}Title),",
" title: Text(t.${TM_FILENAME_BASE/[\\._]/./g}.title),",
" );",
" }",
"}",
Expand All @@ -67,7 +67,7 @@
"",
" @override",
" Widget build(BuildContext context, WidgetRef ref) {",
" final l10n = useL10n();",
" final t = Translations.of(context);",
" return $1;",
" }",
"}",
Expand Down
6 changes: 2 additions & 4 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ packages:
command:
bootstrap:
runPubGetInParallel: true
hooks:
post: |
melos exec --flutter --dir-exists=lib/l10n -- "flutter gen-l10n"
clean:
hooks:
post: |
Expand Down Expand Up @@ -75,10 +72,11 @@ scripts:
flutter: true
dirExists: [lib, test]

# ファイル除外機能に関するissue https://github.com/dart-lang/dart_style/issues/864
format:ci:
run: |
melos exec -- \
dart format --set-exit-if-changed lib test
dart format --set-exit-if-changed $(find . -name "*.dart" -not \( -name "*.*freezed.dart" -o -name "*.*g.dart" -o -name "*.gen.dart" -o -wholename "./.dart_tool/*" \) )
description: Run format.
packageFilters:
flutter: true
Expand Down
21 changes: 20 additions & 1 deletion packages/flutter_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,27 @@ Required only `--release` mode.

## How to use
### Localizations
JSONファイルを作成
```json
{
"hello": "Hello $name",
"save": "Save",
"login": {
"success": "Logged in successfully",
"fail": "Logged in failed"
}
}
```
Dartファイルを生成
```shell
dart run slang
```

生成されたDartファイルをimportして使用
```dart
final l10n = L10n.of(context);
import '../../../gen/strings.g.dart';
final t = Translations.of(context);
```

## FlutterFire Configure
Expand Down
1 change: 0 additions & 1 deletion packages/flutter_app/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ analyzer:
plugins:
- custom_lint
exclude:
- "**/l10n/*.dart"
- "**/gen/*.dart"
- "**/firebase_options_*.dart"
4 changes: 0 additions & 4 deletions packages/flutter_app/l10n.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../../gen/strings.g.dart';

extension ThemeModeExt on ThemeMode {
String title(AppLocalizations l10n) {
String title(Translations t) {
switch (this) {
case ThemeMode.system:
return l10n.themeModeTitleSystem;
return t.theme.mode.title.system;
case ThemeMode.light:
return l10n.themeModeTitleLight;
return t.theme.mode.title.light;
case ThemeMode.dark:
return l10n.themeModeTitleDark;
return t.theme.mode.title.dark;
}
}

String subtitle(AppLocalizations l10n) {
String subtitle(Translations t) {
switch (this) {
case ThemeMode.system:
return l10n.themeModeSubtitleSystem;
return t.theme.mode.subtitle.system;
case ThemeMode.light:
return l10n.themeModeSubtitleLight;
return t.theme.mode.subtitle.light;
case ThemeMode.dark:
return l10n.themeModeSubtitleDark;
return t.theme.mode.subtitle.dark;
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/flutter_app/lib/flutter_app.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:themes/themes.dart';

import 'features/theme_selector/theme_selector.dart';
import 'gen/strings.g.dart';
import 'router/router.dart';
import 'util/localizer/localizer.dart';
import 'util/providers/scaffold_messenger_key_provider.dart';

class FlutterApp extends ConsumerWidget {
Expand All @@ -17,12 +17,12 @@ class FlutterApp extends ConsumerWidget {

return MaterialApp.router(
routerConfig: router,
onGenerateTitle: (context) => L10n.of(context).title,
onGenerateTitle: (context) => Translations.of(context).title,
theme: appLightThemeData,
darkTheme: appDarkThemeData,
themeMode: ref.watch(themeSelectorProvider),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: AppLocaleUtils.supportedLocales,
scaffoldMessengerKey: ref.watch(scaffoldMessengerKeyProvider),
);
}
Expand Down
Loading

0 comments on commit ce6bb80

Please sign in to comment.