form_input 0.9.0.pre1

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +19 -0
  4. data/README.md +3160 -0
  5. data/Rakefile +19 -0
  6. data/example/controllers/ramaze/press_release.rb +104 -0
  7. data/example/controllers/ramaze/profile.rb +38 -0
  8. data/example/controllers/sinatra/press_release.rb +114 -0
  9. data/example/controllers/sinatra/profile.rb +39 -0
  10. data/example/forms/change_password_form.rb +17 -0
  11. data/example/forms/login_form.rb +14 -0
  12. data/example/forms/lost_password_form.rb +14 -0
  13. data/example/forms/new_password_form.rb +15 -0
  14. data/example/forms/password_form.rb +18 -0
  15. data/example/forms/press_release_form.rb +153 -0
  16. data/example/forms/profile_form.rb +21 -0
  17. data/example/forms/signup_form.rb +25 -0
  18. data/example/views/press_release.slim +65 -0
  19. data/example/views/profile.slim +28 -0
  20. data/example/views/snippets/form_block.slim +27 -0
  21. data/example/views/snippets/form_chunked.slim +25 -0
  22. data/example/views/snippets/form_hidden.slim +21 -0
  23. data/example/views/snippets/form_panel.slim +89 -0
  24. data/form_input.gemspec +32 -0
  25. data/lib/form_input/core.rb +1165 -0
  26. data/lib/form_input/localize.rb +49 -0
  27. data/lib/form_input/r18n/cs.yml +97 -0
  28. data/lib/form_input/r18n/en.yml +70 -0
  29. data/lib/form_input/r18n/pl.yml +122 -0
  30. data/lib/form_input/r18n/sk.yml +120 -0
  31. data/lib/form_input/r18n.rb +163 -0
  32. data/lib/form_input/steps.rb +365 -0
  33. data/lib/form_input/types.rb +176 -0
  34. data/lib/form_input/version.rb +12 -0
  35. data/lib/form_input.rb +5 -0
  36. data/test/helper.rb +21 -0
  37. data/test/localize/en.yml +63 -0
  38. data/test/r18n/cs.yml +60 -0
  39. data/test/r18n/xx.yml +51 -0
  40. data/test/reference/cs.txt +352 -0
  41. data/test/reference/cs.yml +14 -0
  42. data/test/reference/en.txt +76 -0
  43. data/test/reference/en.yml +8 -0
  44. data/test/reference/pl.txt +440 -0
  45. data/test/reference/pl.yml +16 -0
  46. data/test/reference/sk.txt +352 -0
  47. data/test/reference/sk.yml +14 -0
  48. data/test/test_core.rb +1272 -0
  49. data/test/test_localize.rb +27 -0
  50. data/test/test_r18n.rb +373 -0
  51. data/test/test_steps.rb +482 -0
  52. data/test/test_types.rb +307 -0
  53. metadata +145 -0
@@ -0,0 +1,49 @@
1
+ # Simple helper for creating default translation file for existing project.
2
+
3
+ require 'yaml'
4
+ require 'form_input/r18n'
5
+
6
+ # Primitive helper for converting hash keys to strings.
7
+ class Hash
8
+ # Convert all hash keys to strings recursively.
9
+ def stringify_keys
10
+ map{ |k, v| [ k.to_s, v.respond_to?( :stringify_keys ) ? v.stringify_keys : v ] }.to_h
11
+ end
12
+ end
13
+
14
+ class FormInput
15
+ class Parameter
16
+ # Array definining explicit default order of names of parameter options in translation files.
17
+ TRANSLATION_ORDER = %w[title form_title error_title gender plural inflect required_msg msg match_msg reject_msg]
18
+
19
+ # Get translation order for given option name.
20
+ def translation_order( name )
21
+ TRANSLATION_ORDER.index( name.to_s ) || TRANSLATION_ORDER.count
22
+ end
23
+
24
+ # Get hash of all parameter values which may need to be localized.
25
+ def translation_hash
26
+ opts.select{ |k, v| v.is_a? String }.sort_by{ |k, v| [ translation_order( k ), k ] }.to_h
27
+ end
28
+ end
29
+
30
+ # Get hash of all form values which may need to be localized.
31
+ def self.translation_hash
32
+ hash = form_params.map{ |k, v| [ k, v.translation_hash ] }.reject{ |k, v| v.empty? }.to_h
33
+ hash[ :steps ] = form_steps.reject{ |k, v| v.nil? } if form_steps
34
+ hash
35
+ end
36
+
37
+ # Get list of all classes inherited from FormInput.
38
+ def self.forms
39
+ ObjectSpace.each_object( Class ).select{ |x| x < FormInput }
40
+ end
41
+
42
+ # Get string containing YAML representation of the default R18n translation for current project.
43
+ def self.default_translation
44
+ hash = forms.sort_by{ |x| x.name }.map{ |x| [ x.translation_name, x.translation_hash ] }.reject{ |k, v| v.empty? }.to_h
45
+ YAML::dump( { forms: hash }.stringify_keys )
46
+ end
47
+ end
48
+
49
+ # EOF #
@@ -0,0 +1,97 @@
1
+ # Form input Czech messages.
2
+ #
3
+ # Supports singular and plural numbers and masculine, feminine, and neuter genders,
4
+ # including animate and inanimate masculine gender. Nominative case is assumed.
5
+ #
6
+ # Inflection classes supported are (standard declension paradigms included for reference):
7
+ #
8
+ # sma: pán muž předseda soudce
9
+ # smi or sm: hrad stroj
10
+ # sf: žena růže píseň kost
11
+ # sn: město moře kuře stavení
12
+ # pma: páni muži předsedové soudci
13
+ # pmi or pm: hrady stroje
14
+ # pf: ženy růže písně kosti
15
+ # pn: města moře kuřata stavení
16
+ #
17
+ # Note that when exact match is not found, longest prefix match applies (e.g., pma->pm->p is tried in turn).
18
+
19
+ form_input:
20
+ default_gender: mi # For 'Parametr x'
21
+ errors:
22
+ required_scalar: !!inflect
23
+ sm: '%p je povinný'
24
+ sf: '%p je povinná'
25
+ sn: '%p je povinné'
26
+ p: '%p jsou povinné'
27
+ pma: '%p jsou povinní'
28
+ pn: '%p jsou povinná'
29
+ required_array: !!inflect
30
+ sm: '%p je povinný'
31
+ sf: '%p je povinná'
32
+ sn: '%p je povinné'
33
+ p: '%p jsou povinné'
34
+ pma: '%p jsou povinní'
35
+ pn: '%p jsou povinná'
36
+ not_array: !!inflect
37
+ s: '%p není pole'
38
+ p: '%p nejsou pole'
39
+ not_hash: !!inflect
40
+ s: '%p není hash'
41
+ p: '%p nejsou hash'
42
+ not_string: !!inflect
43
+ s: '%p není řetězec'
44
+ p: '%p nejsou řetězec'
45
+ match_key: !!inflect
46
+ s: '%p obsahuje neplatný klíč'
47
+ p: '%p obsahují neplatný klíč'
48
+ invalid_key: !!inflect
49
+ s: '%p obsahuje neplatný klíč'
50
+ p: '%p obsahují neplatný klíč'
51
+ min_key: !!inflect
52
+ s: '%p obsahuje příliš malý klíč'
53
+ p: '%p obsahují příliš malý klíč'
54
+ max_key: !!inflect
55
+ s: '%p obsahuje příliš velký klíč'
56
+ p: '%p obsahují příliš velký klíč'
57
+ min_count: '%p musí mít nejméně %1'
58
+ max_count: '%p smí mít nejvíce %1'
59
+ value_type: '%p musí mít správný formát'
60
+ element_type: !!inflect
61
+ s: '%p obsahuje neplatnou hodnotu'
62
+ p: '%p obsahují neplatnou hodnotu'
63
+ min_limit: '%p musí být nejméně %1'
64
+ max_limit: '%p smí být nejvíce %1'
65
+ inf_limit: '%p musí být větší než %1'
66
+ sup_limit: '%p musí být menší než %1'
67
+ invalid_encoding: '%p musí mít platný encoding'
68
+ invalid_characters: '%p nesmí obsahovat zakázané znaky'
69
+ min_size: '%p musí mít nejméně %1'
70
+ max_size: '%p smí mít nejvíce %1'
71
+ min_bytesize: '%p musí mít nejméně %1'
72
+ max_bytesize: '%p smí mít nejvíce %1'
73
+ reject_msg: !!inflect
74
+ sm: '%p v tomto tvaru není povolen'
75
+ sf: '%p v tomto tvaru není povolena'
76
+ sn: '%p v tomto tvaru není povoleno'
77
+ p: '%p v tomto tvaru nejsou povoleny'
78
+ pma: '%p v tomto tvaru nejsou povoleni'
79
+ pn: '%p v tomto tvaru nejsou povolena'
80
+ match_msg: !!inflect
81
+ s: '%p není ve správném tvaru'
82
+ p: '%p nejsou ve správném tvaru'
83
+ units:
84
+ byte: !!pl
85
+ 1: '%1 byte'
86
+ 2: '%1 byty'
87
+ n: '%1 bytů'
88
+ character: !!pl
89
+ 1: '%1 znak'
90
+ 2: '%1 znaky'
91
+ n: '%1 znaků'
92
+ element: !!pl
93
+ 1: '%1 prvek'
94
+ 2: '%1 prvky'
95
+ n: '%1 prvků'
96
+
97
+ # EOF #
@@ -0,0 +1,70 @@
1
+ # Form input English messages.
2
+ #
3
+ # Supports singular and plural inflection for all messages.
4
+ # All parameter names are assumed to use the nominative case.
5
+
6
+ form_input:
7
+ errors:
8
+ required_scalar: !!inflect
9
+ s: '%p is required'
10
+ p: '%p are required'
11
+ required_array: !!inflect
12
+ s: '%p is required'
13
+ p: '%p are required'
14
+ not_array: !!inflect
15
+ s: '%p is not an array'
16
+ p: '%p are not an array'
17
+ not_hash: !!inflect
18
+ s: '%p is not a hash'
19
+ p: '%p are not a hash'
20
+ not_string: !!inflect
21
+ s: '%p is not a string'
22
+ p: '%p are not a string'
23
+ match_key: !!inflect
24
+ s: '%p contains invalid key'
25
+ p: '%p contain invalid key'
26
+ invalid_key: !!inflect
27
+ s: '%p contains invalid key'
28
+ p: '%p contain invalid key'
29
+ min_key: !!inflect
30
+ s: '%p contains too small key'
31
+ p: '%p contain too small key'
32
+ max_key: !!inflect
33
+ s: '%p contains too large key'
34
+ p: '%p contain too large key'
35
+ min_count: '%p must have at least %1'
36
+ max_count: '%p may have at most %1'
37
+ value_type: !!inflect
38
+ s: '%p like this is not valid'
39
+ p: '%p like these are not valid'
40
+ element_type: !!inflect
41
+ s: '%p contains invalid value'
42
+ p: '%p contain invalid value'
43
+ min_limit: '%p must be at least %1'
44
+ max_limit: '%p may be at most %1'
45
+ inf_limit: '%p must be greater than %1'
46
+ sup_limit: '%p must be less than %1'
47
+ invalid_encoding: '%p must use valid encoding'
48
+ invalid_characters: '%p may not contain invalid characters'
49
+ min_size: '%p must have at least %1'
50
+ max_size: '%p may have at most %1'
51
+ min_bytesize: '%p must have at least %1'
52
+ max_bytesize: '%p may have at most %1'
53
+ reject_msg: !!inflect
54
+ s: '%p like this is not allowed'
55
+ p: '%p like these are not allowed'
56
+ match_msg: !!inflect
57
+ s: '%p like this is not valid'
58
+ p: '%p like these are not valid'
59
+ units:
60
+ byte: !!pl
61
+ 1: '%1 byte'
62
+ n: '%1 bytes'
63
+ character: !!pl
64
+ 1: '%1 character'
65
+ n: '%1 characters'
66
+ element: !!pl
67
+ 1: '%1 element'
68
+ n: '%1 elements'
69
+
70
+ # EOF #
@@ -0,0 +1,122 @@
1
+ # Form input Polish messages.
2
+ #
3
+ # Supports singular and plural numbers and masculine, feminine, and neuter genders,
4
+ # including personal, animate, and inanimate masculine gender. Nominative case is assumed.
5
+ #
6
+ # Inflection classes supported are (some declension templates included for reference):
7
+ #
8
+ # smp: Chłopak Rolnik Krzyś Facet Inżynier
9
+ # sma: Kot Pies
10
+ # smi or sm: Komputer Stół
11
+ # sf: Dziewczyna Kobieta Mrówka Kawa Krowa Kasia Komedia Miłość Wysokość
12
+ # sn: Piwo Krzesło Ciastko Dziecko Oko Ucho Spotkanie
13
+ # pmp: Chłopaki Rolnicy Krzysie Faceci Inżynierzy
14
+ # pma: Koty Psy
15
+ # pmi or pm: Komputery Stoły
16
+ # pf: Dziewczyny Kobiety Mrówki Kawy Krowy Kasie Komedie Miłości Wysokości
17
+ # pn: Piwa Krzesła Ciastka Dzieci Oczy Uszy Spotkania
18
+ #
19
+ # Note that when exact match is not found, longest prefix match applies (e.g., pma->pm->p is tried in turn).
20
+
21
+ form_input:
22
+ default_gender: mi # For 'Parametr x'
23
+ errors:
24
+ required_scalar: !!inflect
25
+ sm: '%p jest wymagany'
26
+ sf: '%p jest wymagana'
27
+ sn: '%p jest wymagane'
28
+ pmp: '%p są wymagani'
29
+ p: '%p są wymagane'
30
+ required_array: !!inflect
31
+ sm: '%p jest wymagany'
32
+ sf: '%p jest wymagana'
33
+ sn: '%p jest wymagane'
34
+ pmp: '%p są wymagani'
35
+ p: '%p są wymagane'
36
+ not_array: !!inflect
37
+ s: '%p nie jest tablicą'
38
+ p: '%p nie są tablicami'
39
+ not_hash: !!inflect
40
+ s: '%p nie jest hashem'
41
+ p: '%p nie są hashami'
42
+ not_string: !!inflect
43
+ s: '%p nie jest ciągiem znaków'
44
+ p: '%p nie są ciągiem znaków'
45
+ match_key: !!inflect
46
+ s: '%p zawiera nieprawidłowy klucz'
47
+ p: '%p zawierają nieprawidłowy klucz'
48
+ invalid_key: !!inflect
49
+ s: '%p zawiera nieprawidłowy klucz'
50
+ p: '%p zawierają nieprawidłowy klucz'
51
+ min_key: !!inflect
52
+ s: '%p zawiera za mały klucz'
53
+ p: '%p zawierają za mały klucz'
54
+ max_key: !!inflect
55
+ s: '%p zawiera za duży klucz'
56
+ p: '%p zawierają za duży klucz'
57
+ min_count: !!inflect
58
+ s: '%p musi mieć conajmniej %1'
59
+ p: '%p muszą mieć conajmniej %1'
60
+ max_count: !!inflect
61
+ s: '%p musi mieć maksymalnie %1'
62
+ p: '%p muszą mieć maksymalnie %1'
63
+ value_type: !!inflect
64
+ s: '%p musi mieć poprawny format'
65
+ p: '%p muszą mieć poprawny format'
66
+ element_type: !!inflect
67
+ s: '%p zawiera nieprawidłową wartość'
68
+ p: '%p zawierają nieprawidłową wartość'
69
+ min_limit: !!inflect
70
+ s: '%p musi wynosić conajmniej %1'
71
+ p: '%p muszą wynosić conajmniej %1'
72
+ max_limit: !!inflect
73
+ s: '%p musi wynosić maksymalnie %1'
74
+ p: '%p muszą wynosić maksymalnie %1'
75
+ inf_limit: !!inflect
76
+ s: '%p musi wynosić więcej niż %1'
77
+ p: '%p muszą wynosić więcej niż %1'
78
+ sup_limit: !!inflect
79
+ s: '%p musi wynosić mniej niż %1'
80
+ p: '%p muszą wynosić mniej niż %1'
81
+ invalid_encoding: !!inflect
82
+ s: '%p musi używać poprawnego kodowania'
83
+ p: '%p muszą używać poprawnego kodowania'
84
+ invalid_characters: !!inflect
85
+ s: '%p nie może zawierać zabronionych znaków'
86
+ p: '%p nie mogą zawierać zabronionych znaków'
87
+ min_size: !!inflect
88
+ s: '%p musi mieć przynajmniej %1'
89
+ p: '%p muszą mieć przynajmniej %1'
90
+ max_size: !!inflect
91
+ s: '%p musi mieć maksymalnie %1'
92
+ p: '%p muszą mieć maksymalnie %1'
93
+ min_bytesize: !!inflect
94
+ s: '%p musi mieć przynajmniej %1'
95
+ p: '%p muszą mieć przynajmniej %1'
96
+ max_bytesize: !!inflect
97
+ s: '%p musi mieć maksymalnie %1'
98
+ p: '%p muszą mieć maksymalnie %1'
99
+ reject_msg: !!inflect
100
+ sm: '%p w tej postaci nie jest poprawny'
101
+ sf: '%p w tej postaci nie jest poprawna'
102
+ sn: '%p w tej postaci nie jest poprawne'
103
+ pmp: '%p w tej postaci nie są poprawni'
104
+ p: '%p w tej postaci nie są poprawne'
105
+ match_msg: !!inflect
106
+ s: '%p nie jest w poprawnej formie'
107
+ p: '%p nie są w poprawnej formie'
108
+ units:
109
+ byte: !!pl
110
+ 1: '%1 bajt'
111
+ 2: '%1 bajty'
112
+ n: '%1 bajtów'
113
+ character: !!pl
114
+ 1: '%1 znak'
115
+ 2: '%1 znaki'
116
+ n: '%1 znaków'
117
+ element: !!pl
118
+ 1: '%1 element'
119
+ 2: '%1 elementy'
120
+ n: '%1 elementów'
121
+
122
+ # EOF #
@@ -0,0 +1,120 @@
1
+ # Form input Slovak messages.
2
+ #
3
+ # Supports singular and plural numbers and masculine, feminine, and neuter genders,
4
+ # including animate and inanimate masculine gender. Nominative case is assumed.
5
+ #
6
+ # Inflection classes supported are (standard declension paradigms included for reference):
7
+ #
8
+ # sma: chlap hrdina
9
+ # smi or sm: dub stroj
10
+ # sf: žena ulica dlaň kosť
11
+ # sn: mesto srdce vysvedčenie dievča
12
+ # pma: chlapi hrdinovia
13
+ # pmi or pm: duby stroje
14
+ # pf: ženy ulice dlane kosti
15
+ # pn: mestá srdcia vysvedčenia dievčatá
16
+ #
17
+ # Note that when exact match is not found, longest prefix match applies (e.g., pma->pm->p is tried in turn).
18
+
19
+ form_input:
20
+ default_gender: mi # For 'Parameter x'
21
+ errors:
22
+ required_scalar: !!inflect
23
+ sm: '%p je povinný'
24
+ sf: '%p je povinná'
25
+ sn: '%p je povinné'
26
+ p: '%p sú povinné'
27
+ pma: '%p sú povinní'
28
+ required_array: !!inflect
29
+ sm: '%p je povinný'
30
+ sf: '%p je povinná'
31
+ sn: '%p je povinné'
32
+ p: '%p sú povinné'
33
+ pma: '%p sú povinní'
34
+ not_array: !!inflect
35
+ s: '%p nie je polia'
36
+ p: '%p nie sú polia'
37
+ not_hash: !!inflect
38
+ s: '%p nie je hash'
39
+ p: '%p nie sú hash'
40
+ not_string: !!inflect
41
+ s: '%p nie je reťazec'
42
+ p: '%p nie sú reťazce'
43
+ match_key: !!inflect
44
+ s: '%p obsahuje neplatný kľúč'
45
+ p: '%p obsahujú neplatný kľúč'
46
+ invalid_key: !!inflect
47
+ s: '%p obsahuje neplatný kľúč'
48
+ p: '%p obsahujú neplatný kľúč'
49
+ min_key: !!inflect
50
+ s: '%p obsahuje príliš malý kľúč'
51
+ p: '%p obsahujú príliš malý kľúč'
52
+ max_key: !!inflect
53
+ s: '%p obsahuje príliš veľký kľúč'
54
+ p: '%p obsahujú príliš veľký kľúč'
55
+ min_count: !!inflect
56
+ s: '%p musí mať najmenej %1'
57
+ p: '%p musia mať najmenej %1'
58
+ max_count: !!inflect
59
+ s: '%p smie mať najviac %1'
60
+ p: '%p smú mať najviac %1'
61
+ value_type: !!inflect
62
+ s: '%p musí mať správný formát'
63
+ p: '%p musia mať správný formát'
64
+ element_type: !!inflect
65
+ s: '%p obsahuje neplatnú hodnotu'
66
+ p: '%p obsahujú neplatnú hodnotu'
67
+ min_limit: !!inflect
68
+ s: '%p musí byť najmenej %1'
69
+ p: '%p musia byť najmenej %1'
70
+ max_limit: !!inflect
71
+ s: '%p smie byť najviac %1'
72
+ p: '%p smú byť najviac %1'
73
+ inf_limit: !!inflect
74
+ s: '%p musí byť väčší ako %1'
75
+ p: '%p musia byť väčší ako %1'
76
+ sup_limit: !!inflect
77
+ s: '%p musí byť menší ako %1'
78
+ p: '%p musia byť menší ako %1'
79
+ invalid_encoding: !!inflect
80
+ s: '%p musí mať platný encoding'
81
+ p: '%p musia mať platný encoding'
82
+ invalid_characters: !!inflect
83
+ s: '%p nesmie obsahovať zakázané znaky'
84
+ p: '%p nesmú obsahovať zakázané znaky'
85
+ min_size: !!inflect
86
+ s: '%p musí mať najmenej %1'
87
+ p: '%p musia mať najmenej %1'
88
+ max_size: !!inflect
89
+ s: '%p smie mať najviac %1'
90
+ p: '%p smú mať najviac %1'
91
+ min_bytesize: !!inflect
92
+ s: '%p musí mať najmenej %1'
93
+ p: '%p musia mať najmenej %1'
94
+ max_bytesize: !!inflect
95
+ s: '%p smie mať najviac %1'
96
+ p: '%p smú mať najviac %1'
97
+ reject_msg: !!inflect
98
+ sm: '%p v tomto tvare nie je povolený'
99
+ sf: '%p v tomto tvare nie je povolená'
100
+ sn: '%p v tomto tvare nie je povolené'
101
+ p: '%p v tomto tvare nie sú povolené'
102
+ pma: '%p v tomto tvare nie sú povolení'
103
+ match_msg: !!inflect
104
+ s: '%p nie je v správnom tvare'
105
+ p: '%p nie sú v správnom tvare'
106
+ units:
107
+ byte: !!pl
108
+ 1: '%1 byte'
109
+ 2: '%1 byty'
110
+ n: '%1 bytov'
111
+ character: !!pl
112
+ 1: '%1 znak'
113
+ 2: '%1 znaky'
114
+ n: '%1 znakov'
115
+ element: !!pl
116
+ 1: '%1 prvok'
117
+ 2: '%1 prvky'
118
+ n: '%1 prvkov'
119
+
120
+ # EOF #
@@ -0,0 +1,163 @@
1
+ # R18n localization support.
2
+
3
+ require 'form_input'
4
+ require 'r18n-core'
5
+
6
+ class FormInput
7
+
8
+ # Include R18n helpers in form context.
9
+ include R18n::Helpers
10
+
11
+ # Localize few parameter methods.
12
+ class Parameter
13
+
14
+ # Include R18n helpers in parameter context.
15
+ include R18n::Helpers
16
+
17
+ # R18n specific methods.
18
+ module R18nMethods
19
+
20
+ # Parameter options which are known to be not localized.
21
+ UNLOCALIZED_OPTIONS = [
22
+ :required, :disabled, :array, :hash, :type, :data, :tag, :tags, :filter, :transform, :format, :class, :check, :test, :reject, :match,
23
+ :min_key, :max_key, :match_key, :min_count, :max_count, :min, :max, :inf, :sup, :min_size, :max_size, :min_bytesize, :max_bytesize,
24
+ ]
25
+
26
+ # Automatically attempt to translate available parameter options.
27
+ def []( name )
28
+ if form and r18n
29
+ value = opts[ name ]
30
+ if value.is_a?( String ) or ( value.nil? and not UNLOCALIZED_OPTIONS.include?( name ) )
31
+ text = pt( name )
32
+ return text.to_s if text.translated?
33
+ end
34
+ end
35
+ super
36
+ end
37
+
38
+ # Localized version of error message formatting. See original implementation for details.
39
+ def format_error_message( msg, count = nil, singular = nil, *rest )
40
+ return super unless msg.is_a?( Symbol ) and r18n
41
+ if limit = count and singular
42
+ limit = t.form_input.units[ singular, count ].to_s
43
+ end
44
+ text = t.form_input.errors[ msg, *limit, self ].to_s
45
+ super( text )
46
+ end
47
+
48
+ # Like t helper, except that the translation is looked up in the forms.<form_name> scope.
49
+ # Supports both ft.name( args ) and ft( :name, args ) forms.
50
+ def ft( *args )
51
+ form.ft( *args )
52
+ end
53
+
54
+ # Like t helper, except that the translation is looked up in the forms.<form_name>.<param_name> scope.
55
+ # Supports both pt.name( args ) and pt( :name, args ) forms. The latter automatically adds self as last argument to support inflection.
56
+ def pt( *args )
57
+ translation = ft[ name ]
58
+ args.empty? ? translation : translation[ *args, self ]
59
+ end
60
+
61
+ # Get the inflection string used for correctly inflecting the parameter messages.
62
+ # Note that it ignores the noun case as the parameter names are supposed to use the nominative case anyway.
63
+ def inflection
64
+ ( self[ :inflect ] || "#{pluralize}#{gender}" ).to_s
65
+ end
66
+
67
+ # Get the string corresponding to the grammatical number of the parameter name used for inflecting the parameter messages.
68
+ def pluralize
69
+ p = self[ :plural ]
70
+ p = ! scalar? if p.nil?
71
+ p = 's' if p == false
72
+ p = 'p' if p == true
73
+ p.to_s
74
+ end
75
+
76
+ # Get the gender string used for inflecting the parameter messages.
77
+ def gender
78
+ ( self[ :gender ] || ( t.form_input.default_gender | 'n' ) ).to_s
79
+ end
80
+
81
+ end
82
+
83
+ include R18nMethods
84
+
85
+ end
86
+
87
+ # Localize few step methods.
88
+ module StepMethods
89
+
90
+ # Get name of current or given step, if any.
91
+ def step_name( step = self.step )
92
+ name = raw_step_name( step )
93
+ name = ( ft.steps[ step ] | name ).to_s if r18n and name
94
+ name
95
+ end
96
+
97
+ # Get hash of steps along with their names.
98
+ def step_names
99
+ hash = raw_step_names
100
+ hash = Hash[ hash.map{ |k,v| [ k, ( ft.steps[ k ] | v ).to_s ] } ] if r18n
101
+ hash
102
+ end
103
+
104
+ end
105
+
106
+ # Get path to R18n translations provided by this gem.
107
+ def self.translations_path
108
+ File.expand_path( "#{__FILE__}/../r18n" )
109
+ end
110
+
111
+ # Get name of the form used as translation scope for text translations.
112
+ def self.translation_name
113
+ @translation_name ||= name.split( '::' ).last
114
+ .gsub( /([A-Z]+)([A-Z][a-z])/, '\1_\2' )
115
+ .gsub( /([a-z\d])([A-Z])/, '\1_\2' )
116
+ .downcase
117
+ end
118
+
119
+ # Like t helper, except that the translation is looked up in the forms.<form_name> scope.
120
+ # Supports both ft.name( args ) and ft( :name, args ) forms.
121
+ def ft( *args )
122
+ fail "You need to set the locale with R18n.set('en') or similar. No locale, no helper. Sorry." unless r18n
123
+ translation = t.forms[ self.class.translation_name ]
124
+ args.empty? ? translation : translation[ *args ]
125
+ end
126
+
127
+ # Iterate over each possible inflection for given inflection string and return first non-nil result.
128
+ # You may override this if you need more complex inflection fallbacks for some locale.
129
+ def self.find_inflection( string )
130
+ until string.empty?
131
+ break if result = yield( string )
132
+ string = string[0..-2]
133
+ end
134
+ result
135
+ end
136
+
137
+ # Define our inflection filter.
138
+ R18n::Filters.add( 'inflect', :inflection ) do |translations, config, *params|
139
+ inflection = case param = params.last
140
+ when Parameter
141
+ param.inflection
142
+ when String
143
+ param
144
+ end
145
+ inflection ||= 'sn'
146
+ text = FormInput.find_inflection( inflection ) do |i|
147
+ translations[ i ] if translations.key?( i )
148
+ end
149
+ text || R18n::Translation.new(
150
+ config[ :locale ], config[ :path ],
151
+ locale: config[ :locale ], translations: translations
152
+ )[ inflection ]
153
+ end
154
+
155
+ # Add our translations as R18n extensions.
156
+ R18n.extension_places << R18n::Loader::YAML.new( translations_path )
157
+
158
+ # Localize the helper for boolean args.
159
+ BOOL_ARGS[ :data ] = ->{ [ [ true, r18n ? t.yes.to_s : 'Yes' ], [ false, r18n ? t.no.to_s : 'No' ] ] }
160
+
161
+ end
162
+
163
+ # EOF #