Swedish characters problem

Hello,

I’m playing around with Rails and I’m having problems with displaying
swedish characters. This is how far I’ve gotten:

I have a controller…

class MiscController < ApplicationController
def swedish_chars
@chars_to_display = ‘åäö’
end
end

…and a template (swedish_chars.rhtml)

Test with swedish chars

These are swedish chars: å, ä and ö

Now lets try with a variable: <%= @chars_to_display %>

The thing is that the characters in the first paragraph displays nicely
(the charset of the file swedish_chars.rhtml is set to UTF-8). The
problem is with the second paragraph, the one that displays the content
of the controller set variable @chars_to_display.
There are two types of symptoms depending on which charset I use for the
controller file misc_controller.rb. If I use charset 8-bit the swedish
characters are displayed as ?-characters. If I use charset UTF-8 on the
other hand I get this error from Ruby:


SyntaxError

app/controllers/misc_controller.rb:1: Invalid char \357' in expression app/controllers/misc_controller.rb:1: Invalid char\273’ in expression
app/controllers/misc_controller.rb:1: Invalid char `\277’ in expression

RAILS_ROOT: ./script/…/config/…

[—Long stack trace removed—]

This error occured while loading the following files:
misc_controller.rb


What am I doing wrong?

Oh yes: I,m using WEBrick as server.

Best regards

Hans-Eric Grönlund

  1. Are you sure your editor saves the text as utf-8?
  2. Did you tell rails that the source file is utf-8?

Look here
http://www.fngtps.com/2006/01/encoding-in-rails


SyntaxError

app/controllers/misc_controller.rb:1: Invalid char \357' in expression app/controllers/misc_controller.rb:1: Invalid char \273’ in expression
app/controllers/misc_controller.rb:1: Invalid char `\277’ in expression

Thanks for the reply!

  1. Are you sure your editor saves the text as utf-8?
    Yes
  1. Did you tell rails that the source file is utf-8?
    I’m not sure how to do that.

Encoding in Rails
Thank you, this link helped me understanding the problem better.

I discovered that there were two kinds of utf-8 encodings to choose
between in my editor. When I used the second one: utf-8 cookie, the
problem disapeared. My guess is that the editor save some magic bytes in
the ‘pure’ utf-8 file format, and that ruby chokes on these magic bytes.
Any way it works like a charm for me now. Thanks for the help.

Best regards

Hans-Eric Grönlund

Hans-Eric Grönlund wrote:

  1. Did you tell rails that the source file is utf-8?
    I’m not sure how to do that.

That’s the $KCODE = ‘UTF8’ part mentioned in the article. Put it in
application.rb for switching ruby to utf-8 source files.

You (end everyone else) should also read these two articles:

http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF

Kasper W. wrote:

Hans-Eric Grönlund wrote:

  1. Did you tell rails that the source file is utf-8?
    I’m not sure how to do that.

That’s the $KCODE = ‘UTF8’ part mentioned in the article. Put it in
application.rb for switching ruby to utf-8 source files.

You (end everyone else) should also read these two articles:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) – Joel on Software
ongoing by Tim Bray · Characters vs. Bytes

I also had lots of problems with swedish characters, and tried several
things suggested on different sites, defingin set_charset=utf-8 in
application.rb etc, etc.

But after many trials Thomas F. pointed out that it is best to “not
tell Rails about it” ie use UTF-8 but not do any modifications.

It seems that the problems lie mainly in the editors one uses. I used
Ultraedit which obviously did something to my files.

Now I have nothing in Rails defining my use of UTF-8, I use RadRails for
editing, making sure that all the folder with underlying files are set
to use text file encoding UTF-8.

As of now I have swedish characters in the rhtml files, in the database
content, and in also sent via variables (like the example above) and it
all works.

The only thing I don´t have is swedish characters in the database table
and fields names, but that is something I don´t usually have anyway. I
also do not have any swedish characters in variable names etc, which is
also something I don´t need, since we do all the “internals” in english
anyway.

So: Best not let Rails know about it, to quote Thomas F…

So far so good…

Per-Olof H.

Do tell please :slight_smile:

Sounds like I need your setup exactly.
Have you got web forms for collection of Swedish characters that work
properly with Oracle?

Bealach