Funny things and common gotchas to know and avoid - Get on Rails with Globalize! Part 7 of 8
I'm seeing lot's of strange characters!
... also known as: what's with all these quotation marks?
While it'd probably be tempting to quote the classics "The sixties weren't good to you, were they?" (Sarge, Cars) the real I18n superhero knows that you actually have a problem with your character encoding here. What you need to do is get your entire application stack (Ruby, Rails, your database, webserver ...) talk utf-8.
Here are some resources that you might find useful:
- Some common questions on getting started - Get on Rails with Globalize! Part 2 of 6
- Common mistakes with character encodings - part 1
- Server config: Mistakes with character encoding - part 2
Weird Currency parsing results
Beware that the Globalize Currency class is still somewhat biased when it comes to parsing numbers in that it will always use a dot "." as a decimal separator regardless of what's defined as the current Locale.
Also, there's a bug that causes Globalize to parse a String like "1.9" to a Cents value of 109 Cents instead of 190 Cents like one would expect. So beware the results.
Both of these problems are fixed by this patch though (to the best of my knowledge this was first posted on the Rails I18n mailinglist ):
Locale.set('en-US')
p Globalize::Currency.parse('1.90') # => #<Globalize::Currency:0x3768078 @cents=190>
Locale.set('de-DE')
p Globalize::Currency.parse('1,90') # => #<Globalize::Currency:0x3767f10 @cents=190>
Globalize WrongLanguageError on attribute read
When you encounter this error you're probably doing something like this:
p = Page.find(1) # returns an english page
Globalize::Locale.set 'fr-CH' # p is now the 'wrong' language
puts p.title # This throws WrongLanguageError.
This really should be:
Globalize::Locale.set 'fr-CH'
p = Page.find(1) # or: p.reload (if p is already there)
puts p.title
Alternatively, if you have Globalize extensions installed, you can use the switch_language method:
@page.switch_language(base_language.code) { @page.title }
Globalizes screws my RJS (or: my IE6)!
Not really.
But probably you've followed the instructions from Chad Fowlers book "Rails Receipes" (page 268) and adopted the following piece of code:
after_filter :set_charset
def set_charset
unless @headers["Content-Type"] =~ /charset/i
@headers["Content-Type"] ||= ""
@headers["Content-Type"] += "; charset=utf-8"
end
end
end
With this you'll most probably end up delivering broken HTTP headers like this:
Content-Type: ;charset=utf-8
It has been reported that this successfully screws RJS and apparently leaves IE 6.0 choking, too.
You can easily get around this by changing the fourth line to:
@headers["Content-Type"] ||= "text/html"
Another, probably slightly better variant of the set_charset
filter can be
found here: Some common questions on getting started - Get on Rails with Globalize! Part 2 of 6
Others?
Of course this list can't ever be complete. So, again: if you've found any other common gotchas concerning Globalize, please drop me a note!