验证活动记录回调中的日语字符 [英] Validate Japanese Character in Active Record Callback

查看:99
本文介绍了验证活动记录回调中的日语字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日语项目,需要验证半角和全角日语字符,半角允许14个字符,全角允许7个字符。

I have a Japanese project that needs to validate a half width and full width Japanese character, 14 chars are allowed on half width and 7 characters on full width.

现在在我的模型上

class Customer
   validates_length_of :name, :maximum => 14
end

不是一个好选择

我当前正在使用ror 2.3.5
可以使用全角和半角

I'm currently using ror 2.3.5 Both fullwidth and halfwidth can be used

推荐答案

以下代码可能只是将您推送到行上方,以满足您到目前为止在尽可能短的时间内中指定的 exact 要求。它使用 Moji gem 日语文档),它提供了许多方便的方法来确定日语字符串的内容。

The following code may just push you over the line to fulfil the exact requirement you've so far specified in the least possible time. It uses the Moji gem (Japanese documentation), which gives lots of convenience methods in determining the content of a Japanese language string.

它验证名称中最多14个字符,该由半角字符组成,对于 name 否则(包括包含半角和全角字符的名称,即字符串中甚至存在一个全角字符,都将使整个字符串被视为全角-

It validates a maximum of 14 characters in a name that only consists of half-width characters, and a maximum of 7 characters for names otherwise (including names that contain a combination of half- and full-width characters i.e. the presence of even one full-width character in the string will make the whole string be regarded as "full-width").

class Customer 

  validates_length_of :name, :maximum => 14, 
    :if => Proc.new { |customer| half_width?(customer.name) }
  validates_length_of :name, :maximum => 7
    :unless => Proc.new { |customer| half_width?(customer.name) }

  def half_width?(string)
    Moji.type?(string, Moji::HAN_KATA)
  end

end

所作的假设


  1. 系统内的数据编码为UTF-8,并以此形式存储在数据库中;在另一个模块中完成任何进一步的必要重新编码(例如用于将数据传递到旧系统等)。

  2. 在数据之前不会自动将半角字符转换为全角字符被保存到数据库中,即由于传统系统集成,正确保留实际用户输入(!)和/或半角字符(!)的美观价值等原因,允许在数据库中使用半角字符

  3. 半角字符的变音符号被视为它们自己的单独字符(即,ie和no的解析不被视为确定字符串长度的一个字符)

  4. 您指定的名称字段只有一个,而不是四个(对于姓氏,姓氏 furigana ,给定名称,给定名称 furigana ),这在当今非常普遍。

  1. Data encoding within the system is UTF-8, and gets stored as such in the database; any further necessary re-encoding (such as for passing the data to a legacy system etc) is done in another module.
  2. No automatic conversion of half-to-full width characters done before data is saved to database i.e. half-width characters are allowed in the database for reasons perhaps of legacy system integration, proper preservation of actual user input(!), and/or aesthetic value of half-width characters(!)
  3. Diacritics in half-width characters are treated as their own separate character (i.e. no parsing of カ and ゙ to be considered one character for purposes of determining string length)
  4. There is only one name field as you specify and not, say, four (for surname, surname furigana, given name, given name furigana) which is quite common nowadays.

这篇关于验证活动记录回调中的日语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆