Unicode / Arabic Support in Ruby / Rails

Personal Intro (For frequent visitors of this blog only)

Yes, I’m checking out Rails. Mainly to see what ASP.NET MVC is missing. Very initial impressions, A Lot!

Anyway, this post is intended to be found by new visitors who already know a little Ruby, so, no introductions for this time. Maybe a different post.

 

Just to set expectations, there are only 2 lines needed for getting it to work. This post is just to encourage myself to keep writing…

 

Example

So, let’s assume a simple website that ha a single controller “Say” with only one Action “Hello”.

<span style="color: #0000ff">class</span> SayController < ApplicationController
  <span style="color: #0000ff">def</span> hello
    @name = "<span style="color: #8b0000">محمد</span>"
  <span style="color: #0000ff">end</span>
<span style="color: #0000ff">end</span>

The first time you try to add an Arabic word to any Ruby string, you’ll get the error:v

invalid multibyte char (US-ASCII)

syntax error, unexpected $end, expecting keyword_end

@name = "محمد"

To fix this, you need to tell Ruby that this file is written in Unicode.

This is done by adding the underlined text as the “first” line in the file:


<strong><span style="border-bottom: #008000 1px solid; color: #008000"><u># encoding: utf-8</u></span></strong>

<span style="color: #0000ff">class</span> SayController < ApplicationController
  <span style="color: #0000ff">def</span> hello
    @name = "<span style="color: #8b0000">محمد</span>"
  <span style="color: #0000ff">end</span>
<span style="color: #0000ff">end</span>

Note that this works for all strings either you use single quotes or double quotes to construct the strings.

 

Interestingly, you don’t need to do this in views!

Here is how my /app/views/say/hellohtml.erb file looks like:


<span style="color: #0000ff"><</span><span style="color: #800000">h1</span><span style="color: #0000ff">></span>Hello From Rails<span style="color: #0000ff"></</span><span style="color: #800000">h1</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">p</span> <span style="color: #ff0000">style</span>=<span style="color: #0000ff">"direction: rtl"</span><span style="color: #0000ff">></span>
    أهلا بك مرة أخرى يا <span style="background-color: #ffff00; color: black"><%</span>= @name <span style="background-color: #ffff00; color: black">%></span> .
<span style="color: #0000ff"></</span><span style="color: #800000">p</span><span style="color: #0000ff">></span>

You realize of course the editor is not used with RTL support (which i different than the RTL of the rendered HTML, I’m here talking about the mark-up code itself). That’s fine for our example though.

 

Ideally, this should be enough, however, sometimes the Windows OS Console (if developing on Windows like me) gets it wrong and still causes the Arabic text when firing the server to get Arabc text omitted from response.

The solution to this is also another one line:

x:RailsTest> chcp 65001
x:RailsTest> rails server

Every day I find a good article or video, or find some good tech news. Instead of bookmarking these, I am sharing with over 2,500 developers around the world..

Get all the recent articles and videos on AngularJS, JavaScript, Node, CSS, ASP.NET, and other web developer topics.

* = required field