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
I18nを新たに追加しました。 #1
Conversation
せっかくなのでgitのcommit積み直しをやってみましょうか! 参考にしつつ手順をご説明します。一旦全部説明を読んでいただいて、何が起こるかを把握されてからお手元でやってみると良いかと思います!
※うっかり git checkout と書いちゃったのですがgitの最新では git switch i18n と書くようです。そっちのほうがかっこいいですね! 🙇 ついでに今のcommit logがどうなっているか確認します。ずらっとコミットが積まれていることが分かります。
git status で状態を確認すると、改めて対象ファイル群を確認できます。
git log を見ると、初回commit ~ master の分のログしかなく、うまく取り消しできていることがわかります。
add したら commit します。
このひとまとめにしたい分をadd -> commit を積み直しぶんだけ( git status で変更されたファイルが表示されなくなるまで ) 繰り返します。
(これはやっちゃうと上書きされちゃうので自分からはスクショはありません! 🙇 ) 分からないところがあればお気軽にご質問ください! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gitの他、一旦さっと気になったところについてコメントさせていただきました!
README.md
Outdated
* Deployment instructions | ||
|
||
* ... | ||
# i18nでアプリを多言語化する課題のブランチ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
README はこのリポジトリに関する情報を記入しておくファイルなので、ブランチ毎に変更していたらgitのコンクリフトが発生してしまって大変そうです 💦
もし積み直しされる際にREADME.mdの変更いらないや、というときで、 $ git status
で Changes not staged for commit の一覧に表示され続けて困った場合は、次のようにすることで変更を取り消すことができます。
$ git restore README.md
$ git restore .
としてしまうと全てのファイルの変更が消えてしまうのでこれも取り扱いに十分注意してください!
参考: https://iucstscui.hatenablog.com/entry/2019/11/04/231212
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにコンフリクトが発生していました💦
ご指摘ありがとうございます!🙇🏻♂️
app/views/books/edit.html.erb
Outdated
@@ -1,6 +1,6 @@ | |||
<h1>Editing Book</h1> | |||
<h1><%= t 'books.edit.title' %></h1> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのキーについて、 books.edit.title
って書くの長いな〜と思う場合は、 .title
とすることもできます。
やり方については下記記事を参照してください!
lazy-lookupについて(4.1.4 探索の「遅延」)
https://railsguides.jp/i18n.html#%E6%8E%A2%E7%B4%A2%E3%81%AE%E3%80%8C%E9%81%85%E5%BB%B6%E3%80%8D%EF%BC%88lazy-lookup%EF%BC%89
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
変更出来ました!
app/views/books/index.html.erb
Outdated
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
<td><%= link_to t('books.index.show'), book %></td> | ||
<td><%= link_to t('books.index.edit'), edit_book_path(book) %></td> | ||
<td><%= link_to t('books.index.destroy'), book, method: :delete, data: { confirm: 'Are you sure?' } %></td> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの Are you sure?
もi18nを適用してください。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらも適用出来ました!
app/views/books/_form.html.erb
Outdated
@@ -12,22 +12,22 @@ | |||
<% end %> | |||
|
|||
<div class="field"> | |||
<%= form.label :title %> | |||
<%= form.label t('books.create.title') %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
label は ja.activerecord.attributes.book.title
のようにキーを設定しておくことで、 <%= form.label :title %>
の表記でもi18nを適用してくれます!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
適用出来ました!🙇🏻♂️
commit の積み直し、とても参考になりました!ご丁寧な解説をありがとうございます!🙇🏻♂️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commit がうまく積み直しされたみたいですね! ありがとうございます。
また、i18n自体もいい感じに修正されたと思います 👍
今回はRailsアプリにおいてcommitすべきもの・すべきでないものについて再整理していきたいと思います。
改めて自分の開発環境でこのアプリを動かそうと思ったところ、色々必要なものが足りておらず、動かないということに気付きました 😭 前回指摘しそこねており、すみません...!
gitを使ってリポジトリを共有する一番の目的として、複数人が開発をするためというものがあると思います。
このとき、Rails new した人が最初にコミットしてGitHubにアップしたものを、他の開発者がgit clone してきて、そして動かせる環境にできる必要があります。
なので、動かすために必要なファイルはコミットに含んでおく必要があります。
主に必要なファイルとしては、次のようなものがあります。
・app/ 以下のファイル
・db/ 以下のmigrationファイルなど ( *sqlite3 のファイルは不要 )
・config/ 以下の設定ファイル ( *重要なシステムキーなどは環境変数を利用するなどの方法でコミットしないようにする )
一度、お手元にて今使っている books_app
フォルダと別に空のフォルダを作成し、
そこでこのPullRequestの状態を持ってきて、rails server
でRailsが動く状態を目指してみて欲しいです!
# books_app_clone というフォルダを作成
$ mkdir books_app_clone
$ cd books_app_clone
# このリポジトリをcloneする
$ git clone git@github.com:tam-03/books_app.git
$ cd books_app
# i18nブランチをもってくる
$ git checkout -b i18n origin/i18n
# おそらくGemfileが無いのでbundle install ができない?
$ bundle install
Could not locate Gemfile
おまけ: .gitignore
の雛形ですが下記が結構参照されているかなと思います。
https://github.com/github/gitignore/blob/master/Rails.gitignore
config/locales/ja.yml
Outdated
books: | ||
index: | ||
title: | ||
h1_html: '本の一覧' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
キー名についてだけ... h1_html
という表現だと、次のような問題がありそうです。
- html を含む内容と感じる (例、
<h1>本の一覧</h1>
という値が入っているのでは? と誤解される可能性が高い) - もし同じ表現を使いまわしたくなったとき、h1 以外の箇所では使うことをためらってしまうキーになっている
htmlという表記を使わない、またタグの英単語を用いない形で表現できないか再考をお願いします! 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにそうでした。。ご指摘ありがとうございます!
_html
になっている部分を修正しました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちら日報のほうにも書かせていただきましたが、誤解という意味だけでなくちゃんとRails側が _html
というキーにするとHTMLエスケープさせないという特別な機能をもっていました!確認不足ですみません 🙇
https://railsguides.jp/i18n.html#4.4安全なHTML変換
もし使う場合は、適切なタイミングで使っていけるといいですね 👍
すみません、おまけでもう1点。 以降さまざまなシーンで、解釈違いによる悲しいすれ違いを防ぐために、できるだけ言葉を尽くして意図を伝えようとする癖をつけていただくのが良いかと思っています。 |
ご指摘ありがとうございます! 分からなくて、お聞きしたい事があるのですが、「PullRequestの状態を持ってきて、 現状の手順だと、
この手順でないとRailsアプリが動作しません。。 ※ 現状は 修正した流れ必要なファイルがpush出来ていなかった件なのですが、確かにGemfileが無かったので、 そこで、
エラー1: 「node_modules」がインストールされてない$ rails s
error Couldn't find an integrity file
error Found 1 errors.
========================================
Your Yarn packages are out of date!
Please run `yarn install --check-files` to update.
========================================
To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml). こんな感じのエラーだったのですが、 参考: Rails6 開発時につまづきそうな webpacker, yarn 関係のエラーと解決方法 参考を基に、 エラー2: 「Migration」を保留しているこんなエラーが、(画像は引用です) エラー内容
参考: ActiveRecord::PendingMigrationError と言われた時の解決方法 Migrationを保留しているというエラーだそうで、 これでは、「PullRequestの状態を持ってきて、
で動かないのかが分かりません😭 質問そこで質問させて頂きたいのですが、 このbooksアプリの場合、 長々とすみませんが、お手隙の際にご返信頂けると助かります!🙇🏻♂️ よろしくお願い致します! |
PullRequestのタイトル、曖昧なままで決めてしまいました💦 |
すみません!もう一つ質問があるのですが、 「外に公開してはいけない、秘密の情報」などを検索するワードが分からなくて困っております😭 駒形さんのこの記事も確認したのですが、よく分かりませんでした💦 dbのユーザー・パスワード等、ユーザー認証(?)などの際に使うファイルやコードなのでしょうか?🤔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Railsアプリ動作手順
すみません、これは自分の説明が不足していたようです!
ご説明いただいたとおり、次の手順が正しいと思います。
$ git clone
$ bundle install
$ yarn install
$ bundle exec rake db:migrate
$ rails s
書くのを忘れてしまった理由としては、自分は普段古いリポジトリを仕事として運用しているのですが、migration および yarn が必要になる変更をあまり普段対応しないためスポンと抜けておりました...大変申し訳ないです!
このbooksアプリの場合、yarn install,bundle exec rake db:migrateは、README.mdに使い方を書く様な、必要な手順なのでしょうか?
はい、動かすために必要ですので、README.md に書いてあると後続の開発者のためになって良いと思います 👍
プロジェクトによっては、今回tam-03さんが自己解決していただいたように、エラー時にはちゃんとこのコマンドを叩いてくださいという案内が出るので、それを見ればOKでしょうという解釈をして書いていないケースもあるかもしれません。
config/以下の設定ファイルで、環境変数にして公開を避けるべきファイルはどの様なファイルなのでしょう?
「外部に漏れてしまうとセキュリティ的に問題がある」文字列は基本的にコミットしません。
特に、現在このリポジトリはpublicリポジトリ(誰でも閲覧できる状態)なので、特に注意する必要があると思います。
公開を避ける方法としては、「環境変数にする」以外にも「ファイル自体を.gitignoreに設定してコミットしない」ケースがあります。.gitignore
に書かれている次の2ファイルが「ファイル自体をコミットしない」ケースの主な例になるかなと思います。
# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
config/initializers/secret_token.rb
config/master.key
これのTODOコメントは、「privateリポジトリとして組織内でしか公開してないとかなら、入れるという判断もありですよ」という意味合いにも取れるように感じます。あとはGitHubさんのセキュリティをどこまで信じるかという話になりそうです 👀
ここの表記のうち、secret_token.rb は 古いRails4系で利用されていたものなので、実はもう気にしなくて良いかなと思います。
config/master.key
は、現在のRails(今は最新6.0ですね)で利用される重要な鍵です。rails credentials:edit
コマンドは config/master.key
を暗号化の鍵として秘密情報の編集ができる機能です。編集された秘密情報は config/credentials.yml.enc
に保存されますが、このファイルは config/master.key
を用いて復号しない場合、ぱっと見ても変な文字列が並んでいるだけでよくわからない(暗号化されている)状態になっています。 適切な config/master.key
を配置することで、Railsアプリからはまともな文字列として読めるようになります。
適当な文字列は万が一漏れても基本的には問題ないと考えてコミットし、代わりに鍵としてconfig/master.key
を厳重に守るためにコミットに含みません。鍵と暗号文字がセットでコミットされていたら、どっちもバレたらすぐに復号されてしまうのでNGです。
詳しくはこちらをご覧ください!
https://railsguides.jp/security.html#10.1 独自のcredential
一応Railsガイドにも次のように書かれていますね。
マスターキーは安全な場所に保管してください。マスターキーをコミットに含めてはいけません。
さらに「一体何を暗号として管理すれば良いのか」ですが、ご指摘の通りDBのユーザー名とパスワードとか、あと関連するサービスのログイン情報などです。特に大事故に繋がる話が時々でてくるのはAWS(Amazon Web Service) です。AWSでサーバを立てるとお金が当然かかるのですが、今時はサーバを使ってできるお金儲けとしてビットコインの採掘とかがあったりとか、他にもサーバを他人のお金で作って無限に使いたいという悪い人たちからのニーズはたくさんあります。特にGithubのpublicリポジトリにうっかりAWSのログイン情報をわかりやすく置いてしまうとシュッと抜かれて使われてしまうそうです。本当にありうる怖い話としてぜひ事前に把握しておいてください!
GitHub に AWS キーペアを上げると抜かれるってほんと???試してみよー! - Qiita
.gitignore
Outdated
/.browserslistrc | ||
|
||
# rubocop | ||
/.rubocop.yml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.rubocop.yml
はrubocopのルールを記載する設定ファイルなので、チームで進めていく場合はルール共有のためにリポジトリに含めておいて管理するのがよくあるケースかなと思います。ぜひ追加をお願いします! 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
チームで進めていく場合はルール共有のためにリポジトリに含めておいて管理するのがよくあるケース
そうなんですね!ありがとうございます!
.rubocop.yml
をリポジトリに追加しました!
app/views/books/edit.html.erb
Outdated
@@ -1,6 +1,6 @@ | |||
<h1>Editing Book</h1> | |||
<h1><%= t '.title' %></h1> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ソースコード全体の中に、 t '.title'
, t('.title')
のどちらの書き方も混在している様子です。
片方の書き方に統一してはどうでしょう?
目的としては、今後同様の書き方をするときにわずかな時間でも「あれ、どちらに合わせて書けば良かったかな?」と悩まなくて済んだほうが良いかなと考えています 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ありがとうございます!
t('.title')
に統一しました!
質問のご回答をありがとうございます!
逆に言えば必要でないケースもあるんですね!
確かにコマンドを叩くだけだったので書かなくても良いとなりそうですね! 秘密の情報の公開を避ける方法についても大変勉強になります! 初心者の考えた事としては、 「秘密の情報は だと思っていたので、
以上を頭でループしていました。 多分点在している場合もあって、
気をつけなければいけない事が、 コミットはOK。という事ですよね! AWSの場合の例怖いですね。。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいところですが追加コメントさせていただきました! 🙏
秘密の情報
多分点在している場合もあって
config/master.keyを安全に保管する事で、秘密の情報は基本的にconfig/credentials.yml.encここに記載して暗号化
書いていただいている解釈でだいたいOKと思います! 🙆
どこに何を書くかというのは各プロジェクトの都合などももちろん出てくるので、点在していたり、統一されていたり、様々かなと思います。とりあえず重要なのは 平文 ( 暗号化されていない) 状態でコミットしない、 暗号化されている場合は鍵(今回の場合master.key)をコミットしない ...ということかなと思います!
今後さらにRailsや各種gemがアップデートされていくなかで、ベストなやり方というのは変わってくるのかなと思いますが、ぜひ引き続き注意してみてください〜 👀
awslabs/git-secrets
おお、これ知りませんでした!
自分も使ってみようと思います〜 👍
app/views/books/index.html.erb
Outdated
@@ -1,14 +1,14 @@ | |||
<p id="notice"><%= notice %></p> | |||
|
|||
<h1>Books</h1> | |||
<h1><%= t '.books' %></h1> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
見つけてしまったので! 👀
<h1><%= t '.books' %></h1> | |
<h1><%= t('.books') %></h1> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すみません!見逃してました💦
config/locales/ja.yml
Outdated
notice_destroy: '書籍が正常に破棄されました。' | ||
books: | ||
index: | ||
title: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このキーは空なので消しておきましょうか〜 (下の行で再定義されていますね)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
削除して、これの下の、
title:
memo:
なども、activerecordのモデルで参照する記述に書き換えました!
config/locales/ja.yml
Outdated
index: | ||
title: | ||
books: '本の一覧' | ||
title: 'タイトル' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
設定の中で繰り返し、bookのattributes と同じ名前「タイトル」「メモ」「著者」「写真」が出てくる場合、次のよう次のような書き方で bookのattributesのほうの翻訳を呼び出すことができます。
Book.human_attribute_name(:title)
#=> "タイトル"
こうすることで、もし翻訳を変更したくなった場合、関連箇所を一括で修正することができます。
適用するとja.ymlのこの辺りの箇所の記述を減らすことができそうです。
詳しくはこちら: Rails 国際化 (i18n) API - Railsガイド#4.5 Active Recordモデルで翻訳を行なう
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらに書き換えました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正ありがとうございます!レビューOKです 🎉
Railsのプラクティス、i18nの課題です。