l10nizer 0.0.10 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,12 @@ module L10nizer
10
10
  provisional = [make_safe(namespace), make_safe(string)].compact * "."
11
11
 
12
12
  until try(provisional, string)
13
- provisional.sub!(/(?:_\d+)?$/){ |m| "_" + m.to_i.succ.to_s }
13
+ match = provisional.match(/_(\d+)$/)
14
+ if match
15
+ provisional.sub! /\d+$/, match[1].to_i.succ.to_s
16
+ else
17
+ provisional << "_1"
18
+ end
14
19
  end
15
20
 
16
21
  return provisional
@@ -49,7 +49,7 @@ module L10nizer
49
49
 
50
50
  params = ['"' + key + '"']
51
51
  vars.each_with_index do |v, i|
52
- params << %{:#{variable_name(i)} => (#{v})}
52
+ params << %{#{variable_name(i)}: (#{v})}
53
53
  end
54
54
 
55
55
  %{<%= t(#{params * ", "}) %>}
@@ -17,13 +17,11 @@ module L10nizer
17
17
  end
18
18
 
19
19
  def processed
20
- unless @processed
21
- kcode = $KCODE
22
- $KCODE = "n"
23
- @processed = HtmlErbParser.new.parse(@html).elements.map{ |e| NodeWrapperFactory.wrap(e, @keygen) }
24
- $KCODE = kcode
25
- end
26
- @processed
20
+ @processed ||=
21
+ HtmlErbParser.new.
22
+ parse(@html).
23
+ elements.
24
+ map{ |e| NodeWrapperFactory.wrap(e, @keygen) }
27
25
  end
28
26
  end
29
27
  end
@@ -1,8 +1,8 @@
1
1
  module L10nizer #:nodoc:
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
3
+ MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 10
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,125 +1,118 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: l10nizer
3
- version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 10
10
- version: 0.0.10
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Paul Battley
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-03-02 00:00:00 +00:00
19
- default_executable: l10nizer
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
22
16
  name: treetop
23
17
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.6
25
23
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 19
30
- segments:
31
- - 1
32
- - 2
33
- - 6
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
34
28
  version: 1.2.6
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
35
31
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
32
  name: polyglot
39
33
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 0.2.5
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 29
46
- segments:
47
- - 0
48
- - 2
49
- - 5
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
50
44
  version: 0.2.5
51
- type: :runtime
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: thoughtbot-shoulda
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ type: :development
48
+ name: shoulda
55
49
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ none: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
57
61
  none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
62
+ - !ruby/object:Gem::Dependency
65
63
  type: :development
66
- version_requirements: *id003
64
+ name: rake
65
+ prerelease: false
66
+ requirement: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ none: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
67
78
  description:
68
79
  email: pbattley@gmail.com
69
- executables:
80
+ executables:
70
81
  - l10nizer
71
82
  extensions: []
72
-
73
83
  extra_rdoc_files: []
74
-
75
- files:
76
- - Rakefile
77
- - README.md
84
+ files:
78
85
  - bin/l10nizer
79
- - test/test_key_generator.rb
80
- - test/test_processor.rb
81
- - test/samples/input.html.erb
82
- - test/samples/output.html.erb
86
+ - lib/l10nizer.rb
83
87
  - lib/l10nizer/version.rb
88
+ - lib/l10nizer/processor.rb
84
89
  - lib/l10nizer/grammar.treetop
90
+ - lib/l10nizer/parser.rb
85
91
  - lib/l10nizer/node.rb
86
92
  - lib/l10nizer/keygen.rb
87
- - lib/l10nizer/processor.rb
88
- - lib/l10nizer/parser.rb
89
- - lib/l10nizer.rb
90
- has_rdoc: false
91
93
  homepage:
92
94
  licenses: []
93
-
94
95
  post_install_message:
95
96
  rdoc_options: []
96
-
97
- require_paths:
97
+ require_paths:
98
98
  - lib
99
- required_ruby_version: !ruby/object:Gem::Requirement
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
100
104
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- hash: 3
105
- segments:
106
- - 0
107
- version: "0"
108
- required_rubygems_version: !ruby/object:Gem::Requirement
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
109
110
  none: false
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- hash: 3
114
- segments:
115
- - 0
116
- version: "0"
117
111
  requirements: []
118
-
119
112
  rubyforge_project:
120
- rubygems_version: 1.3.7
113
+ rubygems_version: 1.8.23
121
114
  signing_key:
122
115
  specification_version: 3
123
- summary: Automatically extract strings from ERB templates and replace with calls to t()
116
+ summary: Automatically extract strings from ERB templates and replace with calls to
117
+ t()
124
118
  test_files: []
125
-
data/README.md DELETED
@@ -1,50 +0,0 @@
1
- L10nizer
2
- ========
3
-
4
- Automagic _ex post facto_ localisation for Rails templates.
5
-
6
- What it does
7
- ------------
8
-
9
- Processes all your `html.erb` templates, extracts text, replaces it with `t()` calls, and generates a YAML file of localisations.
10
-
11
- For example, given a file `app/views/things/show.html.erb` with this content:
12
-
13
- <div class="thing">
14
- <h1>Some heading</h1>
15
- <p>This thing is called <%= h(@thing.name)</p>
16
- </div>
17
-
18
- l10nizer will change it to:
19
-
20
- <div class="thing">
21
- <h1><%= t("things.some_heading") %></h1>
22
- <p><%= t("things.this_thing_is_called_a", :a => (h(@thing.name))) %></p>
23
- </div>
24
-
25
- and generate the following entries in `config/locales/l10nized.yml`:
26
-
27
- things:
28
- some_heading: Some heading
29
- this_thing_is_called_a: This thing is called {{a}}
30
-
31
- You can then use `l10nized.yml` as a basis for the localisation file for your current locale, e.g. `en_GB.yml`.
32
-
33
- Usage
34
- -----
35
-
36
- From within a Rails application directory:
37
-
38
- l10nizer
39
-
40
- Specifying the application path explicitly:
41
-
42
- l10nizer /path/to/my/rails/app
43
-
44
- Limitations
45
- -----------
46
-
47
- * Perhaps ironically for a _localisation_ utility, l10nizer assumes that your templates are written in English or generally in ASCII, and ignores non-alphanumeric content when generating localisation keys. This could be fixed by modifying or replacing the L10nizer::KeyGenerator class.
48
- * L10nizer takes no position on HTML entities or escaping. You __will__ need to review the changes it makes.
49
- * Similarly, pluralisation is outside the scope of this application and will require attention.
50
- * Strings that should be single entities but which contain HTML will be broken into multiple localisation strings.
data/Rakefile DELETED
@@ -1,54 +0,0 @@
1
- require "rubygems"
2
- require "rake/gempackagetask"
3
- require "rake/testtask"
4
- require "lib/l10nizer/version"
5
-
6
- task :default => [:test]
7
-
8
- Rake::TestTask.new("test") do |t|
9
- t.libs << "test"
10
- t.pattern = "test/**/test_*.rb"
11
- t.verbose = true
12
- end
13
-
14
- task :default => :test
15
-
16
- require "rake/testtask"
17
- Rake::TestTask.new do |t|
18
- t.libs << "test"
19
- t.test_files = FileList["test/**/*_test.rb"]
20
- t.verbose = true
21
- end
22
-
23
- spec = Gem::Specification.new do |s|
24
- s.name = "l10nizer"
25
- s.version = L10nizer::VERSION::STRING
26
- s.summary = "Automatically extract strings from ERB templates and replace with calls to t()"
27
- s.author = "Paul Battley"
28
- s.email = "pbattley@gmail.com"
29
-
30
- s.has_rdoc = false
31
-
32
- s.files = %w(Rakefile README.md) + Dir.glob("{bin,test,lib}/**/*")
33
- s.executables = FileList["bin/**"].map { |f| File.basename(f) }
34
-
35
- s.require_paths = ["lib"]
36
-
37
- s.add_dependency("treetop", ">= 1.2.6")
38
- s.add_dependency("polyglot", ">= 0.2.5")
39
-
40
- s.add_development_dependency("thoughtbot-shoulda")
41
- end
42
-
43
- Rake::GemPackageTask.new(spec) do |pkg|
44
- pkg.gem_spec = spec
45
-
46
- # Generate the gemspec file for github.
47
- file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
48
- File.open(file, "w") {|f| f << spec.to_ruby }
49
- end
50
-
51
- desc 'Clear out generated packages'
52
- task :clean => [:clobber_package] do
53
- rm "#{spec.name}.gemspec"
54
- end
@@ -1,34 +0,0 @@
1
- <li id="action_<%=h thing.whatsit.id %>" class="item underway">
2
- <div class="wrap">
3
- <div class="placeholder">
4
- <%- if thing.whatsit.identity? -%>
5
- <%- if thing.whatsit.identity.processed? -%>
6
- <%= image_tag thing.whatsit.identity.url(:medium) %>
7
- <%- else -%>
8
- <%= t("general.processing_image") %>
9
- <%- end -%>
10
- <%- end -%>
11
- </div>
12
- <h3>
13
- <% link_to(thing) do %>
14
- <span><%=h thing.whatsit.name %></span><%=h thing.short_address %>
15
- <% end %>
16
- </h3>
17
- </div>
18
- <div class="details">
19
- <div class="format">
20
- <h4 class="obfuscate">Skills</h4>
21
- <ul class="skills">
22
- <% thing.whatsit.skills.each do |skill| %>
23
- <li class="skill">
24
- <%= skill_icon(skill, :small) %>
25
- </li>
26
- <% end %>
27
- <li class="engagement">
28
- <%= engagement_icon(4, :small) %>
29
- </li>
30
- </ul>
31
- <%=h truncate(thing.whatsit.short_description, :length => 130) %>
32
- </div>
33
- </div>
34
- </li>
@@ -1,34 +0,0 @@
1
- <li id="action_<%=h thing.whatsit.id %>" class="item underway">
2
- <div class="wrap">
3
- <div class="placeholder">
4
- <%- if thing.whatsit.identity? -%>
5
- <%- if thing.whatsit.identity.processed? -%>
6
- <%= image_tag thing.whatsit.identity.url(:medium) %>
7
- <%- else -%>
8
- <%= t("general.processing_image") %>
9
- <%- end -%>
10
- <%- end -%>
11
- </div>
12
- <h3>
13
- <% link_to(thing) do %>
14
- <span><%=h thing.whatsit.name %></span><%=h thing.short_address %>
15
- <% end %>
16
- </h3>
17
- </div>
18
- <div class="details">
19
- <div class="format">
20
- <h4 class="obfuscate"><%= t("skills") %></h4>
21
- <ul class="skills">
22
- <% thing.whatsit.skills.each do |skill| %>
23
- <li class="skill">
24
- <%= skill_icon(skill, :small) %>
25
- </li>
26
- <% end %>
27
- <li class="engagement">
28
- <%= engagement_icon(4, :small) %>
29
- </li>
30
- </ul>
31
- <%=h truncate(thing.whatsit.short_description, :length => 130) %>
32
- </div>
33
- </div>
34
- </li>
@@ -1,68 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require "test/unit"
4
- require "l10nizer/keygen"
5
- require "shoulda"
6
-
7
- class KeyGeneratorTest < Test::Unit::TestCase
8
-
9
- def setup
10
- @keygen = L10nizer::KeyGenerator.new
11
- end
12
-
13
- context "without namespacing" do
14
- should "generate keys based on string" do
15
- assert_equal "foo_bar_baz_a", @keygen.call("Foo bar baz {{a}}")
16
- end
17
-
18
- should "truncate exceptionally long keys" do
19
- long = "blah_" * 20
20
- short = "blah_blah_blah_blah_blah_blah_blah_blah"
21
- assert_equal short, @keygen.call(long)
22
- end
23
-
24
- should "reuse key for same text" do
25
- assert_equal @keygen.call("the same"), @keygen.call("the same")
26
- end
27
-
28
- should "prevent duplicate keys for different texts" do
29
- assert_equal "a_thing", @keygen.call("a thing")
30
- assert_equal "a_thing_1", @keygen.call("A thing")
31
- assert_equal "a_thing_2", @keygen.call("A Thing")
32
- end
33
-
34
- should "generate non empty keys for punctuation" do
35
- assert_not_equal "", @keygen.call("<>!@#%#.,")
36
- end
37
-
38
- should "skip entities in keys" do
39
- assert_equal "foo_bar", @keygen.call("foo &apos; bar")
40
- end
41
-
42
- should "skip inline markup in keys" do
43
- assert_equal "foo_bar", @keygen.call("foo <strong>bar</strong>")
44
- end
45
- end
46
-
47
- context "with namespacing" do
48
- setup do
49
- @keygen.namespace = "ns1"
50
- end
51
-
52
- should "prepend namespace" do
53
- assert_equal "ns1.foo", @keygen.call("Foo")
54
- end
55
-
56
- should "prevent duplicate keys for different texts" do
57
- assert_equal "ns1.a_thing", @keygen.call("a thing")
58
- assert_equal "ns1.a_thing_1", @keygen.call("A thing")
59
- assert_equal "ns1.a_thing_2", @keygen.call("A Thing")
60
- end
61
-
62
- should "check duplication by namespace" do
63
- assert_equal "ns1.a_thing", @keygen.call("a thing")
64
- @keygen.namespace = "ns2"
65
- assert_equal "ns2.a_thing", @keygen.call("A thing")
66
- end
67
- end
68
- end
@@ -1,162 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
-
3
- require "test/unit"
4
- require "l10nizer/processor"
5
- require "shoulda"
6
-
7
- class ProcessorTest < Test::Unit::TestCase
8
-
9
- class DumbKeyGenerator
10
- def call(string)
11
- string.downcase.gsub(/[^a-z0-9]+/, "_").gsub(/^_|_$/, "")[0, 40]
12
- end
13
- end
14
-
15
- context "when finding text" do
16
- setup do
17
- html = "just some text"
18
- @l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
19
- end
20
-
21
- should "pass key to t()" do
22
- expected = %{<%= t("just_some_text") %>}
23
- assert_equal expected, @l10nizer.reformed
24
- end
25
-
26
- should "extract l10n strings" do
27
- expected = {"just_some_text" => "just some text"}
28
- assert_equal expected, @l10nizer.l10ns
29
- end
30
- end
31
-
32
- context "when interpolating inline eval" do
33
- setup do
34
- html = "String <%= 27 %> with <%= 42 %>"
35
- @l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
36
- end
37
-
38
- should "pass values to t()" do
39
- expected = %{<%= t("string_a_with_b", :a => (27), :b => (42)) %>}
40
- assert_equal expected, @l10nizer.reformed
41
- end
42
-
43
- should "extract l10n strings" do
44
- expected = {"string_a_with_b" => "String %{a} with %{b}"}
45
- assert_equal expected, @l10nizer.l10ns
46
- end
47
- end
48
-
49
- context "when interpolating in multiple text strings" do
50
- setup do
51
- html = "<p>String <%= 27 %> with <%= 42 %></p><p>Another <%= 'x' %></p>"
52
- @l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
53
- end
54
-
55
- should "pass values to t() reusing placeholder variables" do
56
- expected = %{<p><%= t("string_a_with_b", :a => (27), :b => (42)) %></p><p><%= t("another_a", :a => ('x')) %></p>}
57
- assert_equal expected, @l10nizer.reformed
58
- end
59
-
60
- should "extract l10n strings with placeholder variables" do
61
- expected = {"string_a_with_b" => "String %{a} with %{b}", "another_a" => "Another %{a}"}
62
- assert_equal expected, @l10nizer.l10ns
63
- end
64
- end
65
-
66
- should "not try to localise inline eval on its own" do
67
- html = "<p><%= 27 %></p>"
68
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
69
- assert_equal html, l10nizer.reformed
70
- end
71
-
72
- should "not try to localise an HTML comment" do
73
- html = "<!-- <p><%= 27 %></p> --> <p> <!-- fooo --> </p>"
74
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
75
- assert_equal html, l10nizer.reformed
76
- end
77
-
78
- should "not try to localise Javascript" do
79
- html = "<script>var a = 3;</script> <script>var b = 'b';</script>"
80
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
81
- assert_equal html, l10nizer.reformed
82
- end
83
-
84
- should "not try to localise inline styles" do
85
- html = %{<style type="text/css">\nhtml.js .nojs {display: none; background:#fff!important;}\n</style>}
86
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
87
- assert_equal html, l10nizer.reformed
88
- end
89
-
90
- should "not try to localise control inside a tag" do
91
- html = %{<div class="user-skills block <% unless @user.skills.any? %>blank<% end %>">}
92
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
93
- assert_equal html, l10nizer.reformed
94
- end
95
-
96
- context "when string contains inline markup" do
97
- setup do
98
- html = "<p>String with <strong>strong</strong> and <em>emphasised</em> text</p>"
99
- @l10nizer = L10nizer::Processor.new(html, lambda{ "key" })
100
- end
101
-
102
- should "include that markup in text" do
103
- expected = "String with <strong>strong</strong> and <em>emphasised</em> text"
104
- assert_equal [expected], @l10nizer.l10ns.values
105
- end
106
-
107
- should "use only one localisation" do
108
- expected = %{<p><%= t("key") %></p>}
109
- assert_equal expected, @l10nizer.reformed
110
- end
111
- end
112
-
113
- should "not consider <span> to be inline markup" do
114
- html = %{foo <span>bar</span>}
115
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
116
- assert_equal ["bar", "foo"], l10nizer.l10ns.values.sort
117
- end
118
-
119
- context "when parsing a sample document" do
120
- setup do
121
- @html = File.read(File.join(File.dirname(__FILE__), "samples", "input.html.erb"))
122
- @expected = File.read(File.join(File.dirname(__FILE__), "samples", "output.html.erb"))
123
- @l10nizer = L10nizer::Processor.new(@html, DumbKeyGenerator.new)
124
- end
125
-
126
- should "replace embedded text" do
127
- actual = @l10nizer.reformed
128
- #@expected.split(/\n/).zip(actual.split(/\n/)).each do |a, b|
129
- # puts a, b unless a == b
130
- #end
131
- assert_equal @expected, actual
132
- end
133
-
134
- should "extract l10n strings" do
135
- expected = {"skills" => "Skills"}
136
- assert_equal expected, @l10nizer.l10ns
137
- end
138
- end
139
-
140
- context "when $KCODE is 'UTF8'" do
141
- setup do
142
- @kcode = $KCODE
143
- $KCODE = "UTF8"
144
- end
145
-
146
- teardown do
147
- $KCODE = @kcode
148
- end
149
-
150
- should "parse multi-byte characters in strings" do
151
- html = "<p>We’ve</p>"
152
- l10nizer = L10nizer::Processor.new(html, DumbKeyGenerator.new)
153
- assert_equal ["We’ve"], l10nizer.l10ns.values
154
- end
155
-
156
- should "not change $KCODE" do
157
- l10nizer = L10nizer::Processor.new("", DumbKeyGenerator.new)
158
- ignore = l10nizer.l10ns.values
159
- assert_equal "UTF8", $KCODE
160
- end
161
- end
162
- end