russian 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +8 -0
- data/README.textile +2 -0
- data/Rakefile +1 -1
- data/lib/russian.rb +1 -1
- data/lib/russian/active_record_ext/custom_error_message.rb +96 -40
- data/lib/russian/transliteration.rb +36 -24
- data/spec/transliteration_spec.rb +8 -1
- metadata +2 -2
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
|
data/README.textile
CHANGED
data/Rakefile
CHANGED
data/lib/russian.rb
CHANGED
@@ -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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
options
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
93
|
+
key = :"full_messages.#{@message}"
|
94
|
+
defaults = [:'full_messages.format', '{{attribute}} {{message}}']
|
40
95
|
|
41
|
-
|
42
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
99
|
+
options[:full_message] = options[:message][1..-1]
|
100
|
+
defaults = [:"full_messages.#{@message}.format", '{{full_message}}']
|
101
|
+
end
|
47
102
|
|
48
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
}
|
24
|
-
|
25
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
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.
|
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
|
-
|
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
|
+
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-
|
12
|
+
date: 2009-12-23 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|