How to escape colon : in rails i18n? - ruby-on-rails

Here is a line in the view:
<%= f.input :comm_date, :label => "Hello:", :as => :string %>
The regular i18n file has the format as:
Hello : 'Aloha'
In i18n yml file, we want to translate the "Hello:" as, for example, "Aloha:". Can we escape the colon : by doing below?
'Hello:' : 'Aloha:'
Or what's the right way to escape a colon?

The gem you are using, simple_form, as well as other similar ones such as formtastic, allow for using translation files (e.g. en.yml) for defining your label text.
You can see the translation file expectations from the simple_form documentation. Using your example, you'd need two translations, something like this, assuming your model is called Communication:
en.yml
en:
simple_form:
labels:
communication:
comm_date: "Hello:"
olelo.yml
olelo:
simple_form:
labels:
communication:
comm_date: "Aloha:"
And your view would simply be
<%= f.input :comm_date, :as => :string %>
No need to provide a value for the :label option.

Here is what we did:
'Hello:' : 'Aloah'
Just use single quote and it worked.

Related

Suppress square brackets in input names generated by simple_form

I'm working on a bookmarkable search form - with simple_form in order to get access to some custom inputs. Since no real model object is connected, I use the :q symbol to fake one:
= simple_form_for :q, url: projects_path, method: :get do |f|
= f.input :area_id,
as: :select,
collection: (...)
= f.input :description,
as: :geocomplete
While this works, the naming conventions produce in not so nice URLs such as:
...?q[area_id]=16&q[description]=Paris&q[lng]=4.123&q[lat]=30.123
Is there a way to tell simple_form to suppress the fake :q object and produce URLs like:
...?area_id=16&description=Paris&lng=4.123&lat=30.123
Thanks for your hints!
Like #Martin wrote, it can't be done. So it's either standard form helpers or accept the square brackets. I've chosen the latter.

Passing a variable to the i18n translations in simple_form

I need to pass in a variable to generate the label of a field with simple form. With normal translations you go about this in the following way: http://guides.rubyonrails.org/i18n.html#passing-variables-to-translations but for the life of me I am unable to get it to work with simple form.
I am attempting to pass it in using:
= f.input :name, :contact_type => f.object.contact_type.to_s
And in the simple_form.en.yml file:
en:
simple_form:
labels:
contacts:
name: "Name %{contact_type}"
This is outputting:
Name %{contact_type}"
Ignoring the variable all together. Is there a way to accomplish this?
Thanks,
Ryan Lundie
You need to explicitly add the label to the form input like this :
= f.input :name,
:contact_type => f.object.contact_type.to_s,
:label => t(:'simple_form.labels.contacts.name', :contact_type => "Whatever contact type")

Simple Form - Translating options of a input field

I'm trying to use the i18n feature of simple form, which works great in most cases.
My only problem is, that in one case I want to use numbers as option values, so I can not simply create a symbol like in the other cases. Right now, I'm using this solution:
f.input :adm, :as => :select, :collection => [[:adm11 ,"11"],
[:adm00, "00"], [:adm06, "06"], [:adm99, "99"]]
Can I somehow make simple_form look up adm11 and so on in the usual way, so I can keep a sensible structure in my translation file?
I know I could do it with standard ruby i18n, but I'm looking for a better way.
f.input :adm,
:collection => [[:adm11 ,"11"], [:adm00, "00"], [:adm06, "06"],
[:adm99, "99"]],
:label_method => lambda { |el| t "define.i18n.keys.here.#{el.first}" }
I think you can't do it because of this line in SimpleForm:
collection_translated = translate_collection if collection_classes == [Symbol]
So it means that SimpleForm translates options if it's array of symbols. See discussion here https://github.com/plataformatec/simple_form/pull/302

Add a link to a label/hint in formtastic with haml

I'm trying to add a link to a checkbox in formtastic, using haml. It looks like formtastic removes all ruby/haml/html syntax from the text for the label or hint when it parses it.
That is, this doesn't work:
= f.input :terms, label: '=link_to \'Here\', path_to_link', hint: '=link_to \'Other Place\', path_to_other_link', as: :boolean
Is there anything that will other than writing another div outside of this input? Or am I going about this all wrong? I'm a rails/haml/formtastic noob.
if you use ' for string quotation, you're basically telling ruby not to parse that content.
try something like this instead:
= f.input :terms, label: link_to('Here', path_to_link), hint: link_to('Other place', path_to_other_link), as: :boolean
Formtastic::FormBuilder.escape_html_entities_in_hints_and_labels = false
In your formtastic initializer
If you mark the string passed to label: or hint: as HTML safe then Haml doesn't escape it.
= f.input :terms,
label: "Please accept our ".html_safe + link_to('Terms and Conditions', path_to_link)
19 ways tried, with either the hyperlink being encoded or html_safe replacing hyphens in the url ???
This is what worked for me
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy', 'http://www.your-­url.co.uk/privacypolicy')}".html_safe
end %>
The specific use of " and ' appears significant.

Forcing "humanized" names of fields to be lowercase in Rails 3

As far as I know, the accepted way to set the "humanized" names of fields in Rails 3 is to use locales:
# config/locales/en.yml
en:
activerecord:
attributes:
member:
username: 'username' # rather than 'Username'
However, I simply want Rails 3 to use lowercase versions of its default humanized names. Is there an easy, built-in way to do this?
An example to help clarify: When inside of a form_for, <%= f.label :username %> displays "Username" by default. I want it to display "username".
I had the same problem.
I solved it via css:
In foo.html.erb:
<%= f.label :username, :class => "my_class" %>
In bar.css:
label.my_class {
text-transform: lowercase;
}
I would prefer a different solution, too. But that's the only one I've been able to find, so far.
The label helper defaults to using human_attribute_name to turn an attribute into a human name. If you look at the source, human_attribute_name tries a few things before falling back to attributes.to_s.humanize. It tries the translation first, and then it looks for a :default option in the options hash.
So, the simplest/best way to get the functionality you want is to override human_attribute_name with your own that provides a :default option and then calls the original. Rails provides a reasonable way to do this sort of thing with alias_method_chain, so...
I've heard enough, just give me the answer!
Put the following in any file in config/initializers and restart your app:
module ActiveModel
module Translation
def human_attribute_name_with_foo attribute, options = {}
human_attribute_name_without_foo attribute, options.merge( :default => attribute.humanize.downcase )
end
alias_method_chain :human_attribute_name, :foo
end
end

Resources