# Swedish characters problem

**URL:** <https://www.ruby-forum.com/t/swedish-characters-problem/58174>\
**Category:** Rails\
**Created:** [April 19, 2006, 9:27am UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174 "2006-04-19T09:27:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [April 19, 2006, 9:27am UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/1 "2006-04-19T09:27:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [April 19, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/2 "2006-04-19T14:37:33Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [April 20, 2006, 9:52am UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/3 "2006-04-20T09:52:01Z")

</div>

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](http://www.fngtps.com/2006/01/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

---

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [April 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/4 "2006-04-20T14:45:09Z")

</div>

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...](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/)**
>
> Ever wonder about that mysterious Content-Type tag? You know, the one you’re supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in…

[http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF](http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF)

---

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [April 20, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/5 "2006-04-20T23:37:23Z")

</div>

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](http://www.joelonsoftware.com/articles/Unicode.html)  
> [ongoing by Tim Bray · Characters vs. Bytes](http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF)

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.

---

<div class="post-metadata">

**Author:** ![Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@Hans-Eric\_GrSSSSnlu](https://www.ruby-forum.com/u/Hans-Eric_GrSSSSnlu)\
**Post date:** [July 23, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/swedish-characters-problem/58174/6 "2006-07-23T23:23:43Z")

</div>

Do tell please 🙂

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

Bealach
