开发者

store utf8 strings in db, but validate and parameterize transliteriated

In my app there's a number of routes like :user_login/resource/resource_name

example:

:vlad/photo_albums/my-first-album

Users should be able to create an albums with their native language namings ( in my case its russian ). But if user named his album as, for example, "Привет, Мир!" ( which is "Hello World!" in English), I want to use a string where all letters of the Russian alphabet are replaced by similar-sounding Latin in a resource link. E.g, user provides album title "Привет Мир!" and the corresponding link looks like 'vlad/photo_albums/privet-mir'.

I've made all necessary methods to transform russian to latin, but now I'm trying to find the best way to arrange all this.

First issue is that I need to find album by it's title:

@album = @user.albums.
        where( :title => params[:album_title] ).first
    redirect_to user_albums_path(@user) unless @album

I would really want to avoid using anything but latin in my sql statements.

Second issue is that I don't want to run validations on non-latin string ( should I be? ) so I want to latinize and parameterize it before validation, but still save the original st开发者_开发知识库ring if it's latinized version passed the validation:

  validates :title, :presence => true, :length => { :within => (2..25) },
            :allow_blank => false, :uniqueness => { :scope => :user_id }

What I was thinking about to accomplish this were hash serialization like {:latin_version => ..., :original_version => ..} or separate yaml file concepts.

I need your thoughts on how to arrange this properly and what would be the most elegant way. Or am I to pedantic about it? Would it be fine to just store/search for/validate/display non-latin characters?


It's completely fine to store, validate and search non-latin characters. Most Ruby on Rails companies that provide multilingual and international versions of their applications use UTF-8 in the application and database layers. UTF-8 can be properly parameterized, displayed and validated in Ruby on Rails and all major browsers so you should not see any issues there. The best way to handle this is to set your database encoding and/or table string encoding to UTF-8 and then your Ruby on Rails encoding in application.rb:

config.encoding = "UTF-8"

If you are using MySQL or Postgres you will probably also want to be explicit about the database encoding in your database.yml file:

development:  
  adapter: mysql2  
  database: development_db  
  user: root  
  password:  
  encoding: utf8 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜