rails check_box value using translation I18n - ruby-on-rails

just wondering how I should do internationalization in rails when using a checkbox in a normal (very simple) form;
This is what I tried:
view/form file:
<%= f.check_box :do_you_agree, {}, t('submissions.yes'), t('submissions.no') %>
<%= f.check_box :do_you_agree, {}, I18n.t('submissions.yes'), I18n.t('submissions.no') %>
<%= f.check_box :do_you_agree, {}, "#{t('submissions.yes')}", "#{t('submissions.no')}" %>
...and all them return:
translation_missing in HTML.
When I use :
<%= f.check_box :do_you_agree, {}, 'NO', 'YES' %>
...everything is ok!
The YML file is ok. Many thanks.

In your config/application.rb you must have the line config.i18n.default_locale = :es (:es for spanish, as an example)
And the yml must be like this:
es:
submissions:
'yes': Si
'no': No
Edit: Make the yes/no keys explicit strings. And it will works.
Why? Because Rails does yaml conversion with psych gem. And psych return true and false with all those values (see the links).
Then, if you define a locales this way:
es:
submissions:
yes: Si
no: No
at the rails console you get:
irb> I18n.t('submissions')
=> {true=>"Si",false=>"No"}
irb> I18n.t('submissions')[true]
=> "Si"
irb> I18n.t('submissions.true')
=> "translation missing: es.submissions.true"

Related

RAILS: undefined method `map' caused by missing I18n translation

I changed the format for a datetime field in a RAILS 4 new.html.erb from :string to datetime and it caused error as below:
undefined method `map' for "translation missing: zh-CN.date.order":String
The view code causing the error above is:
<%= f.input :start_time, :label => t("Start Time"), required: true, :as => :datetime, :ampm => true, :minute_step => 10, :start_year => Date.today.year - 1, :end_year => Date.today.year + 1, :format => 'YYYY/MM/DD/HH/MM', :use_month_numbers => true, :include_blank => true %>
The RAILS source code blows up is in actionview/helpers/date_helper.rb:
def translated_date_order
date_order = I18n.translate(:'date.order', :locale => #options[:locale], :default => [])
date_order = date_order.map { |element| element.to_sym } #<<<<<<===blows up
forbidden_elements = date_order - [:year, :month, :day]
if forbidden_elements.any?
raise StandardError,
"#{#options[:locale]}.date.order only accepts :year, :month and :day"
end
date_order
end
I do have a file zh-CN.yml under /config/locale/ and it is providing translations for others except this one.
UPDATE portion of zh-CN.yml:
zh-CN:
#maint_recordx
Mfg Batches : '订单批次一览'
New Batch : '新批次'
Update Batch : '更新批次'
Edit Batch : '更新批次'
...........
After being bitten by this same error, I found that Rails sets the following key:
:'date.order'
to the value:
["year", "month", "day"]
for the default :en locale
You can confirm this by running the following snippet in rails console for a default rails install:
date_order = I18n.translate(:'date.order', :locale => :en, :default => [])
Notice I just switched #options[:locale] for the default :en value
The rails helper you reference, expects an array for the date_order value, and will blow up if it doesn't get one.
In my case, I improperly configured the I18n::Backend::ActiveRecord gem and therefore it interfered with the value being returned by I18n. You probably have a similar issue preventing the correct value for the :'date.order' key being returned.
EDIT:
In order to fix this, you should probably just need to install the gem 'rails-i18n'. It will handle returning the correct date formats for supported locales. In my case I had a custom configuration in my es.yml locale file that returned an incorrect date format.
Bingo !!! You just have to add the missing key translation to your local translations.
I solved it by adding
en:
date:
order: ["day", "month", "year"]
to
config/locales/en.yml

How to use disable option with country_select in a ruby on rails form

Using a normal select field in a ruby on rails form, you can disable it as follows:
f.select :place_type, %w(Club Range Other), {}, disabled: true
But if you try this using the country_select gem i.e.
f.country_select :country, priority_countries: ['AU','US','GB','CA','ZA'], {}, disabled: true
it throws an exception with a message syntax error, unexpected ','.
What is the correct way to do this?
use this code:
<%= f.country_select :country, { priority_countries: ['AU','US','GB','CA','ZA'] }, {}, {:disabled => true}%>
According to the official country_select gem documentation, try that:
country_select(:country, { priority_countries: ['AU','US','GB','CA','ZA'] }, { disabled: 'disabled' })

Spree deface override tutorial

I am following the Spree deface overrides developer guide: http://guides.spreecommerce.com/developer/deface_overrides_tutorial.html
My code matches their's exactly, but I keep getting this error. I looked all around but I didn't see anyone else having this problem or anything similar at all:
undefined method `content_tag' for Spree:Module
I am running Rails 4.0.2 and ruby 1.9.3 (it's possible that the tutorial wasn't updated for rails 4?)
here's my code:
app/overrides/add_sale_price_to_product_edit.rb
Deface::Override.new(:virtual_path => 'spree/admin/products/_form',
:name => 'add_sale_price_to_product_edit',
:insert_after => "erb[loud]:contains('text_field :price')",
:text => "
<%= f.field_container :sale_price do %>
<%= f.label :sale_price, raw(Spree.t(:sale_price)) %><span>*</span>
<%= f.text_field :sale_price, :value =>
number_to_currency(#product.sale_price, :unit => '') %>
<%= f.error_message_on :sale_price %>
<% end %>
")
app/models/spree/product_decorator.rb
module Spree
Product.class_eval do
delegate_belongs_to :master, :sale_price
end
end
You're getting the error because the translation for Spree.t(:sale_price) is not specified. This is failing because Rails 4.0.2 made some changes to the I18n API . You have a few choices.
Add a translation for the missing tag and remember that this content_tag issue is caused by this crazy bug.
Downgrade to Rails 4.0.0 (not recommended)
Upgrade spree to the 2-1-stable branch (or wait until 2.1.4 is released)
Apply this change to your local Spree installation. It should correct this issue.
Any of those should get you working again.

Translation of "will_paginate" doesn't work. What am I doing wrong?

The basic scheme:
= will_paginate #products, :previous_label => t("previous_label"), :next_label => t("next_label")
de.yml
will_paginate:
page_gap: "…"
previous_label: "word for back"
next_label: "word for next"
en.yml
en:
will_paginate:
page_gap: "…"
previous_label: "previous"
next_label: "next"
But in the output are still the labels called Previous Label and Next Label.
What is still wrong? Also, I thought I didn't restart the server... but after restart still the same labels, not my translations
You can grab YAML files translated in various languages for will_paginate here : https://github.com/tigrish/will-paginate-i18n.
In your example, you're overriding the :previous_label and the :next_label, but you're not scoping it to 'will_paginate'.
Either remove the overrides completely and customise the labels in your translation file :
will_paginate #products
or scope the .t calls correctly :
will_paginate #products,
:previous_label => t("will_paginate.previous_label"),
:next_label => t("will_paginate.next_label")
you can then change the text of the pagination links by adding the following to config/locales/will_paginate.en.yml:
en:
will_paginate:
page_gap: "…"
previous_label: "previous"
next_label: "next"
And add following in application.rb
config.i18n.default_locale = :en

Rails: using the calendar_date_select gem

Environment: Rails 3.2.3, ruby 1.9.3-p125
My gemfile has the following in it:
gem 'calendar_date_select'
my layouts/application.html.erb has the following statement (as the last statement in the head section):
<%= calendar_date_select_includes %>
I installed the gem via gem install calendar_date_select
and ran rake bundle:install
I read the documentation, and some of the responses on StackOverflow, and here's what I ended up putting in my view:
<%= form_for(#panel) do |f| %>
.......
Start Date/Time: <%= f.calendar_date_select :start_time, :iso_date => true %>
I am getting the following error message:
undefined method `calendar_date_select' for #<#:0x007fae3b136938>
Any ideas?
I've switched from using that gem to just using jqueryUI which has the same functionality.
To do that:
view
= f.text_field :content_date, id: 'datepicker', size:10
= f.time_select :content_date, :ignore_date => true # Added later. not shown in screenshot
Gemfile
# Nothing - jquery & jqueryUI are now the default.
# Check web pages to see what really gets included (in the header).
application.js
%script
# The main code
$(function() {
$( "#datepicker" ).datepicker();
});
# This makes the pop-up show the actual db date by fixing a format difference.
$(function (){
var dateInput = $("#datepicker");
var format = 'yy-mm-dd';
dateInput.datepicker({dateFormat: format});
dateInput.datepicker('setDate', $.datepicker.parseDate(format, dateInput.val()));
});
Use this code to enable time in calender select tag
<%= f.calendar_date_select "date", :time => true , :readonly=>true, :popup=>"force" %>

Resources