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