开发者

Difference between t(:str) and t :str in ROR

i am new to ROR.. i am having a doubt in internationalization commands.

in some case we were using <%=t :str_use%>

and in some cases we were using <%= t(:str_use) %>

what is the difference between these two

when should i have to use 1st and when to use the second one..

Pls give some ideas regarding this.

i am having a view file with in that i am 开发者_开发问答having lot of strings i wanna to internationalization them.

in some cases i am having like <td>Use</td>

and in some cases

<% if use %> Use <br />
<% else %>


This is ruby's syntax, not specifically ror ;)

Both are the same. Ruby can guess the parenthesis even if they're not there.
So it's completely up to you.


There's no difference between t :str and t(:str) — they both call the method t with the symbol :str as an argument. In Ruby, parentheses around arguments are optional.

But these are both different from t: str, which is Ruby 1.9 shorthand for the hash {:t => str}.


When to use t(:str)? When you want to chain further methods.

Try this in Rails:

t 'some.translation'.capitalize
t('some.translation').capitalize

First approach will return:

Translation

..as in the second half the identifier after the period, instead of translating the whole text. This, from what I can tell, is because you are passing the argument 'some.translation'.capitalize, not calling capitalize on the return, like the second example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜