I18N任務可幫助您查找和管理丟失和未使用的翻譯。
該GEM在靜態上分析密鑰用法的代碼,例如I18n.t('some.key') ,以:
因此解決了I18N寶石設計的兩個主要問題:
I18N任務可以使用Ruby I18N GEM(Rails中的默認值)與任何項目一起使用。
將I18N任務添加到Gemfile:
gem 'i18n-tasks' , '~> 1.0.14' , group : :development複製默認配置文件:
$ cp $( i18n-tasks gem-path ) /templates/config/i18n-tasks.yml config/複製RSPEC測試以測試缺失和未使用的翻譯作為套件的一部分(可選):
$ cp $( i18n-tasks gem-path ) /templates/rspec/i18n_spec.rb spec/或最大的:
$ cp $( i18n-tasks gem-path ) /templates/minitest/i18n_test.rb test/ 運行bundle exec i18n-tasks以獲取所有任務的列表,其中包含簡短的描述。
i18n-tasks health檢查是否缺少任何密鑰,插值變量在整個地區都保持一致,並且所有語言環境文件均已歸一化(自動形式):
$ i18n-tasks health與占位符(基本價值或人性化密鑰)一起添加缺失的鑰匙:
$ i18n-tasks add-missing這個和其他任務接受參數:
$ i18n-tasks add-missing -v ' TRME %{value} ' fr通過--help以獲取更多信息:
$ i18n-tasks add-missing --help
Usage: i18n-tasks add-missing [options] [locale ...]
-l, --locales Comma-separated list of locale(s) to process. Default: all. Special: base.
-f, --format Output format: terminal-table, yaml, json, keys, inspect. Default: terminal-table.
-v, --value Value. Interpolates: %{value}, %{human_key}, %{value_or_human_key}, %{key}. Default: %{value_or_human_key}.
-h, --help Display this help message.使用您選擇的後端服務翻譯丟失的鍵。
$ i18n-tasks translate-missing
# accepts backend, from and locales options
$ i18n-tasks translate-missing --from=base es fr --backend=google可用後端:
google -Google翻譯deepl -Deepl Proyandex -yandex翻譯openai Openaiwatsonx -WATSONX查看與i18n-tasks find :
$ i18n-tasks find common.help
$ i18n-tasks find ' auth.* '
$ i18n-tasks find ' {number,currency}.format.* '$ i18n-tasks unused
$ i18n-tasks remove-unused這些任務可以推斷--no-strict鍵search.strict例如t("category.#{category.name}")
如果您想在使用刪除刪除時將訂購從原始語言文件中保留,請通過-k或--keep-order 。
排序鑰匙:
$ i18n-tasks normalize對鍵進行排序,然後將它們移至config.write所定義的相應文件:
$ i18n-tasks normalize -pi18n-tasks mv <pattern> <target>是一項多功能任務,可移動或刪除與給定模式匹配的密鑰。
所有節點(葉子或子樹)匹配<pattern>均合併在一起,並移動到<target> 。
重命名節點(葉子或子樹):
$ i18n-tasks mv user account移動節點:
$ i18n-tasks mv user_alerts user.alerts將孩子們提高一個層次:
$ i18n-tasks mv ' alerts.{:} ' ' 1 '合併移動多個節點:
$ i18n-tasks mv ' {user,profile} ' account合併(非葉)節點與父母:
$ i18n-tasks mv ' {pages}.{a,b} ' ' 1 '使用rm任務刪除鍵:
$ i18n-tasks rm ' user.{old_profile,old_title} ' another_keyi18n-tasks還提供了可複雜的任務,用於閱讀,寫作和操縱語言環境數據。下面的示例。
通過missing , tree-set-value和data-merge實現的add-missing :
$ i18n-tasks missing -f yaml fr | i18n-tasks tree-set-value ' TRME %{value} ' | i18n-tasks data-merge remove-unused使用和data-remove的unused (不確認):
$ i18n-tasks unused -f yaml | i18n-tasks data-remove從en中刪除fr中不存在的所有鍵。不要更改en :
$ i18n-tasks missing -t diff -f yaml en | i18n-tasks tree-mv en fr | i18n-tasks data-remove請參閱i18n-tasks --help 。
i18n-tasks使用.rb和.html.erb文件的AST掃描儀,以及基於Regexp的其他文件(例如.haml的掃描儀。
i18n-tasks為相對鍵提供支持,例如t '.title' 。
✔鍵相對於它們在中使用的文件路徑(請參見相對根配置)。
✔鍵相對於controller.action_name的鍵。使用最接近的def名稱。
✔複數鍵,例如key.{one,many,other,...}得到完全支持。
✔參考鍵(具有:symbol值的鍵)得到了完全支持。這些鍵在add/translate-missing中被複製為IS,可以在find中通過參考或值查找。
t()關鍵字參數✔ scope關鍵字參數得到了AST掃描儀以及RegexP掃描儀的完全支持,但僅在第一個參數時才支持。
✔ default參數可用於預填充本地文件(僅AST掃描儀)。
默認情況下,未識別諸如t "cats.#{cat}.name"之類的動態鍵。我鼓勵您使用I18N任務的提示來標記它們。
另外,您可以通過將search.strict設置為false在配置中啟用動態鍵推理。在這種情況下,將考慮使用密鑰的所有動態部分,例如cats.tenderlove.name不會報告為未使用。請注意,只有鑰匙的一個部分被視為每個字符串插值的通配符。即在此示例中, cats.tenderlove.special.name將被報告為未使用。
I18n.localize不支持,使用I18N任務 - 使用提示。這是因為I18n.localize生成的密鑰取決於傳遞給對象的類型,因此無法從靜態上推斷。
配置是從config/i18n-tasks.yml或config/i18n-tasks.yml.erb讀取的。使用i18n-tasks config檢查配置。
使用以下方式安裝默認配置文件
$ cp $( i18n-tasks gem-path ) /templates/config/i18n-tasks.yml config/默認情況下,設置與導軌兼容。
默認情況下, base_locale設置為en ,並且從數據文件的路徑中推斷出語言locales 。您可以在配置中覆蓋它們。
默認數據適配器支持YAML和JSON文件。
I18N任務可以管理多個翻譯文件並讀取其他寶石的翻譯。要了解更多信息,請參閱配置中的data選項。 NB:默認情況下,僅讀取僅%{locale}.yml文件,而不是namespace.%{locale}.yml 。確保檢查配置。
為了寫入語言環境文件,i18n任務提供了2個選項。
模式路由器根據鍵模式列表組織鍵,如下示例:
data:
router: pattern_router
# a list of {key pattern => file} routes, matched top to bottom
write:
# write models.* and views.* keys to the respective files
- ['{models,views}.*', 'config/locales/1.%{locale}.yml']
# or, write every top-level key namespace to its own file
- ['{:}.*', 'config/locales/1.%{locale}.yml']
# default, sugar for ['*', path]
- 'config/locales/%{locale}.yml'
保守的路由器可以將發現的鑰匙保留在鑰匙中,或者從基地區域中滲透路徑。如果鑰匙是全新的,則保守的路由器將歸結為模式路由器的行為。保守路由器是默認路由器。
data:
router: conservative_router
write:
- ['devise.*', 'config/locales/devise.%{locale}.yml']
- 'config/locales/%{locale}.yml'
如果您想使用data.write將i18n任務重組您現有的鍵,請將路由器設置為上述pattern_router ,或運行i18n-tasks normalize -p (強迫使用模式路由器為此運行)。
隔離路由器假設每個YAML文件都是獨立的,並且可以包含相似的密鑰。
結果,將翻譯寫入每個源文件的替代目標文件(僅更改了%{locale}零件以匹配目標語言環境)。因此,沒有必要指定任何write配置(實際上,將完全忽略)。
例如,當使用ViewComponent Sidecars時,這可能很有用(ViewComponent為每個SideCar YAML文件分配一個隱式範圍,但是i18n-tasks不知道該邏輯,從而導致碰撞):
app/components/movies_component.en.yml :
en :
title : Movies app/components/games_component.en.yml
en :
title : Games但是,該路由器有一個限制:它不支持從代碼使用中檢測丟失的鍵(因為它不知道隱式範圍邏輯)。
在I18N任務中,使用類似於文件的特殊語法,以匹配翻譯鍵:
| 句法 | 描述 |
|---|---|
* | 匹配一切 |
: | 匹配一個鍵 |
*: | 匹配單鍵的一部分 |
{a, bc} | 匹配任何設置,可以使用:和* ,捕獲匹配 |
用法的示例:
$ bundle exec i18n-tasks mv " {:}.contents.{*}_body " " 1.attributes.2.body "
car.contents.name_body ⮕ car.attributes.name.body
car.contents.description_body ⮕ car.attributes.description.body
truck.contents.name_body ⮕ truck.attributes.name.body
truck.contents.description_body ⮕ truck.attributes.description.body如果將數據存儲在某個地方但在文件系統中,例如在數據庫或MongoDB中,則可以實現自定義適配器。如果您實施了自定義適配器,請在Wiki上分享。
I18N任務使用.rb和.html.erb文件的AST掃描儀,以及所有其他文件的RegexP掃描儀。可以輕鬆添加新的掃描儀:請參閱此示例。
有關所有可用配置選項,請參見配置文件中的search部分。 NB:默認情況下,僅搜索app/目錄。
添加提示在靜態分析中使用魔術評論提示(默認情況下以(#|/) i18n-tasks-use行):
# i18n-tasks-use t('activerecord.models.user') # let i18n-tasks know the key is used
User . model_name . human您還可以通過ignore*設置在語言文件文件中明確忽略鍵。
如果您具有生成翻譯鍵的輔助方法,例如返回t '.page_title'的page_title方法,或者返回t "spree.#{key}"的Spree.t(key)方法,請使用內置PatternMapper映射這些。
對於更複雜的情況,您可以實現自定義掃描儀。
請參閱配置文件以了解更多信息。
i18n-tasks translate-missing需要Google Translate API鍵,在Google API控制台上獲取。
此密鑰的位置取決於您的Google API控制台:
在這兩種情況下,如果不存在密鑰,則可能需要創建密鑰。
將鍵放入GOOGLE_TRANSLATE_API_KEY環境變量或配置文件中。
# config/i18n-tasks.yml
translation :
backend : google
google_translate_api_key : <Google Translate API key>或通過環境變量:
GOOGLE_TRANSLATE_API_KEY= < Google Translate API key >i18n-tasks translate-missing需要一個deepl pro api鍵,然後在deepl上獲取它。如果您僅在內部使用簡單的語言環境,則可以指定別名環境。
# config/i18n-tasks.yml
translation :
backend : deepl
deepl_api_key : <DeepL Pro API key>
deepl_host : <optional>
deepl_version : <optional>
deepl_glossary_ids :
- f28106eb-0e06-489e-82c6-8215d6f95089
- 2c6415be-1852-4f54-9e1b-d800463496b4
deepl_options :
formality : prefer_less
deepl_locale_aliases :
en : en-us
pt : pt-br或通過環境變量:
DEEPL_API_KEY= < DeepL Pro API key >
DEEPL_HOST= < optional >
DEEPL_VERSION= < optional >i18n-tasks translate-missing需要Yandex API鍵,在Yandex上獲取。
# config/i18n-tasks.yml
translation :
backend : yandex
yandex_api_key : <Yandex API key>或通過環境變量:
YANDEX_API_KEY= < Yandex API key >i18n-tasks translate-missing需要OpenAI API鍵,然後在OpenAi獲取。
# config/i18n-tasks.yml
translation :
backend : openai
openai_api_key : <OpenAI API key>
openai_model : <optional>或通過環境變量:
OPENAI_API_KEY= < OpenAI API key >
OPENAI_MODEL= < optional >i18n-tasks translate-missing需要WATSONX項目和API密鑰,然後在IBM Watsonx上獲取它。
# config/i18n-tasks.yml
translation :
backend : watsonx
watsonx_api_key : <watsonx API key>
watsonx_project_id : <watsonx project id>
watsonx_model : <optional>或通過環境變量:
WATSONX_API_KEY= < watsonx API key >
WATSONX_PROJECT_ID= < watsonx project id >
WATSONX_MODEL= < optional >具有更多背景的解析導軌有一個實驗功能。 i18n-tasks將支持:
before_actions的翻譯Model.human_attribute_name調用Model.model_name.human呼叫通過在config/i18n-tasks.yml中添加掃描儀來啟用它:
<% I18n :: Tasks . add_scanner (
'I18n::Tasks::Scanners::PrismScanner' ,
only : %w( *.rb )
) %>為了僅啟用Ruby-Scanning而不啟用任何導軌支持,請在search部分下添加配置:
search :
prism_visitor : " ruby " # default "rails" i18n-tasks irb在I18N任務上的上下文中啟動了IRB會話。類型guide以獲取更多信息。
請參閱I18N任務Wiki:CSV導入和導出任務。
GEM隨附的任務是在lib/i18n/tasks/command/命令中定義的。可以輕鬆添加自定義任務,請參見Wiki上的示例。
bundle install安裝依賴項bundle exec rspec運行測試overcommit --install SKIP=RuboCop git commitOVERCOMMIT_DISABLE=1 git commit