localize 0.1 → 0.2

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.
@@ -4,6 +4,7 @@ Example:
4
4
 
5
5
  puts t.hello # => Hola
6
6
  puts t.hello.world # => Hola, mundo
7
+ puts t.hello('world', 'space') # => Hello world and space
7
8
  puts f 380441234567 # => +380 (44) 123-45-67
8
9
  puts l Time.utc(2000, "jan"), :short # => Sat 01-Jan-00
9
10
  puts l 1200.05 # => 1.200,05
@@ -58,6 +59,19 @@ Text section contains translations and can have an unlimited nesting:
58
59
  foo:
59
60
  bar: 'baz' # t.foo.bar
60
61
 
62
+ You may interpolate you translations:
63
+
64
+ text:
65
+ hello: 'Hello ${1} and ${2}!'
66
+ foo: 'Foo ${2}, ${1} or ${1}, ${3}'
67
+
68
+ …in translation, and…
69
+
70
+ t.hello('world', 'space')
71
+ t.foo('bar', 'baz', 'fee')
72
+
73
+ …in view produce 'Hello world and space!' and 'Foo baz, bar or bar, fee'
74
+
61
75
  Formats section contains different localization rules and formats:
62
76
 
63
77
  formats:
@@ -120,7 +120,14 @@ module Localize
120
120
  key = key.to_s
121
121
  instance_variable_set("@#{key}", value)
122
122
  self.class.class_eval do
123
- define_method("#{key}") { instance_variable_get("@#{key}") }
123
+ define_method("#{key}") do |*args|
124
+ str = instance_variable_get("@#{key}")
125
+ if args.length > 0
126
+ _interpolate(str, args)
127
+ else
128
+ str
129
+ end
130
+ end
124
131
  end
125
132
  end
126
133
  end
@@ -128,6 +135,14 @@ module Localize
128
135
  def method_missing(name, *params)
129
136
  MissString.new('Translation missing: '+name.to_s)
130
137
  end
138
+
139
+ private
140
+ def _interpolate(string, args)
141
+ args.length.times do |i|
142
+ string.gsub!(/\$\{#{i+1}\}/, args[i])
143
+ end
144
+ string
145
+ end
131
146
  end
132
147
 
133
148
  class MissString < String
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "localize"
3
- s.version = "0.1"
4
- s.date = "2010-05-06"
3
+ s.version = "0.2"
4
+ s.date = "2010-09-03"
5
5
 
6
6
  s.summary = "Lightweight localization library"
7
7
  s.description = "Lightweight, ruby-way localization solution"
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  test/formats_test.rb
22
22
  test/translate_test.rb
23
23
  test/stores/en.yml
24
+ test/interpolation_test.rb
24
25
  ]
25
26
  s.require_paths = %w[lib]
26
27
 
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/localize')
2
+
3
+ describe Localize do
4
+ before do
5
+ Localize.location = 'stores'
6
+ @t = Localize.translate
7
+ end
8
+
9
+ it "Interpolate one param" do
10
+ @t.interp_test.one('one').should == 'It interpolate one param'
11
+ end
12
+
13
+ it "Interpolate many params" do
14
+ @t.interp_test.many('one', 'two', 'three').should == 'It interpolate one, two, three and more params'
15
+ end
16
+
17
+ it "Interpolate many params with shuffle" do
18
+ @t.interp_test.shuffle('one', 'two', 'three').should == 'It shuffle two, three, one'
19
+ end
20
+
21
+ it "Repeat params" do
22
+ @t.interp_test.repeat('one', 'two').should == 'It repeat one, two, one'
23
+ end
24
+
25
+ it "Ignore more params" do
26
+ @t.interp_test.ignore('one', 'two', 'three', 'four').should == 'It print one, two, and not print other'
27
+ end
28
+
29
+ it "Ignore more interpolations" do
30
+ @t.interp_test.ignore_more('one', 'two').should == 'It print one, two, but not ${3}'
31
+ end
32
+ end
@@ -2,6 +2,14 @@ text:
2
2
  hello: 'world'
3
3
  foo:
4
4
  bar: 'baz'
5
+ interp_test:
6
+ one: 'It interpolate ${1} param'
7
+ many: 'It interpolate ${1}, ${2}, ${3} and more params'
8
+ shuffle: 'It shuffle ${2}, ${3}, ${1}'
9
+ repeat: 'It repeat ${1}, ${2}, ${1}'
10
+ ignore: 'It print ${1}, ${2}, and not print other'
11
+ ignore_more: 'It print ${1}, ${2}, but not ${3}'
12
+
5
13
  formats:
6
14
  phone:
7
15
  :full: '+### (##) ###-##-##'
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localize
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 15
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 1
8
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
9
10
  platform: ruby
10
11
  authors:
11
12
  - Andrey Savchenko
@@ -13,16 +14,18 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2010-05-06 00:00:00 +03:00
17
+ date: 2010-09-03 00:00:00 +03:00
17
18
  default_executable:
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency
20
21
  name: rspec
21
22
  prerelease: false
22
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
23
25
  requirements:
24
26
  - - ">="
25
27
  - !ruby/object:Gem::Version
28
+ hash: 3
26
29
  segments:
27
30
  - 0
28
31
  version: "0"
@@ -48,6 +51,7 @@ files:
48
51
  - test/formats_test.rb
49
52
  - test/translate_test.rb
50
53
  - test/stores/en.yml
54
+ - test/interpolation_test.rb
51
55
  has_rdoc: true
52
56
  homepage: http://aejis.eu/tools/localize
53
57
  licenses: []
@@ -58,26 +62,31 @@ rdoc_options: []
58
62
  require_paths:
59
63
  - lib
60
64
  required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
61
66
  requirements:
62
67
  - - ">="
63
68
  - !ruby/object:Gem::Version
69
+ hash: 3
64
70
  segments:
65
71
  - 0
66
72
  version: "0"
67
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
68
75
  requirements:
69
76
  - - ">="
70
77
  - !ruby/object:Gem::Version
78
+ hash: 3
71
79
  segments:
72
80
  - 0
73
81
  version: "0"
74
82
  requirements: []
75
83
 
76
84
  rubyforge_project:
77
- rubygems_version: 1.3.6
85
+ rubygems_version: 1.3.7
78
86
  signing_key:
79
87
  specification_version: 3
80
88
  summary: Lightweight localization library
81
89
  test_files:
82
90
  - test/formats_test.rb
83
91
  - test/translate_test.rb
92
+ - test/interpolation_test.rb