International characters


#1

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


#2

“Per-Olof” == Per-Olof H. removed_email_address@domain.invalid 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 D. <removed_email_address@domain.invalid>
	 http://www.livejournal.com/users/cdybedahl/
"Just about anything can be done if you are demented enough."
	-- Christopher C. Petro, scary.devil.monastery

#3

Calle D. wrote:

“Per-Olof” == Per-Olof H. removed_email_address@domain.invalid 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 D. <removed_email_address@domain.invalid>
   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


#4

“Per-Olof” == Per-Olof H. removed_email_address@domain.invalid 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 D. <removed_email_address@domain.invalid>
	 http://www.livejournal.com/users/cdybedahl/
         Try again. Try harder. -*-  Fail again. Fail better.

#5

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-mysql-and-rails-to-cooperate/

Magnus


#6

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-mysql-and-rails-to-cooperate/

Magnus

Thanks, I´ll try it,

Per-Olof


#7

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-mysql-and-rails-to-cooperate/

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.


#8

Calle D. wrote:

“Per-Olof” == Per-Olof H. removed_email_address@domain.invalid 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 D. <removed_email_address@domain.invalid>
   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/HowToUseUnicodeStrings and
they take upp some of the issues (strings, sorting etc)


#9

Per-Olof H. wrote:

Calle D. 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


#10

We’re using UTF-8 here and just don’t tell Rails about it. :slight_smile:

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 H.:


#11

Thomas F. wrote:

We’re using UTF-8 here and just don’t tell Rails about it. :slight_smile:

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"?> Förlagets Namn <%= stylesheet_link_tag 'subscribe', :media => 'all' %>
<body>

  <div id='banner'>
    <img src="/images/logo.png" alt="Logga" height="40" width="40" 

border=“5” />
<%= @page_title || “Prenumerationsanmälan” %>

  <div id='columns'>

but not
/views/subscribe/index.rhtml:

<% for product in @products %>

<%=h(product.designation) %>

<%= product.commitment %> <%= product.segment %>

<%= sprintf("%0.2f", product.price) %> Kr
  <%= link_to 'Beställ',
              {:action => 'add_to_cart', :id     => product },
              :class => 'addtocart' %> <br/>
 
<% 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


#12

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


#13

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:

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 F. messsage not to tel “Rails about it” that got me
thinking in the right direction.

Thanks everyone!

Per-Olof


#14

Per-Olof H. wrote:

Per-Olof H. wrote:

Calle D. 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.


#15

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

Per-Olof
Ctrl+Click a file/folder --> Properties --> Text file encoding


#16

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


#17

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