2011-06-30 94 views
10

有沒有辦法做這樣的事情:如何在I18n語言環境中執行插值?

en: 
    welcome: 
    hello there, #{current_user.first_name}! It's nice to see you again. 

這顯然是行不通的,而且顯然「#{」是YAML無效字符,因爲該字符串顯示爲只是「你好」當我把它拉出來。

我能做的最好是這樣的:

en: 
    welcome: 
    hello there, (name)! It's nice to see you again. 

.... 

t(:welcome).gsub("(name)", current_user.first_name) 

但我不是瘋了有關......必須有一個更好的方式做這樣的事情。

回答

13

更換你en.yml這樣

en: 
    welcome: 
    "hello there, %{name}! It's nice to see you again." 

和您的看法是這樣

<%=t(:welcome, :name=> current_user.first_name) %> 

基本上它是作爲命名的參數傳遞。您可以在Rails Guides 18n Interpolation

相關問題