russian 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.2.5 - 23.12.2009
2
+
3
+ * Added support for multi-char substrings in Russian.transliterate [Alex Fortuna]
4
+ * Rails 2.3.5 error messages overloading compatibility (error messages overloading) (thanks Alex Eagle for bugreport)
5
+
6
+ * Russian.transliterate: Теперь можно добавлять правила матчинга подстрок из нескольких символов ("Воробьёв", "Алябьев"). [Alex Fortuna]
7
+ * Rails 2.3.5: совместимость для перегрузки сообщений об ошибках (багрепорт -- Александр Орел)
8
+
1
9
  === 0.2.4 - 12.11.2009
2
10
 
3
11
  * Revert nested validation errors patch
@@ -155,6 +155,8 @@ Russian::strftime
155
155
 
156
156
  Russian::strftime(Time.now)
157
157
  => "Пн, 01 сент. 2008, 11:12:43 +0300"
158
+ Russian::strftime(Time.now, "%d %B")
159
+ >> "01 сентября"
158
160
  Russian::strftime(Time.now, "%B")
159
161
  => "Сентябрь"
160
162
  </code></pre>
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rubygems/specification'
5
5
  require 'date'
6
6
 
7
7
  GEM = "russian"
8
- GEM_VERSION = "0.2.4"
8
+ GEM_VERSION = "0.2.5"
9
9
  AUTHOR = "Yaroslav Markin"
10
10
  EMAIL = "yaroslav@markin.net"
11
11
  HOMEPAGE = "http://github.com/yaroslav/russian/"
@@ -29,7 +29,7 @@ module Russian
29
29
  module VERSION
30
30
  MAJOR = 0
31
31
  MINOR = 2
32
- TINY = 4
32
+ TINY = 5
33
33
 
34
34
  STRING = [MAJOR, MINOR, TINY].join('.')
35
35
  end
@@ -1,56 +1,112 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- if defined?(ActiveRecord::Error)
3
+ if defined?(ActiveRecord::Error) # Rails 2.3.4+
4
4
  module ActiveRecord
5
5
  class Error
6
6
  protected
7
- # Redefine the ActiveRecord::Error::generate_full_message method:
8
- # Returns all the full error messages in an array. 'Base' messages are handled as usual.
9
- # Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
10
- # in which case the attribute name is omitted.
11
- # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
12
- #
13
- #
14
- # Переопределяет метод ActiveRecord::Error::generate_full_message. Сообщения об ошибках для атрибутов
15
- # теперь не имеют префикса с названием атрибута если в сообщении об ошибке первым символом указан "^".
16
- #
17
- # Так, например,
18
- #
19
- # validates_acceptance_of :accepted_terms, :message => 'нужно принять соглашение'
20
- #
21
- # даст сообщение
22
- #
23
- # Accepted terms нужно принять соглашение
24
- #
25
- # однако,
26
- #
27
- # validates_acceptance_of :accepted_terms, :message => '^Нужно принять соглашение'
28
- #
29
- # даст сообщение
30
- #
31
- # Нужно принять соглашение
32
- def generate_full_message(message, options = {})
33
- options.reverse_merge! :message => self.message,
34
- :model => @base.class.human_name,
35
- :attribute => @base.class.human_attribute_name(attribute.to_s),
36
- :value => value
7
+ if instance_methods.include?('default_options') # Rails 2.3.5+
8
+ # Redefine the ActiveRecord::Error::generate_full_message method:
9
+ # Returns all the full error messages in an array. 'Base' messages are handled as usual.
10
+ # Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
11
+ # in which case the attribute name is omitted.
12
+ # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
13
+ #
14
+ #
15
+ # Переопределяет метод ActiveRecord::Error::generate_full_message. Сообщения об ошибках для атрибутов
16
+ # теперь не имеют префикса с названием атрибута если в сообщении об ошибке первым символом указан "^".
17
+ #
18
+ # Так, например,
19
+ #
20
+ # validates_acceptance_of :accepted_terms, :message => 'нужно принять соглашение'
21
+ #
22
+ # даст сообщение
23
+ #
24
+ # Accepted terms нужно принять соглашение
25
+ #
26
+ # однако,
27
+ #
28
+ # validates_acceptance_of :accepted_terms, :message => '^Нужно принять соглашение'
29
+ #
30
+ # даст сообщение
31
+ #
32
+ # Нужно принять соглашение
33
+ def generate_full_message(options = {})
34
+ keys = [
35
+ :"full_messages.#{@message}",
36
+ :'full_messages.format',
37
+ '{{attribute}} {{message}}'
38
+ ]
39
+
40
+ if self.message.is_a?(String) && self.message =~ /^\^/
41
+ ActiveSupport::Deprecation.warn("Using '^' hack for ActiveRecord error messages has been deprecated. Please use errors.full_messages.format I18n key for formatting")
42
+
43
+ options[:full_message] = self.message[1..-1]
44
+
45
+ keys = [
46
+ :"full_messages.#{@message}",
47
+ '{{full_message}}'
48
+ ]
49
+ else
50
+ keys = [
51
+ :"full_messages.#{@message}",
52
+ :'full_messages.format',
53
+ '{{attribute}} {{message}}'
54
+ ]
55
+ end
56
+
57
+ options.merge!(:default => keys, :message => self.message)
58
+
59
+ I18n.translate(keys.shift, options)
60
+ end
61
+ else # Rails 2.3.4
62
+ # Redefine the ActiveRecord::Error::generate_full_message method:
63
+ # Returns all the full error messages in an array. 'Base' messages are handled as usual.
64
+ # Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
65
+ # in which case the attribute name is omitted.
66
+ # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
67
+ #
68
+ #
69
+ # Переопределяет метод ActiveRecord::Error::generate_full_message. Сообщения об ошибках для атрибутов
70
+ # теперь не имеют префикса с названием атрибута если в сообщении об ошибке первым символом указан "^".
71
+ #
72
+ # Так, например,
73
+ #
74
+ # validates_acceptance_of :accepted_terms, :message => 'нужно принять соглашение'
75
+ #
76
+ # даст сообщение
77
+ #
78
+ # Accepted terms нужно принять соглашение
79
+ #
80
+ # однако,
81
+ #
82
+ # validates_acceptance_of :accepted_terms, :message => '^Нужно принять соглашение'
83
+ #
84
+ # даст сообщение
85
+ #
86
+ # Нужно принять соглашение
87
+ def generate_full_message(message, options = {})
88
+ options.reverse_merge! :message => self.message,
89
+ :model => @base.class.human_name,
90
+ :attribute => @base.class.human_attribute_name(attribute.to_s),
91
+ :value => value
37
92
 
38
- key = :"full_messages.#{@message}"
39
- defaults = [:'full_messages.format', '{{attribute}} {{message}}']
93
+ key = :"full_messages.#{@message}"
94
+ defaults = [:'full_messages.format', '{{attribute}} {{message}}']
40
95
 
41
- if options[:message].is_a?(String) && options[:message] =~ /^\^/
42
- ActiveSupport::Deprecation.warn("Using '^' hack for ActiveRecord error messages has been deprecated. Please use errors.full_messages.format I18n key for formatting")
96
+ if options[:message].is_a?(String) && options[:message] =~ /^\^/
97
+ ActiveSupport::Deprecation.warn("Using '^' hack for ActiveRecord error messages has been deprecated. Please use errors.full_messages.format I18n key for formatting")
43
98
 
44
- options[:full_message] = options[:message][1..-1]
45
- defaults = [:"full_messages.#{@message}.format", '{{full_message}}']
46
- end
99
+ options[:full_message] = options[:message][1..-1]
100
+ defaults = [:"full_messages.#{@message}.format", '{{full_message}}']
101
+ end
47
102
 
48
- I18n.t(key, options.merge(:default => defaults, :scope => [:activerecord, :errors]))
103
+ I18n.t(key, options.merge(:default => defaults, :scope => [:activerecord, :errors]))
104
+ end
49
105
  end
50
106
  end
51
107
  end
52
108
 
53
- else
109
+ else # Rails 2.3.3-
54
110
  module ActiveRecord
55
111
  class Errors
56
112
  # DEPRECATED as of Rails 2.3.4
@@ -6,39 +6,51 @@ module Russian
6
6
  # Транслитерация для букв русского алфавита
7
7
  module Transliteration
8
8
  extend self
9
-
9
+
10
10
  # Transliteration heavily based on rutils gem by Julian "julik" Tarkhanov and Co.
11
11
  # <http://rutils.rubyforge.org/>
12
12
  # Cleaned up and optimized.
13
-
14
- LOWER = {
15
- "і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
16
- "ї"=>"yi","а"=>"a","б"=>"b",
17
- "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
18
- "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
19
- "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
20
- "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
21
- "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
22
- "ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
23
- }.freeze
24
-
25
- UPPER = {
13
+
14
+ LOWER_SINGLE = {
15
+ "і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
16
+ "ї"=>"yi","а"=>"a","б"=>"b",
17
+ "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
18
+ "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
19
+ "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
20
+ "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
21
+ "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
22
+ "ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
23
+ }
24
+ LOWER_MULTI = {
25
+ "ье"=>"ie",
26
+ "ьё"=>"ie",
27
+ }
28
+
29
+ UPPER_SINGLE = {
26
30
  "Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
27
31
  "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
28
- "Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
29
- "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
30
- "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
31
- "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
32
- "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"Y","Ь"=>"",
33
- "Э"=>"E","Ю"=>"YU","Я"=>"YA",
34
- }.freeze
32
+ "Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
33
+ "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
34
+ "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
35
+ "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
36
+ "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"Y","Ь"=>"",
37
+ "Э"=>"E","Ю"=>"YU","Я"=>"YA",
38
+ }
39
+ UPPER_MULTI = {
40
+ "ЬЕ"=>"IE",
41
+ "ЬЁ"=>"IE",
42
+ }
43
+
44
+ LOWER = (LOWER_SINGLE.merge(LOWER_MULTI)).freeze
45
+ UPPER = (UPPER_SINGLE.merge(UPPER_MULTI)).freeze
46
+ MULTI_KEYS = (LOWER_MULTI.merge(UPPER_MULTI)).keys.sort_by {|s| s.length}.reverse.freeze
35
47
 
36
48
  # Transliterate a string with russian characters
37
49
  #
38
50
  # Возвращает строку, в которой все буквы русского алфавита заменены на похожую по звучанию латиницу
39
51
  def transliterate(str)
40
- chars = str.split(//)
41
-
52
+ chars = str.scan(%r{#{MULTI_KEYS.join '|'}|\w|.})
53
+
42
54
  result = ""
43
55
 
44
56
  chars.each_with_index do |char, index|
@@ -54,7 +66,7 @@ module Russian
54
66
  end
55
67
  end
56
68
 
57
- return result
69
+ result
58
70
  end
59
71
  end
60
72
  end
@@ -17,7 +17,7 @@ describe Russian do
17
17
  end
18
18
 
19
19
  # These tests are from rutils, <http://rutils.rubyforge.org>.
20
-
20
+
21
21
  it "should transliterate properly" do
22
22
  t("Это просто некий текст").should == "Eto prosto nekiy tekst"
23
23
  t("щ").should == "sch"
@@ -40,5 +40,12 @@ describe Russian do
40
40
  t("Н.П. Шерстяков").should == "N.P. Sherstyakov"
41
41
  t("ШАРОВАРЫ").should == "SHAROVARY"
42
42
  end
43
+
44
+ it "should work for multi-char substrings" do
45
+ t("38 воробьёв").should == "38 vorobiev"
46
+ t("Вася Воробьёв").should == "Vasya Vorobiev"
47
+ t("Алябьев").should == "Alyabiev"
48
+ t("АЛЯБЬЕВ").should == "ALYABIEV"
49
+ end
43
50
  end
44
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Markin
@@ -9,7 +9,7 @@ autorequire: russian
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-12 00:00:00 +03:00
12
+ date: 2009-12-23 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15