我想my_translation用可选参数进行翻译.例如:
> I18n.t('my_translation')
=> "This is my translation"
> I18n.t('my_translation', parameter: 1)
=> "This is my translation with an optional parameter which value is 1"
Run Code Online (Sandbox Code Playgroud)
这可能吗?
这可能是I18n-ception,但我可以说我有一个en.yml文件如下
en:
my_var: Foo
my_message: "This is a message where I'd like to interpolate I18n's %{my_var}"
Run Code Online (Sandbox Code Playgroud)
有没有办法向I18n表明%{my_var}应该是my_var关键en.yml?
我知道我可以通过做类似的事情来实现它
I18n.t 'my_message', :my_var => I18n.t('my_var')
Run Code Online (Sandbox Code Playgroud)
但我希望I18n有办法自我引用键.
有没有办法在yaml中使用占位符,如下所示:
foo: &FOO
<<propname>>:
type: number
default: <<default>>
bar:
- *FOO
propname: "some_prop"
default: "some default"
Run Code Online (Sandbox Code Playgroud) 这个 StackOverflow问题给了我一些关于Rails i18n文件的良好结构的思考,所以我想我会分享另一个结构来重构Rails i18n yml文件供您考虑/批评.
鉴于我想
t('.some_translation')在我的视图中使用简写的"懒惰"查找,并且知道在应用程序中使用翻译的地方,对于config/locales/en.yml文件,看起来像这样:
activerecord:
attributes:
user:
email: Email
name: Name
password: Password
password_confirmation: Confirmation
models:
user: User
users:
fields:
email: Email
name: Name
password: Password
confirmation: Confirmation
sessions:
new:
email: Email
password: Password
Run Code Online (Sandbox Code Playgroud)
我可以看到存在重大的重复,并且诸如"电子邮件"和"密码"之类的词语的上下文是明确的并且在它们各自的视图中具有相同的含义.如果我决定将"电子邮件"更改为"电子邮件",那么必须去更改它们会有点烦人,所以我想重构字符串以引用某种字典.那么,如何使用&这样的锚点将字典哈希添加到文件的顶部:
dictionary:
email: &email Email
name: &name Name
password: &password Password
confirmation: &confirmation Confirmation
activerecord:
attributes:
user:
email: *email
name: *name
password: *password
password_confirmation: *confirmation
models:
user: User
users:
fields:
email: *email
name: *name …Run Code Online (Sandbox Code Playgroud) refactoring yaml structure ruby-on-rails internationalization
在Perl中,我可以执行以下操作:
my $home = "/home";
my $alice = "$home/alice";
Run Code Online (Sandbox Code Playgroud)
我可以在YAML中执行以下操作:
Home: /home
Alice: $Home/alice
Run Code Online (Sandbox Code Playgroud)
那么"爱丽丝" /home/alice到底有效吗?
我在Rails 3应用程序中使用Devise来创建帐户.我有不同类型的用户,因此我想根据用户类型发送自定义密码恢复电子邮件.
我能够发送自定义电子邮件,我还没有找到在该电子邮件上设置自定义标题的方法.我特别感兴趣的是设置电子邮件的主题.
我做了以下事情:
devise_mail.我的自定义邮件看起来像这样:
class AccountMailer < Devise::Mailer
helper :application # gives access to all helpers defined within application_helper.
def reset_partner_instructions(record, opts={})
devise_mail(record, :reset_partner_instructions, opts)
end
end
Run Code Online (Sandbox Code Playgroud)
问题是电子邮件的主题始终是"重置合作伙伴说明".我相信Devise正在从邮件模板的名称生成这个标题.
在本教程https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer中,他们调用以下代码:
def confirmation_instructions(record, opts={})
headers["Custom-header"] = "Bar"
super
end
Run Code Online (Sandbox Code Playgroud)
由于我直接调用"devise_mail",我没有看到如何将邮件传递给邮件程序.我可以使用简单的设置或方法来设置电子邮件主题吗?
有没有办法让我的en.yml文件包含一个常量?
# en.yml
foo:
bar:
I love BAZ so much!
# initializers/constants.rb
BAZ = "stackoverflow.com"
I18n.t("foo.bar")
-> "I love stackoverflow.com so much!"
Run Code Online (Sandbox Code Playgroud)
?
如果没有,有没有办法自我引用yaml文件?
foo:
bar:
I love *baz* so much!
baz:
stackoverflow.com
I18n.t("foo.bar")
-> "I love stackoverflow.com so much!"
Run Code Online (Sandbox Code Playgroud) 我想让一个yaml对象引用另一个,如下所示:
intro: "Hello, dear user."
registration: $intro Thanks for registering!
new_message: $intro You have a new message!
Run Code Online (Sandbox Code Playgroud)
上面的语法只是它可能如何工作的一个例子(它也是它在这个cpan模块中的工作方式.)
我正在使用标准的ruby yaml解析器.
这可能吗?
如何在 YAML 文件中使用环境变量?
我正在使用 kubectl 创建一个命名空间,并想知道如何使用变量而不是testnamespace像name: $var
apiVersion: v1
kind: Namespace
metadata:
name: testnamespace
spec:
finalizers:
- kubernetes
Run Code Online (Sandbox Code Playgroud) yaml ×6
ruby ×2
devise ×1
indirection ×1
kubectl ×1
localization ×1
rails-i18n ×1
refactoring ×1
structure ×1