How can change the text that will_paginate shows?
Right now, it renders Previous ... Next. I need to put that in french Précédent ... Suivant. I checked on google and got this link : http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3
However, I was wondering if there was an easier way.
You can override the default of Previous and Next this way:
<%= will_paginate #posts, :previous_label => 'Précédent', :next_label => 'Suivant' %>
Note: :previous_label was called :prev_label in versions 2.3.2 and older
So you have to ensure that the following is in place:
Your locale is set to french. This normally depends on the browser your are using. Chrome e.g. uses the locale of the operating system. I have set in application.rb the default locale (german for me): config.i18n.default_locale = :de
The directory config/locales contains a file fr.yml with the following content (there could be more customization):
views:
pagination:
first: "F"
previous: "« Prev"
next: "Next »"
last: "L"
truncate: "..."
This works for me in the current version of Rails 3.2.2 with gem 'will_paginate', '>= 3.0'.
Related
Per the Rails 3.2 API Docs, to use different locales for number_to_currency, I need to do the following:
<%= number_to_currency(1234567890.506, :locale => :fr) %>
I was expecting the following output:
# => 1 234 567 890,51 €
Even though I literally use that exact thing within my app and it keeps outputting the following:
$1,234,567,890.51
When I check for the available_locales within my app I get the following:
> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN"]
So it SHOULD work, but it doesn't.
What am I missing?
Update 1
Per #s3tjan's comment, I did some digging in that linked Rails issue and that led me to my application.rb where I discovered I18n.enforce_available_locales = false. I changed that to true and restarted the server.
When I tried the above again, I am now getting this error:
ActionView::Template::Error (:fr is not a valid locale):
Not sure how to fix this.
Update 2
So I just realize that I never had a locale file in my config/locales. What I really want is to use the GBP Pounds for currency, so I added an en-GB.yml file in my config/locales, then I restarted my server and console.
In my application.rb, I have the following:
I18n.enforce_available_locales = true
Then I checked my console and got this:
[1] pry(main)> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN", :"en-GB"]
[2] pry(main)>
So the :"en-GB" was added successfully to my app's load path.
But when I do this in my view:
<%= number_to_currency(1234567890.506, :locale => :"en-GB") %>
This is the error I get:
:"en-GB" is not a valid locale excluded from capture due to environment or should_capture callback
ActionView::Template::Error (:"en-GB" is not a valid locale):
So still not working.
Update 3
My en-GB.yml file was taken directly from https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-GB.yml
So it looks exactly like that. Yet I am still getting the same error:
ActionView::Template::Error (:"en-GB" is not a valid locale):
Synopsis:
Remove custom language ymls and add the correct version of the i18n-rails gem. This resolved this special issue.
Original answer:
Ok my guess is that your en-GB.yml is empty. So it actually finds the file and adds the locale in I18n.available_locales BUT this does not include that all translations are available.
When you look at the format of such a yml file you will recognize they all start with
---
language-code
some_keys: ...
This is what actually is loaded into memory and therefore provides all the available translations. Available locale is just defined by found files in config/locales.
When you check the source of number_to_currency It takes the locale from the options and passes it along the key it looks for to I18n.
I18n.translate(:'number.format', :locale => options[:locale], :default => {})
Since you just say that en-GB is available but don't have the actual keys along the locale in memory you get the missing translation issue.
What I suggest is you either use the content of the linked yml file and paste it into your en-GB.yml or you remove your en-GB.yml and find a 3.2 working i18n-rails version and use it. i18n-rails provides plenty of default translations which are utilized all over default rails.
Addition:
Before you added the en-GB.yml file it actually worked like expected.
When no locale is found it defaults to dollar in here since the currency variable will be just and empty {}.
Somehow, I found the solution that worked for me.
First of all, you need to have a locale file with your requirements in it.
Here is the example of fr.yml file
For an instance, copy and paste this file in app/config/locales/
then restart your console,
then try, number_to_currency(1000.51, locale: :fr)
for sure, you will get '1 000,51 €'
Here is the full list of all supported countries' locale file.
Until and unless you don't have locale file with your format required in it, you won't get the desired result.
my rails version is 3.2.22.5, ruby is 2.4.2(I didnt install below 2.x.x)
I use locale yml from
https://github.com/svenfuchs/rails-i18n/blob/rails-3-x/rails/locale/en-GB.yml
This is work well
<%= number_to_currency(1234567890.506, :locale => :"en-GB") %>
<%= number_to_currency(1234567890.506, :locale => "en-GB") %>
to result
£1,234,567,890.51
and I add fr.yml too like
fr:
...
number:
currency:
format:
...
unit: €
And then this is work too
<%= number_to_currency(1234567890.506, :locale => :fr) %>
to result
€1,234,567,890.51
I didn't change or add configuration. I add only controller, view and locale file. And test it.
You could translate directly like, so test this
I18n.translate(:'number.currency.format', :locale => "en-GB", :default => {})
if it occur a same error, then check out you file's name, extension, path.
and you must restart server
I have the following configured in my application.rb
config.i18n.available_locales = [:at, :de, :ch_de, :ch_fr, :fr, :int_en, :int_fr]
config.i18n.default_locale = :at
My default locale is set to :at (Austria). Which I require for Route Translation. Rails server won't start without it and to be fair it makes sense.
I now created a fallback map, which works just fine:
config.i18n.fallbacks = {'de' => 'at', 'ch_de' => 'at', 'ch_fr' => 'fr', 'int_fr' => 'fr', 'fr' => 'fr', 'int_en' => 'int_en'}
So basically I want all German speaking countries to fallback on :at, whilst all French speaking countries fall back to :fr.
However, I do NOT under any circumstances want :fr to fallback on :at. This is for SEO purposes as some french pages do not have metadata configured. So now the french pages would display the Austrian :at metadata entry. Which is wrong.
If I turn of fallbacks completely:
config.i18n.fallbacks = false
the following works fine in my views:
t('.metatitle', :default => "")
In that case if there is no translation available then nothing is displayed. However, the rest of the site that already exists relies on fallbacks - so this is not an option, considering the effort to implement the change.
Is there a way turn off fallbacks for individual translations?
Or can I implement the fallback map and make sure that the map doesn't fallback to it's default locale if for example no french :fr translation exists?
PS: The route translating gem that requires the default locale is this one here.
Thank you for your help !
Figured it out - and thought of sharing it with you:
If you wish to avoid fallback to the default locale on individual translation you simply have to send a empty fallback array like this:
t('.metatitle', :default => "", :fallback => [])
Et Voila !
This is tricky in Rails up to 6.1 because you need to beat the logic in the Railtie initializer which desperately wants to fallback to the default_locale.
To set the default fallback locale to nil you need to use the following code:
config.i18n.default_locale = "de-AT"
config.i18n.fallbacks.defaults = [[]] # If you just write [], then default_locale is used
config.i18n.fallbacks.map = {
:de => "de-AT",
"de-CH" => "de-AT",
}
Let's check:
$ rails console
2.7.2 :001 > I18n.fallbacks["de"]
=> [:de, :"de-AT"]
2.7.2 :002 > I18n.fallbacks["fr"]
=> [:fr]
2.7.2 :003 > I18n.fallbacks["de-CH"]
=> [:"de-CH", :de, :"de-AT"]
2.7.2 :004 > I18n.fallbacks["de-AT"]
=> [:"de-AT", :de]
2.7.2 :005 >
Not 100% what you want, but there just seems to be no way to prevent the fallbacks to go from country specific locale to generic language locale, when fallbacks are enabled.
Note #1: Your locales are a bit non-standard. AFAIK there is no 'at' locale, but only "de-AT".
Note #2: Some more subtleties and notes in this answer.
I installed the Kaminari gem and followed the instructions. I customized my theme to the foundations but when trying to further customize the pagination style using the en.yml file, I run into this error:
I18n::InvalidLocaleData in Campaigns#index
can not load translations from /myapp/config/locales/en.yml: #<Psych::SyntaxError: (/myapp/config/locales/en.yml): did not find expected key while parsing a block mapping at line 23 column 3>
Extracted source (around line #2):
1 <li>
2 <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
3 </li>
I simply copied and pasted what was in the docs..
en:
hello: "Hello world"
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"
If I delete everything other than pagination: line, the error still remains. I'm not super familiar with the locale file in Rails as well as YAML files so any help is highly appreciated.
According to the results from YAML Lint, it appears that the hello: "Hello world" line is invalid.
Also, it looks like the views.pagination.first line in your code is looking for a path like: views -> pagination -> first in your YAML file (and because this pattern can't be found, there's another error).
Therefore, it might be better to use this kind of YAML example:
en:
views:
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"
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
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" %>