[Original] improved rails to support gb2312 Encoding

Source: Internet
Author: User

BenArticleThe environment is rails1.2.3, MySQL 5, and utf8 is used for database encoding.

 

For the first step, refer to [reprinted] solutions for rails using gb2312/GBK to coexist with utf8

 

Step 2: Modify the Web Front-end and perform GBK transcoding for Chinese Characters in Params before the action is executed. It mainly works in APP/controller/application. RB.

Code:

Code
Before_filter: encode_request_params

#If the parameter is UTF-8 encoded, GBK transcoding is performed on the parameter.
DefEncode_request_params
IfRequest. env ["Http_content_type"]= ~ /UTF-8/
Encode_hash (Params)
End
End

# Traverse Params and perform GBK transcoding for all string parameters.
Def Encode_hash (hash)
Hash. Each do | K, V |
Logger.info v. Class
If V. instance_of? Hashwithindifferentaccess
Encode_hash (V)
Elsif v. instance_of? String
Hash [k] = GBK (V)
End
End

Hash
End

Step 3: Modify actionview: Helpers: instancetag, and change the to_input_field_tag method to output the default input behavior. The strange thing is, why is the value_before_type_cast method used? You can use the value method. After reading the implementation in activerecord, for the string and text types, the type_cast operation is also an empty operation without any changes.

Code:

 

Code
Class Actionview: Helpers: instancetag

Alias: original_to_input_field_tag: to_input_field_tag

DefTo_input_field_tag (field_type, options={})
Options ["Value"]| =Value (object) Unless field_type= "File"
Original_to_input_field_tag (field_type, options)
End

End

 

Step 4: Modify activerecord: Base and activerecord: connectionadapters: column to automatically encode and convert each value. As for why I did this, Please carefully study the relevant code. I also spent a lot of effort to find the solution.

Code:

 

Code
Class Activerecord: Base

Alias: original_write_attribute: write_attribute

Private
Def Write_attribute (attr_name, value)
# Logger.info "write_attribute (# {attr_name}, # {value })"
If Text_column? (Attr_name)
Value = Utf8 (value)
# Logger.info "change value to # {value }"
End
Original_write_attribute (attr_name, value)
End

Public
Def Text_column? (Attr_name)
Col = Column_for_attribute (attr_name)
If (! Col. nil? && (Col. Type = : String | Col. Type = : Text ))
True
Else
False
End
End

End

Class Activerecord: connectionadapters: Column

Alias: original_type_cast_code: type_cast_code

Def Type_cast_code (var_name)
Case Type
When: String then " GBK (# {var_name }) "
When: text then " GBK (# {var_name }) "
Else Original_type_cast_code (var_name)
End
End

End

 

Step 5: Modify the object base class and add two methods: GBK and utf8. We can see that the GBK, utf8, and other methods are used in Step 4. These methods are added to the object.

Code:

Code
Class Object
Def GBK (STR)
If Str. Blank?
''
Elsif! Kconv. isutf8 (STR)
Str
Else
Iconv. Conv ( ' Gb2312 // ignore ' , ' UTF-8 /// ignore ' , STR)
End
End

DefUnicode_to_gbk (STR)
Iconv. Conv ('Gb2312 // ignore','Unicode // ignore', STR)
End

# Convert a gb2312 encoded string to a UTF-8 encoded string
Def Utf8 (STR)
If Str. Blank?
''
# Isutf8 cannot be used to determine the encoding. For example, the word "status" is mistaken for utf8.
# Elsif kconv. isutf8 (STR)
# Str
Else
Iconv. Conv ( ' UTF-8 /// ignore ' , ' Gb2312 // ignore ' , STR)
End
End
End

 

How can these codes take effect? My approach is to write one or more RB files, put them in Lib, and load them by using require at the end of the config/enviroments. RB file.

 

Success!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.