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

Conversation

k-nkmr
Copy link
Contributor

@k-nkmr k-nkmr commented Dec 12, 2023

Fixes #283

🔗 Issue リンク

closes #283

🙌 やったこと

  • arbファイルをjsonファイルに変換
    • slangで用意されているコマンドを使用して変換
    • arbファイルに記載されているがプロジェクトで使用されていない単語を検索して削除
  • l10n.of(context)から、slangを使ったTranslations.of(context)に変更
  • その他、設定ファイル等で既存のローカライズに関連していそうな箇所を修正

✍️ やらないこと

  • なし

✅ 動作確認

  • Android
  • iOS
  • macOS
  • Web

スクリーンショット

その他

  • arbファイルをjsonファイルに変換
    • コマンドで生成されたjsonファイルを手動でいじってないのですが、appBarという単語がappbarに分かれてしまっていたりと、分けない方が良さそうな箇所もあるので書き換えてもいいかもしれないと思っています。
    • arbファイルに"notFoundPageReturnButtonLabel"というキーがあったのですが、jsonに変換する際に単語ごとに分解されるので"return"というキーができてしまい、Dartの予約語のreturnとかぶってしまって上手く動きませんでした。そのため、"notFoundPageBackButtonLabel"としましたが、分かりにくかったら教えてください。

@github-actions github-actions bot added documentation Improvements or additions to documentation flutter_app labels Dec 12, 2023
Copy link

codecov bot commented Dec 12, 2023

Codecov Report

Merging #285 (5853c27) into main (1da4a37) will not change coverage.
Report is 1 commits behind head on main.
The diff coverage is 0.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #285   +/-   ##
=======================================
  Coverage   25.10%   25.10%           
=======================================
  Files          16       16           
  Lines         247      247           
=======================================
  Hits           62       62           
  Misses        185      185           
Files Coverage Δ
...pp/lib/features/theme_selector/theme_mode_ext.dart 22.22% <0.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c2c2ef...5853c27. Read the comment docs.

@k-nkmr k-nkmr marked this pull request as ready for review December 13, 2023 03:16
@k-nkmr k-nkmr requested a review from riscait as a code owner December 13, 2023 03:16
Copy link
Member

@riscait riscait left a comment

Choose a reason for hiding this comment

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

ご対応ありがとうございます🚀
いくつか質問させていただきました!

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

@@ -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.

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


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に書き直します📝

Copy link
Member

Choose a reason for hiding this comment

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

badge
マイグレーションすると、すべて1単語ずつの階層構造になるんですね✏️

Copy link
Member

@riscait riscait Dec 13, 2023

Choose a reason for hiding this comment

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

badge(←質問です)

  1. JSONファイルだけでなくyamlファイルも対応しているようですが、ARBからyamlへもmigrate可能ですか?
  2. home.page のようなところは homePage とまとめたい気がしますが、後からjsonファイルを編集すれば可能ですか?
  3. json(yaml)ファイルの分割は可能ですか?それとも各言語1ファイルにまとめないといけないでしょうか?

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.

マイグレーションすると、すべて1単語ずつの階層構造になるんですね✏️

キャメルケースの大文字になってるところを区切りに階層になるようです📝

JSONファイルだけでなくyamlファイルも対応しているようですが、ARBからyamlへもmigrate可能ですか?

ドキュメントにARBからJSONへのmigrateしか書いていないということと、arb_migration_test.dartというテストコードを見たのですがjsonファイルへのmigrateテストしか書いていないことから今はyamlは対応していないと思われます(migrate_arb.dartが少し長かったのでテストのみ見ました....👀)

home.page のようなところは homePage とまとめたい気がしますが、後からjsonファイルを編集すれば可能ですか?

はい!JSONファイルを自分で編集してからコマンドを叩いて、strings.g.dartを生成するという流れになるので可能です。
私もまとめた方がいいかなと思う箇所がいくつかあったので(app.barなど)、このあたりは自分でJSONファイルを修正しようかと思います📝

json(yaml)ファイルの分割は可能ですか?それとも各言語1ファイルにまとめないといけないでしょうか?

こちらもできるようで、以下のstructuring-featuresという項目のネームスペースがこれにあたるかと思います👀
https://pub.dev/packages/slang#structuring-features

Copy link
Member

Choose a reason for hiding this comment

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

badge

arb_migration_test.dartというテストコードを見たのですがjsonファイルへのmigrateテストしか書いていないことから今はyamlは対応していないと思われます

パッケージのテストコード見れて素晴らしいですね👍

なるほど、そしたらJSONファイルからyamlファイルにしたいときは、GitHub CopilotかChatGPTにお願いして変換してもらうのが捗りそうですね🚀

はい!JSONファイルを自分で編集してからコマンドを叩いて、strings.g.dartを生成するという流れになるので可能です。

良かったです。柔軟に設定できそうですね🙌

こちらもできるようで、以下のstructuring-featuresという項目のネームスペースがこれにあたるかと思います👀

リンクありがとうございます!なるほど、ファイル名を変えたりフォルダ作ったりしてnamespaceを確保できるんですね!
これは良さそう✨


回答ありがとうございます、勉強になりました!

Copy link
Member

Choose a reason for hiding this comment

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

badge
これと同じように Translationsのextensionも定義して context.translations で使用できるようにすると便利そうですね👀

@k-nkmr k-nkmr requested a review from a team as a code owner December 14, 2023 08:19
@k-nkmr k-nkmr requested review from naipaka and riscait and removed request for a team December 14, 2023 08:19
@k-nkmr
Copy link
Contributor Author

k-nkmr commented Dec 14, 2023

  • 全体的にfinal i18n = Translations.of(context); から final t = Translations.of(context); へ書き換えました。
  • flutter_app/README.mdに dart run slangを追記しました。
  • JSONファイルにて階層になると違和感がある単語を修正しました。(修正しない方が良かった箇所・修正した方がいい箇所があればご指摘お願いします🙇🏻‍♀️)

Copy link
Member

@riscait riscait left a comment

Choose a reason for hiding this comment

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

LGTM
Slangパッケージを使った多言語化対応ありがとうございました!

dart format で失敗しているのでそれだけ修正&コミットお願いします👍

Copy link
Member

Choose a reason for hiding this comment

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

badge
わかりやすいです!

Copy link
Contributor

@naipaka naipaka left a comment

Choose a reason for hiding this comment

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

slang良さそうですね!🚀
LGTM

@k-nkmr
Copy link
Contributor Author

k-nkmr commented Dec 18, 2023

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/*" \) )

find lib test と書いていたところをこちらに修正しました📝

Copy link
Member

@riscait riscait left a comment

Choose a reason for hiding this comment

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

LGTM
テストの実行が無事完了されてますね🙌
ご対応ありがとうございました!

@k-nkmr k-nkmr merged commit ce6bb80 into main Dec 18, 2023
7 checks passed
@k-nkmr k-nkmr deleted the Internationalization-using-slang branch December 18, 2023 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation flutter_app
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internationalization using slang
3 participants