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

docs_getting_started #172

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

docs_getting_started #172

wants to merge 4 commits into from

Conversation

wtdlee
Copy link

@wtdlee wtdlee commented Feb 29, 2024

Why

Handbookの多言語化執筆でhi18nの使い方を一目で見れるドキュメントがあったら良さそうと思ったので追加

What

原さんのブログシリーズを参考に実装でよく使われるものをピックアップして1ページにまとめました。

@wtdlee wtdlee self-assigned this Feb 29, 2024
@wtdlee wtdlee requested a review from qnighy February 29, 2024 06:30
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you rename it as getting_started.ja.md?

Copy link
Author

Choose a reason for hiding this comment

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

ファイル名変更しました!

Comment on lines 81 to 83
2. 1 で追記したスコープを [/config/i18n-js.yml](https://github.com/wantedly/wantedly/blob/master/config/i18n-js.yml) に追記する。

ここで追加されたスコープは [/frontend/assets/javascripts/shims/I18n.js](https://github.com/wantedly/wantedly/blob/master/frontend/assets/javascripts/shims/I18n.js) をインポートすることで以下のよう使えるようになります。
Copy link
Collaborator

Choose a reason for hiding this comment

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

hi18n is a public library. Please keep the documentation useful outside Wantedly.

Copy link
Author

Choose a reason for hiding this comment

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

#172 (comment)
内容を修正・補充しました!

Comment on lines +5 to +114
// Some people even need to add the extension to make it work, see https://github.com/fnando/i18n-js/issues/283
//= require i18n.js
//
// This is a must
//= require i18n/translations
```

#### Rails app without Asset Pipeline
まず、以下の内容を `application.html` (layout file)に追加してください。その後続きの内容に沿って JS ファイルを取得します。

```
<%# This is just an example, you can put `i18n.js` and `translations.js` anywhere you like %>
<%# Unlike the Asset Pipeline example, you need to require both **in order** %>
<%= javascript_include_tag "i18n" %>
<%= javascript_include_tag "translations" %>
```

#### `translations.js`を取得する2つのほうほう

1. `config/application.rb`ファイルに`config.middleware.use I18n::JS::Middleware`を追加するだけで`I18n::JS::Middleware`によって`translations.js`ファイルを自動的に生成することができます
2. `translations.js`を生成したくないまたは生成しないことを好む場合は、ミドルウェア行を`config/environments/development.rb`ファイルに移動しデプロイ前に`rake i18n:js:export`を実行します。これにより、`config/i18n-js.yml`に定義されているカスタムスコープを含むすべての翻訳ファイルがエクスポートされます。`I18n.available_locales`が設定されている場合(例えば、Railsの`config/application.rb`ファイル内)、指定されたロケールのみがエクスポートされます。現在の`i18n.js`のバージョンもエクスポートされダウンロードによるバージョンの不一致を避けます。

#### 設定エクスポート(翻訳)

`I18n::JS::Middleware`または`rake i18n:js:export`によって生成されたエクスポート済み翻訳ファイルは、設定ファイル`config/i18n-js.yml`を使用してカスタマイズすることができます(この作成に`rails generate i18n:js:config`を使用します)。さらに、異なるフォルダーに異なる翻訳でより多くのファイルを生成することもできます。この設定は、Asset Pipelineを使用して翻訳する開発者にも影響します。ただし、`//= require i18n/translations`を追加するとすべての翻訳が要求されるため、fileオプションは除外されます。

Examples:
```
translations:
- file: 'public/javascripts/path-to-your-messages-file.js'
only: '*.date.formats'
- file: 'public/javascripts/path-to-your-second-file.js'
only: ['*.activerecord', '*.admin.*.title']
```
`only`が省略された場合、すべての翻訳が保存されます。また、初めに`*`を確認してください。`*`はすべての言語のエクスポートを指定します。一つの言語のみをエクスポートしたい場合は以下のように行うことができます:

```
translations:
- file: 'public/javascripts/en.js'
only: 'en.*'
- file: 'public/javascripts/pt-BR.js'
only: 'pt-BR.*'
```
必要に応じて、`%{locale}`プレースホルダーを指定することで、利用可能なロケールごとに翻訳ファイルを自動生成することができます。

```
translations:
- file: "public/javascripts/i18n/%{locale}.js"
only: '*'
- file: "public/javascripts/frontend/i18n/%{locale}.js"
only: ['*.frontend', '*.users.*']
```
設定ファイルにERBを含めることもできます。

```
translations:
<% Widgets.each do |widget| %>
- file: <%= "'#{widget.file}'" %>
only: <%= "'#{widget.only}'" %>
<% end %>
```
特定のフレーズやフレーズのグループ全体を除外する場合には`except設定オプション`にYAMLキーを指定します。出力されたJS翻訳ファイル(エクスポートされたものやミドルウェアによって生成されたもの)は`except設定パラメーター`にリストされたキーを省略します。

```
translations:
- except: ['*.active_admin', '*.ransack', '*.activerecord.errors']
```

#### 設定エクスポート(その他)
- `I18n::JS.config_file_path`
- 期待される型: 文字列
- デフォルト: `config/i18n-js.yml`
- 挙動: その場所から設定ファイルを読み込もうとします
- `I18n::JS.export_i18n_js_dir_path`
- 期待される型: 文字列
- デフォルト: `public/javascripts`
- 挙動:
- 任意の文字列: `Rails.root`に対する相対パスとしてのフォルダーを考慮し、`rake i18n:js:export`でそのフォルダーに`i18n.js`をエクスポートします
- 文字列以外のもの(nil, false, :noneなど): i18n.jsのエクスポートを無効にします
- `I18n::JS.sort_translation_keys`
- 期待される型: ブール値
- デフォルト: `true`
- 挙動:
- すべての翻訳キーを深くソートして、同じ翻訳に対して同一の出力を生成するかどうかを設定します
- アセットパイプラインのために同一のアセットフィンガープリントを保証するには`true`に設定します
- 設定ファイルで`export_i18n_js`や`sort_translation_keys`も設定することができます。

```
export_i18n_js: false
# OR
export_i18n_js: "my/path"

sort_translation_keys: false

translations:
- ...
```

#### 参考ドキュメント
- [rubydoc: i18n-js](https://www.rubydoc.info/gems/i18n-js/3.0.2)
Copy link
Author

Choose a reason for hiding this comment

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

📝 設定方法を詳しく記載

@wtdlee wtdlee requested a review from qnighy March 18, 2024 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants