Forum: Ruby on Rails International characters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 00:57
I am new to Ruby on Rails. I have read a lot, and bought several books,
and now I have started to implement a modified "shopping cart",
following the AWDWR-book, with modifications to suit our application
(subscriptions and memberships).

Having done up to chapter 7 I have had no real problems, except one:

Since our customers are Swedish I need the screen names, texts etc to be
in swedish, (the internals are ok with English, which we always have
used to be prepared for business abroad some day).

When I in the .rhtml files write characters å, ä, and ö (or �, �,
Ã?)[these are swedish charactera a a and o, A, A with umlaut characters
above] I get a problem: the show up on the screen as a question mark [?]

Does not UTF-8 take care of this?

How do I adjust the .rhtml files to show Swedish characters?

Thanks in advance!

Per-Olof
9d7d8ef2179661d6b30e180fa588cd45?d=identicon&s=25 Calle Dybedahl (Guest)
on 2006-04-09 08:32
(Received via mailing list)
>>>>> "Per-Olof" == Per-Olof Hermansson <per-olof.hermansson@massmarketing.se> writes:

> When I in the .rhtml files write characters å, ä, and ö (or Å, Ä,
> Ö)[these are swedish charactera a a and o, A, A with umlaut characters
> above] I get a problem: the show up on the screen as a question mark [?]

> Does not UTF-8 take care of this?

You have to make sure that the rhtml files are actually in UTF-8.
--
		     Calle Dybedahl <calle@cyberpomo.com>
		 http://www.livejournal.com/users/cdybedahl/
	"Just about anything can be done if you are demented enough."
		-- Christopher C. Petro, scary.devil.monastery
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 09:35
Calle Dybedahl wrote:
>>>>>> "Per-Olof" == Per-Olof Hermansson <per-olof.hermansson@massmarketing.se> writes:
>
>> When I in the .rhtml files write characters �, �, and � (or �, �,
>> �)[these are swedish charactera a a and o, A, A with umlaut characters
>> above] I get a problem: the show up on the screen as a question mark [?]
>
>> Does not UTF-8 take care of this?
>
> You have to make sure that the rhtml files are actually in UTF-8.
> --
> 		     Calle Dybedahl <calle@cyberpomo.com>
> 		 http://www.livejournal.com/users/cdybedahl/
> 	"Just about anything can be done if you are demented enough."
> 		-- Christopher C. Petro, scary.devil.monastery

I have tried that, but I realize now that I also must configure RoR to
be in "UTF-8 mode", and that there are some complications with this.

Is there any other way?

Per-Olof
9d7d8ef2179661d6b30e180fa588cd45?d=identicon&s=25 Calle Dybedahl (Guest)
on 2006-04-09 09:45
(Received via mailing list)
>>>>> "Per-Olof" == Per-Olof Hermansson <per-olof.hermansson@massmarketing.se> writes:

> I have tried that, but I realize now that I also must configure RoR to
> be in "UTF-8 mode", and that there are some complications with this.

There are? It seems to be working perfectly for me. What complications
are you experiencing?

> Is there any other way?

Make everything Latin-1 instead? Rails itself is fairly
encoding-agnostic, as far as I've seen.
--
		     Calle Dybedahl <calle@cyberpomo.com>
		 http://www.livejournal.com/users/cdybedahl/
             Try again. Try harder. -*-  Fail again. Fail better.
0286a6611fab2ca90a07909d24302250?d=identicon&s=25 magnus Hjelm (Guest)
on 2006-04-09 10:00
(Received via mailing list)
> > Is there any other way?
Hej!

Try putting this snippet in application.rb:

def configure_charsets
  @response.headers["Content-Type"] = "text/html; charset=utf-8"
  suppress(ActiveRecord::StatementInvalid) do
    ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
  end
end

If you want to learn more, This is a good article about Rails and
Unicode
http://ruphus.com/blog/2005/06/23/getting-unicode-...

Magnus
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 10:01
Calle Dybedahl wrote:
>>>>>> "Per-Olof" == Per-Olof Hermansson <per-olof.hermansson@massmarketing.se> writes:
>
>> I have tried that, but I realize now that I also must configure RoR to
>> be in "UTF-8 mode", and that there are some complications with this.
>
> There are? It seems to be working perfectly for me. What complications
> are you experiencing?
>
>> Is there any other way?
>
> Make everything Latin-1 instead? Rails itself is fairly
> encoding-agnostic, as far as I've seen.
> --
> 		     Calle Dybedahl <calle@cyberpomo.com>
> 		 http://www.livejournal.com/users/cdybedahl/
>              Try again. Try harder. -*-  Fail again. Fail better.

I haven´t set the environment.rb to UTF-8 yet, but I read the
Wiki:http://wiki.rubyonrails.com/rails/pages/HowToUseUn... and
they take upp some of the issues (strings, sorting etc)
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 10:02
magnus Hjelm wrote:
>> > Is there any other way?
> Hej!
>
> Try putting this snippet in application.rb:
>
> def configure_charsets
>   @response.headers["Content-Type"] = "text/html; charset=utf-8"
>   suppress(ActiveRecord::StatementInvalid) do
>     ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
>   end
> end
>
> If you want to learn more, This is a good article about Rails and
> Unicode
> http://ruphus.com/blog/2005/06/23/getting-unicode-...
>
> Magnus

Thanks, I´ll try it,

Per-Olof
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 10:10
magnus Hjelm wrote:
>> > Is there any other way?
> Hej!
>
> Try putting this snippet in application.rb:
>
> def configure_charsets
>   @response.headers["Content-Type"] = "text/html; charset=utf-8"
>   suppress(ActiveRecord::StatementInvalid) do
>     ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
>   end
> end
>
> If you want to learn more, This is a good article about Rails and
> Unicode
> http://ruphus.com/blog/2005/06/23/getting-unicode-...
>
> Magnus

This seems only to have changed the content of fields in tables. That
was actually no problem before. I had problems with the .rhtml text, not
the content strings of my database. Still get wrong characters of the
texts i the .rhtml pages.
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 10:11
Per-Olof Hermansson wrote:
> Calle Dybedahl wrote:
>> Make everything Latin-1 instead? Rails itself is fairly
>> encoding-agnostic, as far as I've seen.

That´s what I had from start. Contents are ok, but problems with the
characters in  the .rhtml pages.

Per-Olof
E813e9cc70159e103a64a5237c0d3ee2?d=identicon&s=25 Thomas Fuchs (Guest)
on 2006-04-09 10:15
(Received via mailing list)
We're using UTF-8 here and just don't tell Rails about it. :)

So, we're just moving the character data from the database to the
browser
or vice-versa as "plain bytes".

If you need stuff like sorting/upcase/downcase in Ruby to work
correctly,
you can try using the "unicode" gem as described on the wiki page:
...using the routines from the Unicode library by Yoshida Masato,
available as a gem

gem install unicode
After that the following functions will be available:

Unicode::downcase(string)
Unicode::downcase(string)
Unicode::normalize etc.


-Thomas

Am 09.04.2006 um 10:01 schrieb Per-Olof Hermansson:
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 10:32
Thomas Fuchs wrote:
> We're using UTF-8 here and just don't tell Rails about it. :)
>
As of now I have content from database fields showing ok, but problems
with certaing .rhtml files:

/views/layouts/subcribe.rhtml works fine:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...

<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>Förlagets Namn</title>
    <%= stylesheet_link_tag 'subscribe', :media => 'all' %>
  </head>

	<body>

	  <div id='banner'>
	    <img src="/images/logo.png" alt="Logga" height="40" width="40"
border="5" />
	    <%= @page_title || "Prenumerationsanmälan" %>
	  </div>

	  <div id='columns'>


---
but not
/views/subscribe/index.rhtml:

<% for product in @products %>
  <div class="catalogentry">
      <h3><%=h(product.designation) %></h3>
         <%= product.commitment %>  <%= product.segment %> <br/>
      <br/>
      <span class ="catalogprice"> <%= sprintf("%0.2f", product.price)
%> Kr</span>

      <%= link_to 'Beställ',
                  {:action => 'add_to_cart', :id     => product },
                  :class => 'addtocart' %> <br/>
  </div>
  <div class="separator">&nbsp;</div>
<% end %>
<%= link_to "Visa min beställning", :action => "display_cart" %>

the word 'Beställ' and "Visa min beställning" becomes Best?ll etc.

Per-Olof
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 14:52
I have now reverted to "not telling Rails about it", i.e in
application.rb I don´t have any reference to character sets.

In the layout I have:
 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

Thirdly I made sure to save the index.rhtml (referenced as   <%=
@content_for_layout %> in the layout) with format UTF-8. Obviously my
editor (UltraEdit) saved it as something else by default, and when I
experimented the layout got saved as UTF-8 but not the index.rhtml.

So, my problem solved.

It was Thomas Fuchs messsage not to tel "Rails about it" that got me
thinking in the right direction.

Thanks everyone!

Per-Olof
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 15:24
On the issue of international characters I (as a total newbie) wonder if
the following distinction of internationalization is correct in RoR.

The issue has three levels (and I found it difficult to distinguish
between them in the documentation on the RoR-Wikil

1. rhtml-level, how to get UTF-8 characters to be presented correctly in
a browser (view-level?)

2. database content (i.e the content in the fields of the tables in the
database (controller-level?)

3. database design (meta?) level, i.e the names of tables and fields of
the database (model-level?)

I have solved level one in my installation, and that works for me, I
don´t need table names and fields with other than english characters
since we use english on the database design level.

In the future I might need a solution on level 2,  but I´ll tackle that
when that time comes...

Regards,

Per-Olof
D893e113b51a8f200d2abb3ed9e54143?d=identicon&s=25 Gazoduc (Guest)
on 2006-04-09 16:29
Per-Olof Hermansson wrote:
> Per-Olof Hermansson wrote:
>> Calle Dybedahl wrote:
>>> Make everything Latin-1 instead? Rails itself is fairly
>>> encoding-agnostic, as far as I've seen.
>
> That´s what I had from start. Contents are ok, but problems with the
> characters in  the .rhtml pages.
>
> Per-Olof
Make sure your .rhtml files are encoded in UTF8 and not some latin-1 or
macroman format... This setting is done in your editor. Textedit on mac
has an option to set the encoding when you "save as..." for example.
Textmate has the same.
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-09 18:03
Gazoduc wrote:
> Make sure your .rhtml files are encoded in UTF8 and not some latin-1 or
> macroman format... This setting is done in your editor. Textedit on mac
> has an option to set the encoding when you "save as..." for example.
> Textmate has the same.

Yes, I am learning that the hard way, UltraEdit that I use can set "sav
as...", but then it seam I cannot type in UTF8-mode (I can´t see what I
type). I have posted a question on UltraEdit forum about this, but it
seems that I need to fina another editor.

I tried RadRails, but could not find where to set the encoding type.

Per-Olof
D893e113b51a8f200d2abb3ed9e54143?d=identicon&s=25 Gazoduc (Guest)
on 2006-04-09 18:35
>
> I tried RadRails, but could not find where to set the encoding type.
>
> Per-Olof
Ctrl+Click a file/folder --> Properties --> Text file encoding
3a3e856b665333316075a35f78a7f8a3?d=identicon&s=25 Per-olof Hermansson (perherman)
on 2006-04-10 13:22
Gazoduc wrote:
>>
>> I tried RadRails, but could not find where to set the encoding type.
>>
>> Per-Olof
> Ctrl+Click a file/folder --> Properties --> Text file encoding

Thanks,
I tried it and it works fine. Also found where to set it in UltraEdit.
But from what i have seen in Radrails I will use Radrails for working
with RoR, and keep UltraEdit for working with large textfiles.

Per-Olof
This topic is locked and can not be replied to.