Question

How do you pass a variable or argument to the gritter Ruby on Rails gem so that it can be used as part of internationalisation (i18n) replacement?

You can hard code gflash in the controller like so:

butter = 'melted'
gflash :notice => "The knob of butter is #{butter}"

In the above, #{butter} is replaced with "melted".

But that leaves the translation set in the controller, ignoring the use of the gflash.en.yml file, and at one language.

Testing with variants on how to call the variable seems to result in no substitution.

en:
  gflash:
    titles:
      notice: "Notice"
    accounts:
      login:
        notice: "@butter     {butter}    $butter      #butter     %butter       
                 %{@butter}  %{#@butter} %#{@butter}  %#{butter}  %s
                 %@butter    %{#butter}  #{@butter}   #@butter    {#@butter}   
                 %{butter}   #{butter}   ${butter}    @{butter}"

Not one of the above keys in the gflash.en.yml file display "melted" and instead just show as-is.

Trying to pass it like so also will not work:

gflash :notice => {:butter => 'melted'}

Is there a way to pass variables to the translation keys for the gritter gem to use instead of being limited to hard code one language in the controller?

Was it helpful?

Solution

It doesn't seem that it allows any variables: https://github.com/RobinBrouwer/gritter/blob/master/lib/gritter/gflash.rb#L48

I guess you can do something like this then:

gflash :notice => t("gflash.#{params[:controller]}.#{params[:action]}.notice", butter: butter)

But the best way would be to create an GH issue (with pull request preferably).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top