root/trunk/lib/globalize/models/language.rb

Revision 1, 2.6 kB (checked in by yann, 2 years ago)

import from old svn hosting (revision 200)

Line 
1 module Globalize
2   class Language < ActiveRecord::Base # :nodoc:
3     set_table_name "globalize_languages"
4
5     validates_presence_of :english_name
6
7     validates_uniqueness_of :iso_639_1, :if => :iso_639_1
8     validates_uniqueness_of :iso_639_2, :if => :iso_639_2
9     validates_uniqueness_of :iso_639_3, :if => :iso_639_3
10     validates_uniqueness_of :rfc_3066,  :if => :rfc_3066
11
12     validates_length_of :pluralization, :maximum => 200, :if => :pluralization
13     validates_format_of :pluralization, :with => /^[c=\d?:%!<>&|() ]+$/, :if => :pluralization,
14       :message => " has invalid characters. Allowed characters are: " +
15         "'c', '=', 0-9, '?', ':', '%', '!', '<', '>', '&', '|', '(', ')', ' '."
16
17     def self.reloadable?; false end
18
19     def after_initialize
20       if !pluralization.nil? && pluralization.size > 200
21         raise SecurityError, "Pluralization field for #{self.english_name} language " +
22           "contains potentially harmful code. " +
23           "Must be less than 200 characters in length. Was #{pluralization.size} characters."
24       end
25
26       if !pluralization.nil? && pluralization !~ /^[c=\d?:%!<>&|() ]+$/
27         raise SecurityError, "Pluralization field ('#{pluralization}') for #{self.english_name} language " +
28           "contains potentially harmful code. " +
29           "Must only use the characters: 'c', '=', 0-9, '?', ':', " +
30           "'%', '!', '<', '>', '&', '|', '(', ')', ' '."
31       end
32     end
33
34     def self.pick(rfc)
35       if rfc.kind_of? String then rfc = RFC_3066.parse(rfc) end
36       if rfc.locale.include? '-'
37         lang = find_by_rfc_3066(rfc.locale)
38         return lang if lang
39       end
40
41       code = rfc.language
42       if code.size == 2
43         lang = find_by_iso_639_1(code)
44       elsif code.size == 3
45         lang = find_by_iso_639_3(code)
46       end
47
48       lang
49     end
50
51     def code; iso_639_1 || iso_639_3 || rfc_3066; end
52    
53     def code=(new_code)
54       if new_code =~ /-/
55         self.rfc_3066 = new_code
56       else
57         raise ArgumentError,
58           "code must be in rfc_3066 format, with a hyphen character; was #{new_code}"
59       end
60     end
61
62     def native_name; self['native_name'] || self['english_name'] end
63
64     def ==(other)
65       return false if !other.kind_of? Language
66       self.code == other.code
67     end
68
69     def plural_index(num)
70
71       # number is not defined, so we assume no pluralization
72       return 1 if num.nil?
73
74       c = num
75       expr = pluralization || 'c == 1 ? 2 : 1'
76
77       instance_eval(expr)
78     end
79
80     def to_s;    english_name end
81     def inspect; english_name end
82
83   end
84 end
Note: See TracBrowser for help on using the browser.