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

fix: slangを使った多言語化 #285

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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 i18n = Translations.of(context);",
" return AppBar(",
" title: Text(l10n.${TM_FILENAME_BASE/(.*)/${1:/camelcase}${2}/}Title),",
" title: Text(i18n.${TM_FILENAME_BASE/[\\._]/./g}.title),",
" );",
" }",
"}",
Expand All @@ -67,7 +67,7 @@
"",
" @override",
" Widget build(BuildContext context, WidgetRef ref) {",
" final l10n = useL10n();",
" final i18n = Translations.of(context);",
" return $1;",
" }",
"}",
Expand Down
3 changes: 0 additions & 3 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
2 changes: 1 addition & 1 deletion packages/flutter_app/README.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge
わかりやすいです!

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Required only `--release` mode.
## How to use
### Localizations
```dart
final l10n = L10n.of(context);
final i18n = Translations.of(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge
slangパッケージREADMEでも t が使われていたので、 Translations を意味する t で良さそうですね👌

Suggested change
final i18n = Translations.of(context);
final t = Translations.of(context);

Copy link
Contributor Author

@k-nkmr k-nkmr Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsonファイルを編集して build_runner build すると、文言ファイルが生成されるような運用になるでしょうか?

  1. JSONファイルを作成/編集
  2. dart run build_runner build -d もしくは dart run slang(build_runnerより速いので開発中はこちらがおすすめとのこと)を実行する
  3. strings.g.dartファイルが生成される

このstrings.g.dartをimportすると各所で使用できるようになります👀

JSONファイルの置き場所、生成ファイルのアウトプット場所や名前は設定ファイルを作成することでカスタマイズできます。

build_runner を使用する場合は build.yaml を、 dart run slang を実行する場合は slang.yaml を設定ファイルとして作成します。
今回dart run slang を使っていたのでslang.yamlを設定ファイルとして作成しました。

slangパッケージREADMEでも t が使われていたので、 Translations を意味する t で良さそうですね👌

こちらもtに書き換えます🙏🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

詳しくありがとうございます!
なるほど、 build_runner で他の生成ファイルとまとめて生成もできるけど、 dart run slang で文言ファイルのみ個別に生成できてこちらの方が速いんですね🚀

flutter_app/README.md#Localizations 欄に dart run slang を使用することの追記をお願いしたいです🙇

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知いたしました!
追記します✍️

```

## 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 i18n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge
こちらも、型(実態)に沿った変数名にしつつ、スコープが狭いので t で十分そうです👌

Suggested change
String title(Translations i18n) {
String title(Translations t) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知いたしました!
tに書き直します📝

switch (this) {
case ThemeMode.system:
return l10n.themeModeTitleSystem;
return i18n.theme.mode.title.system;
case ThemeMode.light:
return l10n.themeModeTitleLight;
return i18n.theme.mode.title.dark;
case ThemeMode.dark:
return l10n.themeModeTitleDark;
return i18n.theme.mode.title.dark;
}
}

String subtitle(AppLocalizations l10n) {
String subtitle(Translations i18n) {
switch (this) {
case ThemeMode.system:
return l10n.themeModeSubtitleSystem;
return i18n.theme.mode.subtitle.system;
case ThemeMode.light:
return l10n.themeModeSubtitleLight;
return i18n.theme.mode.subtitle.light;
case ThemeMode.dark:
return l10n.themeModeSubtitleDark;
return i18n.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