0

I have script that write log messages like this:

<i18n param1="value1" param2="value2">translation_template</i18n>

Parameters amount and names may be various. Message, parameters names and values I gets using regular expression, after that I have data

{
    message: 'translation_template'
    param1: 'value1'
    param2: 'value2'
}

And I want translate that messages using Ruby on Rails internalization. Method i18n.t can pass parameters and use them in translation:

t 'translation_template', param1: 'value1', param2: 'value2'

If localization file has row

some_lang:
  translation_template: "Translated %{param1} is %{param2}"

User will see Translated value1 is value2

But this syntax implies a certain number of parameters.

How to pass previously unknown parameters set? For example, via hash, something like this:

t 'translation_template', { 'param1': 'value1', 'param2': 'value2'}
General Failure
  • 2,421
  • 4
  • 23
  • 49

1 Answers1

0

Ruby on Rails internalization already can pass parameters in hash.

But somehow first I could not do it :)

Errors that I have encountered:

  • count parameter must be integer
  • keys must has Symbol type

This code works for me:

value_params = get_params # get parameters method
translation_params = Hash.new
value_params.each do |param|
    name = param.get_name # get parameter name method
    value = param.get_value # get parameter value method
    value = value.to_i if name == 'count' # convert count value to integer
    translation_params.merge!({name.to_sym => value}) # convert parameter name to Symbol
end
t 'translation_template', translation_params
General Failure
  • 2,421
  • 4
  • 23
  • 49