سؤال

Let's say that I have a field witch consist of multiple parts separated by a semicolon (;). For example, this would look like the following:

'range_numeric; 10; 100'
'length_numeric; 5'

The first part will be the key to the translation and any subsequent parts will be interpolation values to be filled into the text. Basically it is passed as a named argument, but I don't want to define keys for each interpolation values and I'm trying to use I18n as following:

en:
 length_numeric: "%{1} digits"
 range_numeric: "%{1} - %{2} digits"

I was trying to pass interpolation values like following:

t(:range_numeric, 10, 100) # returns "%{1} - %{2} digits" 

So, is this possible doing this way? Any suggestions ?

هل كانت مفيدة؟

المحلول 2

I don't know how to interface with rails, but interpolating a string with an array is easy. Use %s for simple string substitution. You can even have finer control by using an appropriate format syntax.

"%s - %s digits" % [10, 100]
# => "10 - 100 digits"

نصائح أخرى

You could convert the array to a hash by the position of the array items:

values = [10, 100]
mapped_values = Hash[(0...values.size).map(&:to_s).map(&:to_sym).zip(values)]
t(:range_numeric, mapped_values)

(The interpolation keys would start with 0 %{0})

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top