A Study on the Issue of Chinese Character in Ruby on Rails and Relevant Solving Scheme

As Ruby on rails springs out in China, it wins greater attentions from developers at once. Focusing on the Chinese character issue appeared in the Ruby on rails development frame, this paper puts forward specific solving scheme. 1. Introduction Although Ruby has already emerged for a period of time, it has not won popular except in Japan before the year 2000 because it does not have the English file. Since the appearance of Rails frame in 2000, the Ruby has become popular in Web development. The Rails is a frame edited by pure Ruby. It serves as a powerful support for Web development, such as data reflection, MVC mode, Web services, and safety. And these functions can be realized easier than similar products. But the development of Ruby on rails is still at its first step in China. Up till now, there is still no perfect scheme for solving the Chinese character issue. It is a critical weakness for domestic programmers who are engaged in developing Chinese interface.


Introduction
Although Ruby has already emerged for a period of time, it has not won popular except in Japan before the year 2000 because it does not have the English file.Since the appearance of Rails frame in 2000, the Ruby has become popular in Web development.The Rails is a frame edited by pure Ruby.It serves as a powerful support for Web development, such as data reflection, MVC mode, Web services, and safety.And these functions can be realized easier than similar products.But the development of Ruby on rails is still at its first step in China.Up till now, there is still no perfect scheme for solving the Chinese character issue.It is a critical weakness for domestic programmers who are engaged in developing Chinese interface.
Since we have already determined to use Ruby on rails to finish some projects, we should try our best to achieve this goal.As a matter of fact, most programmers are persistent and never give up.Therefore, focusing on the Chinese character issue emerged in program development, this paper puts forward specific solving scheme, with the hope of being helpful references for all programmers.

The code of rails
As the base of rails, although the ruby language supports the Unicode character string, its class libraries do not support the Unicode.In other words, in dealing with one Chinese character, Ruby fails to identify that two bytes under the Unicode format mean one character.

The relationship between utf8 and Unicode
The utf8 means Unicode Translation Format.Unicode has many different formats, such as utf8 and utf16, in transmission.In fact, the utf8 is not a friendly format for Chinese character.Why?In the utf8, one Chinese character has three bytes.But one English ascii character has one bytes.As a result, Chinese characters will occupy a large space and their numbers can not be deduced merely by their bytes.In contrast, the utf16 is better.Each character has two bytes.However, the utf16 and even the utf32 are seldom used.The utf8 is generally recognized.

Converse the code
If we require for the 'iconv' database, we can do: Iconv.conv ("utf8", "GBK", "……").Turn the characters under the GBK mode into the ones under the utf8 mode.As the source characters have illegal code, we ask iconv to neglect it.Otherwise, iconv will order to stop.

The Chinese characters under the utf8 mode
Calculate the number of the characters.

The Chinese character issue in the rails database
Under the common situation, change the MySQL file C:\Program Files\MySQL\MySQL Server 5.0\my.ini.Make two changes.default-character-set=utf8.Then restart MySQL (windows service).This step can also be realized by instance wizard.By this way, the Chinese character issue existed in most rails databases can be solved.But there are still some special situations.
As database mysql uses utf-8 to store Chinese characters, the SQLyog Enterprise shows a series of random codes.Because the Chinese characters are turned into utf8 mode as we store them.And they are still in this mode as they are on the screen.At this time, we can use this order as follow.
Set names 'gbk'; Here, Chinese characters are changed into gbk mode from utf8 mode.We can read the Chinese characters at last.
For another instance, if we set source file code as UTF-8 mode, the database content is normal in web pages.But if we require the database content by phpMyAdmin and mysql-front, it shows random code.And under the control platform mode, the database content still shows random code.In practice, we find the solving method as follow.
(1) For MySql: set the character as utf8 mode

The random code issue in uploading and downloading files in rails
For Chinese users, the default code of files is gbk.In other words, the file name in utf8 mode is random code for us.That is why as we upload or download files, we usually come across random code.Therefore, we should converse the random code.
Generally speaking, ruby is not good at providing with support for Unicode, which in a sense blocks the development.Therefore, this issue has to turn to other tools.In general, we are merely familiar with the utf8 mode and the gbk mode.As long as the database, connection way, source code, and page files are in utf8 mode, the work will become easy.But recently as we download files, we still find the random code issue.Then we can use the additional database Iconv in ruby.It is simple.

Iconv. new (to, from)
It is a class method.Generate and back from to the new converter.To and from respectively refers to the after-conversion character and the before-conversion character.

Iconv#iconv(str)
Start to converse characters and return the result.
Firstly, we define two global variable in environment.rbUTF8_TO_GBK = Iconv.new"gbk", "utf-8" GBK_TO_UTF8 = Iconv.new"utf-8", "gbk" Then as we download files, we use "send_file".The file name can be shown in Chinese character.But we still have to turn the utf8 mode into the gbk mode.We use: :filename => UTF8_TO_GBK.iconv(filename) As a result, it shows Chinese character.As far as uploading files with Chinese names is concerned, the main problem is the random code of local files stored in the service.We have to ensure that the local files are in GBK mode as we read or store them.

Conclusion
Here this paper puts forward a way of solving the Chinese character issue in programming by rails.Comparing with JAVA, Ruby on rails is a new frame and it is still immature.Especially in China, it is still on its initial stage and few articles can serve as valuable reference.As a matter of fact, the Chinese character issue in rails is originated from the unpopularity of Chinese and the lag-behind technology of China.Rails and ruby have taken references from java in many ways.But they sustain their own special advantages.It is believed that more and more people will use rails to write web programs.