haml-i18n-extractor 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +110 -0
- data/Rakefile +54 -0
- data/TODO +5 -0
- data/bin/haml-i18n-extractor +31 -0
- data/haml-i18n-extractor.gemspec +29 -0
- data/lib/haml-i18n-extractor/core-ext/hash.rb +8 -0
- data/lib/haml-i18n-extractor/extractor.rb +84 -0
- data/lib/haml-i18n-extractor/haml_reader.rb +20 -0
- data/lib/haml-i18n-extractor/haml_writer.rb +21 -0
- data/lib/haml-i18n-extractor/text_finder.rb +94 -0
- data/lib/haml-i18n-extractor/text_replacer.rb +78 -0
- data/lib/haml-i18n-extractor/version.rb +7 -0
- data/lib/haml-i18n-extractor/yaml_tool.rb +74 -0
- data/lib/haml-i18n-extractor.rb +7 -0
- data/test/extractor_test.rb +78 -0
- data/test/haml_reader_test.rb +29 -0
- data/test/haml_writer_test.rb +34 -0
- data/test/support/bad.haml +12 -0
- data/test/support/ex1.haml +28 -0
- data/test/support/ex1.i18n-extractor.haml +28 -0
- data/test/support/ex1.output.haml +28 -0
- data/test/support/nothing_to_translate.haml +2 -0
- data/test/support/nothing_to_translate.i18n-extractor.haml +2 -0
- data/test/test_helper.rb +98 -0
- data/test/text_finder_test.rb +130 -0
- data/test/text_replacer_test.rb +67 -0
- data/test/yaml_tool_test.rb +68 -0
- metadata +168 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Shai Rosenfeld
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Haml::I18n::Extractor
|
2
|
+
|
3
|
+
Extract strings likely to be translated from haml into locale file
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
After installing this gem, you can run the binary in a rails app:
|
8
|
+
|
9
|
+
`cd your-rails-app; haml-i18n-extractor .`
|
10
|
+
|
11
|
+
## Notes
|
12
|
+
|
13
|
+
Right now the design works on a per-file basis:
|
14
|
+
|
15
|
+
<pre>
|
16
|
+
begin
|
17
|
+
@ex1 = Haml::I18n::Extractor.new(haml_path)
|
18
|
+
@ex1.run
|
19
|
+
rescue Haml::I18n::Extractor::InvalidSyntax
|
20
|
+
puts "There was an error with #{haml_path}"
|
21
|
+
rescue Haml::I18n::Extractor::NothingToTranslate
|
22
|
+
puts "Nothing to translate for #{haml_path}"
|
23
|
+
end
|
24
|
+
</pre>
|
25
|
+
|
26
|
+
This is pretty much beta - but it does work! Since it is beta, it does not overwrite anything but lets you decide what you want, and don't want.
|
27
|
+
|
28
|
+
The workflow at the moment is, to run the binary, then do a `git diff` and see all the changes.
|
29
|
+
|
30
|
+
What you should be seeing for a "foo.haml" file is a dumped version "foo.i18n-extractor.haml" file which has the temporary representation of the file it tried to translate. It also dumps the corresponding i18n locale .yml file for the haml just translated in the current working directory, suffixed with the path of the haml file it was working on.
|
31
|
+
|
32
|
+
## Before
|
33
|
+
|
34
|
+
<pre>
|
35
|
+
shai@comp ~/p/project ‹master*› » cat app/views/admin/notifications/index.html.haml
|
36
|
+
%h1 Consumer Notifications
|
37
|
+
|
38
|
+
.nav= will_paginate(@consumer_notifications)
|
39
|
+
%table.themed{cellspacing: 0}
|
40
|
+
%thead
|
41
|
+
%tr
|
42
|
+
%th.first Type
|
43
|
+
%th Identifier
|
44
|
+
%th Data
|
45
|
+
%th Success
|
46
|
+
%th Reported To
|
47
|
+
%th.last
|
48
|
+
- @consumer_notifications.each do |cn|
|
49
|
+
%tr
|
50
|
+
%td.type= cn.notification.type
|
51
|
+
%td.identifier= cn.notification.identifier
|
52
|
+
%td.data= cn.notification.data
|
53
|
+
%td.success= cn.success
|
54
|
+
%td.reported_to= cn.reported_to
|
55
|
+
.nav= will_paginate(@consumer_notifications)
|
56
|
+
</pre>
|
57
|
+
|
58
|
+
## After: new haml, new yaml.
|
59
|
+
|
60
|
+
- Note how some of the strings are replaced, and the ones that shouldn't, aren't. Yup.
|
61
|
+
|
62
|
+
Haml:
|
63
|
+
|
64
|
+
<pre>
|
65
|
+
shai@comp ~/p/project ‹master*› » cat app/views/admin/notifications/index.html.i18n-extractor.haml
|
66
|
+
%h1= t('.consumer_notifications')
|
67
|
+
|
68
|
+
.nav= will_paginate(@consumer_notifications)
|
69
|
+
%table.themed{cellspacing: 0}
|
70
|
+
%thead
|
71
|
+
%tr
|
72
|
+
%th.first= t('.type')
|
73
|
+
%th= t('.identifier')
|
74
|
+
%th= t('.data')
|
75
|
+
%th= t('.success')
|
76
|
+
%th= t('.reported_to')
|
77
|
+
%th.last= t('.nbsp;')
|
78
|
+
- @consumer_notifications.each do |cn|
|
79
|
+
%tr
|
80
|
+
%td.type= cn.notification.type
|
81
|
+
%td.identifier= cn.notification.identifier
|
82
|
+
%td.data= cn.notification.data
|
83
|
+
%td.success= cn.success
|
84
|
+
%td.reported_to= cn.reported_to
|
85
|
+
.nav= will_paginate(@consumer_notifications)
|
86
|
+
</pre>
|
87
|
+
|
88
|
+
Yaml:
|
89
|
+
|
90
|
+
<pre>
|
91
|
+
shai@comp ~/p/project ‹master*› » cat notifications.index.html.haml.yml
|
92
|
+
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
93
|
+
en: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
94
|
+
notifications: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
95
|
+
consumer_notifications: Consumer Notifications
|
96
|
+
type: Type
|
97
|
+
identifier: Identifier
|
98
|
+
data: Data
|
99
|
+
success: Success
|
100
|
+
reported_to: Reported To
|
101
|
+
nbsp;: ! ' '
|
102
|
+
</pre>
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
1. Fork it
|
107
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
108
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
109
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
110
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
def with_each_gemfile
|
6
|
+
old_env = ENV['BUNDLE_GEMFILE']
|
7
|
+
gemfiles.each do |gemfile|
|
8
|
+
puts "Using gemfile: #{gemfile}"
|
9
|
+
ENV['BUNDLE_GEMFILE'] = gemfile
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
ensure
|
13
|
+
ENV['BUNDLE_GEMFILE'] = old_env
|
14
|
+
end
|
15
|
+
|
16
|
+
def silence_warnings
|
17
|
+
the_real_stderr, $stderr = $stderr, StringIO.new
|
18
|
+
yield
|
19
|
+
ensure
|
20
|
+
$stderr = the_real_stderr
|
21
|
+
end
|
22
|
+
|
23
|
+
Rake::TestTask.new do |t|
|
24
|
+
t.libs << 'lib' << 'test'
|
25
|
+
t.test_files = Dir["test/**/*_test.rb"]
|
26
|
+
t.warning = true
|
27
|
+
t.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :test
|
31
|
+
namespace :test do
|
32
|
+
namespace :bundles do
|
33
|
+
desc "Install all dependencies necessary to test."
|
34
|
+
task :install do
|
35
|
+
with_each_gemfile {sh "bundle"}
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "Update all dependencies for testing."
|
39
|
+
task :update do
|
40
|
+
with_each_gemfile {sh "bundle update"}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Test all supported versions of rails. This takes a while."
|
45
|
+
task :rails_compatibility => 'test:bundles:install' do
|
46
|
+
with_each_gemfile {sh "bundle exec rake test"}
|
47
|
+
end
|
48
|
+
task :rc => :rails_compatibility
|
49
|
+
end
|
50
|
+
|
51
|
+
# FIXME, jsut run this
|
52
|
+
task :force_build_and_install do
|
53
|
+
%{ gem uninstall -x haml-i18n-extractor; rm *gem; gem build *gemspec; gem install --local *gem }
|
54
|
+
end
|
data/TODO
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
- Have an interactive mode: define workflow(s) for using this lib/tool.
|
2
|
+
- Remove the ex1.output ex1.i18n-extractor duplication. (or move to tmp dir)
|
3
|
+
- Add {} to disallowed characters
|
4
|
+
- Add yaml fixtures and compare those
|
5
|
+
- handled email suffixes (foo.en.html.haml etc)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'haml-i18n-extractor'
|
4
|
+
|
5
|
+
if ARGV.empty?
|
6
|
+
puts "Usage: <haml-i18n-extractor> [path]"
|
7
|
+
abort
|
8
|
+
end
|
9
|
+
|
10
|
+
relative_path = ARGV[0]
|
11
|
+
absolute_path = File.expand_path(relative_path)
|
12
|
+
|
13
|
+
all_files = Dir.glob("**/*")
|
14
|
+
haml_paths = all_files.select do |file|
|
15
|
+
file.match /.haml$/
|
16
|
+
end
|
17
|
+
|
18
|
+
# right now, this should create a copy of the haml file in that directory with a special suffix for review.
|
19
|
+
# review it, see what fails, fix, etc.
|
20
|
+
|
21
|
+
haml_paths.each do |haml_path|
|
22
|
+
begin
|
23
|
+
puts "extracting strings for #{haml_path}"
|
24
|
+
@ex1 = Haml::I18n::Extractor.new(haml_path)
|
25
|
+
@ex1.run
|
26
|
+
rescue Haml::I18n::Extractor::InvalidSyntax
|
27
|
+
puts "There was an error with #{haml_path}"
|
28
|
+
rescue Haml::I18n::Extractor::NothingToTranslate
|
29
|
+
puts "Nothing to translate for #{haml_path}"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/haml-i18n-extractor/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Shai Rosenfeld"]
|
6
|
+
gem.email = ["shaiguitar@gmail.com"]
|
7
|
+
gem.description = %q{Parse the texts out of the haml files into localization files}
|
8
|
+
gem.summary = %q{Parse the texts out of the haml files into localization files}
|
9
|
+
gem.homepage = ""
|
10
|
+
gem.executables = ['haml-i18n-extractor']
|
11
|
+
gem.default_executable = 'haml-i18n-extractor'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "haml-i18n-extractor"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = Haml::I18n::Extractor::VERSION
|
19
|
+
|
20
|
+
gem.add_dependency "tilt"
|
21
|
+
gem.add_dependency "haml"
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rails', '>= 3.0.0'
|
24
|
+
gem.add_development_dependency 'rbench'
|
25
|
+
gem.add_development_dependency 'pry'
|
26
|
+
gem.add_development_dependency 'minitest'
|
27
|
+
gem.add_development_dependency 'nokogiri'
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Haml
|
2
|
+
module I18n
|
3
|
+
class Extractor
|
4
|
+
|
5
|
+
def self.debug?
|
6
|
+
ENV['DEBUG']
|
7
|
+
end
|
8
|
+
|
9
|
+
class InvalidSyntax < StandardError ; end
|
10
|
+
class NothingToTranslate < StandardError ; end
|
11
|
+
class NotDefinedLineType < StandardError ; end
|
12
|
+
|
13
|
+
LINE_TYPES_ALL = [:text, :not_text, :loud, :silent, :element]
|
14
|
+
LINE_TYPES_ADD_EVAL = [:text, :element]
|
15
|
+
|
16
|
+
attr_reader :haml_reader, :haml_writer
|
17
|
+
attr_reader :locale_hash, :yaml_tool
|
18
|
+
|
19
|
+
def initialize(haml_path)
|
20
|
+
@haml_reader = Haml::I18n::Extractor::HamlReader.new(haml_path)
|
21
|
+
validate_haml(@haml_reader.body)
|
22
|
+
@haml_writer = Haml::I18n::Extractor::HamlWriter.new(haml_path)
|
23
|
+
@yaml_tool = Haml::I18n::Extractor::YamlTool.new
|
24
|
+
@locale_hash = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def run
|
28
|
+
assign_replacements
|
29
|
+
validate_haml(@haml_writer.body)
|
30
|
+
@haml_writer.write_file
|
31
|
+
@yaml_tool.write_file
|
32
|
+
end
|
33
|
+
|
34
|
+
def assign_new_body
|
35
|
+
@haml_writer.body = new_body
|
36
|
+
end
|
37
|
+
|
38
|
+
def assign_yaml
|
39
|
+
@yaml_tool.locale_hash = @locale_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def assign_replacements
|
43
|
+
assign_new_body
|
44
|
+
assign_yaml
|
45
|
+
end
|
46
|
+
|
47
|
+
# processes the haml document, replaces the input body to an output body of replacements. also sets an in memory hash of replacement info
|
48
|
+
# to be used later by the yaml tool.
|
49
|
+
# keep indentation to use later when printing out.
|
50
|
+
def new_body
|
51
|
+
new_lines = []
|
52
|
+
file_has_been_parsed = false
|
53
|
+
@haml_reader.lines.each_with_index do |orig_line, line_no|
|
54
|
+
orig_line.chomp!
|
55
|
+
orig_line.rstrip.match(/([ \t]+)?(.*)/)
|
56
|
+
whitespace_indentation = $1
|
57
|
+
orig_line = $2
|
58
|
+
line_type, line_match = Haml::I18n::Extractor::TextFinder.new(orig_line).process_by_regex
|
59
|
+
if line_match && !line_match.empty?
|
60
|
+
file_has_been_parsed = true
|
61
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type)
|
62
|
+
@locale_hash[line_no] = replacer.replace_hash.dup.merge!({:path => @haml_reader.path })
|
63
|
+
new_lines << "#{whitespace_indentation}#{replacer.replace_hash[:modified_line]}"
|
64
|
+
else
|
65
|
+
@locale_hash[line_no] = { :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }
|
66
|
+
new_lines << "#{whitespace_indentation}#{orig_line}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
raise NothingToTranslate if !file_has_been_parsed
|
70
|
+
new_lines.join("\n")
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def validate_haml(haml)
|
76
|
+
parser = Haml::Parser.new(haml, Haml::Options.new)
|
77
|
+
parser.parse
|
78
|
+
rescue Haml::SyntaxError
|
79
|
+
raise InvalidSyntax, "invalid syntax for haml #{@haml_reader.path}"
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Haml
|
2
|
+
module I18n
|
3
|
+
class Extractor
|
4
|
+
class HamlReader
|
5
|
+
|
6
|
+
attr_reader :body, :path, :lines
|
7
|
+
|
8
|
+
def initialize(path)
|
9
|
+
file = File.open(path, "r")
|
10
|
+
@path = path
|
11
|
+
@body = file.read
|
12
|
+
file.rewind
|
13
|
+
@lines = file.readlines
|
14
|
+
file.rewind
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Haml
|
2
|
+
module I18n
|
3
|
+
class Extractor
|
4
|
+
class HamlWriter
|
5
|
+
|
6
|
+
attr_accessor :path, :lines, :body
|
7
|
+
|
8
|
+
def initialize(orig_path)
|
9
|
+
@path = orig_path.gsub(/.haml$/, ".i18n-extractor.haml")
|
10
|
+
end
|
11
|
+
|
12
|
+
def write_file
|
13
|
+
f = File.open(@path, "w+")
|
14
|
+
f.puts(self.body)
|
15
|
+
f.close
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'haml'
|
2
|
+
require 'haml/parser'
|
3
|
+
module Haml
|
4
|
+
module I18n
|
5
|
+
class Extractor
|
6
|
+
class TextFinder
|
7
|
+
|
8
|
+
THINGS_THAT_ARE_NOT_POTENTIAL_TEXT = [ Haml::Parser::DIV_CLASS, Haml::Parser::DIV_ID,
|
9
|
+
Haml::Parser::COMMENT, Haml::Parser::SANITIZE, Haml::Parser::FLAT_SCRIPT,
|
10
|
+
Haml::Parser::FILTER, Haml::Parser::DOCTYPE, Haml::Parser::ESCAPE ]
|
11
|
+
|
12
|
+
## hint: Use http://rubular.com
|
13
|
+
SIMPLE_STRING_REGEX = /["'](.*)["']/
|
14
|
+
# = "yes"
|
15
|
+
# = 'yes'
|
16
|
+
LINK_TO_REGEX = /link_to\s*\(?\s*['"](.*)['"]\s*,\s*(.*)\)?/
|
17
|
+
# = link_to "yes", "http://bla.com"
|
18
|
+
# = link_to('yes' , "http://bla.com")
|
19
|
+
# = link_to( "yes's w space", "http://bla.com")
|
20
|
+
ELEMENT_REGEX = /%([\w.#-]+)({.+?})?(=)?(.*)/
|
21
|
+
# %foo.bar yes
|
22
|
+
# %foo.bar= no
|
23
|
+
# %foo{:a => 'b'} yes
|
24
|
+
# rubular.com against %foo#bar#cheez{:cheeze => 'hell'}= "what #{var}"
|
25
|
+
|
26
|
+
def initialize(haml)
|
27
|
+
@haml = haml
|
28
|
+
@parser = Haml::Parser.new(haml, Haml::Options.new)
|
29
|
+
end
|
30
|
+
|
31
|
+
def line
|
32
|
+
if @haml == ""
|
33
|
+
return Haml::Parser::Line.new("", "", "", 0, @parser, false)
|
34
|
+
end
|
35
|
+
|
36
|
+
match = @haml.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m)
|
37
|
+
match.pop
|
38
|
+
haml_line ||= match.each_with_index.map do |(full, whitespace, text), index|
|
39
|
+
Haml::Parser::Line.new(whitespace, text.rstrip, full, index, @parser, false)
|
40
|
+
end.first
|
41
|
+
haml_line
|
42
|
+
end
|
43
|
+
|
44
|
+
def find
|
45
|
+
text_match = process_by_regex.last
|
46
|
+
end
|
47
|
+
|
48
|
+
# returns [ line_type, text_found ]
|
49
|
+
def process_by_regex
|
50
|
+
case line.full[0]
|
51
|
+
when *THINGS_THAT_ARE_NOT_POTENTIAL_TEXT
|
52
|
+
[:not_text, ""]
|
53
|
+
when Haml::Parser::SILENT_SCRIPT
|
54
|
+
parse_silent_script
|
55
|
+
when Haml::Parser::ELEMENT
|
56
|
+
parse_element
|
57
|
+
when Haml::Parser::SCRIPT
|
58
|
+
parse_loud_script
|
59
|
+
else
|
60
|
+
[:text, line.full.strip]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def parse_silent_script
|
67
|
+
line.full.match(/-[\s\t]*#{SIMPLE_STRING_REGEX}/)
|
68
|
+
[:silent, $1.to_s]
|
69
|
+
end
|
70
|
+
|
71
|
+
def parse_loud_script
|
72
|
+
line.full.match(/=[\s\t]*#{LINK_TO_REGEX}/)
|
73
|
+
return [:loud, $1.to_s] if text = $1
|
74
|
+
line.full.match(/=[\s\t]*#{SIMPLE_STRING_REGEX}/)
|
75
|
+
return [:loud, $1.to_s]
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_element
|
79
|
+
line.full.match(ELEMENT_REGEX)
|
80
|
+
elem_with_class_and_ids = $1
|
81
|
+
attributes_ruby_style = $2
|
82
|
+
is_loud_script = $3
|
83
|
+
text = $4.to_s
|
84
|
+
if is_loud_script
|
85
|
+
self.class.new("= #{text}").process_by_regex # treat like a loud script.
|
86
|
+
else
|
87
|
+
[:element, text.strip]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Haml
|
2
|
+
module I18n
|
3
|
+
class Extractor
|
4
|
+
class TextReplacer
|
5
|
+
|
6
|
+
attr_reader :full_line, :text_to_replace, :line_type
|
7
|
+
|
8
|
+
T_REGEX = /t\('\..*'\)/
|
9
|
+
# limit the number of chars
|
10
|
+
LIMIT_KEY_NAME = 30
|
11
|
+
# do not pollute the key space it will make it invalid yaml
|
12
|
+
NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , )
|
13
|
+
|
14
|
+
def initialize(full_line, text_to_replace,line_type)
|
15
|
+
@full_line = full_line
|
16
|
+
@text_to_replace = text_to_replace
|
17
|
+
if LINE_TYPES_ALL.include?(line_type)
|
18
|
+
@line_type = line_type
|
19
|
+
else
|
20
|
+
raise Extractor::NotDefinedLineType, "line type #{line_type} for #{full_line} does not make sense!"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def replace_hash
|
25
|
+
@replace_hash ||= { :modified_line => modified_line, :keyname => keyname, :replaced_text => @text_to_replace }
|
26
|
+
end
|
27
|
+
|
28
|
+
# the new full line, including a `t()` replacement instead of the `text_to_replace` portion.
|
29
|
+
def modified_line
|
30
|
+
full_line = @full_line.dup
|
31
|
+
return @full_line if has_been_translated?(full_line)
|
32
|
+
remove_surrounding_quotes(full_line)
|
33
|
+
apply_ruby_evaling(full_line)
|
34
|
+
full_line
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def keyname
|
40
|
+
text_to_replace = @text_to_replace.dup
|
41
|
+
has_been_translated?(text_to_replace) ? text_to_replace : "t('.#{to_keyname(text_to_replace)}')"
|
42
|
+
end
|
43
|
+
|
44
|
+
def apply_ruby_evaling(str)
|
45
|
+
if LINE_TYPES_ADD_EVAL.include?(@line_type)
|
46
|
+
if @line_type == :element
|
47
|
+
str.match /^([^\s\t]*)(.*)$/
|
48
|
+
elem, keyname = $1, $2
|
49
|
+
str.gsub!($2, "= #{$2.strip}")
|
50
|
+
elsif @line_type == :text
|
51
|
+
str.gsub!(str, "= "+str)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_been_translated?(str)
|
57
|
+
str.match T_REGEX
|
58
|
+
end
|
59
|
+
|
60
|
+
def remove_surrounding_quotes(str)
|
61
|
+
# if there are quotes surrounding the string, we want them removed as well...
|
62
|
+
unless str.gsub!('"' + @text_to_replace + '"', keyname)
|
63
|
+
unless str.gsub!("'" + @text_to_replace + "'", keyname)
|
64
|
+
str.gsub!(@text_to_replace, keyname)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_keyname(str)
|
70
|
+
NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
|
71
|
+
str = str.gsub(/\s+/, " ").strip
|
72
|
+
str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'pathname'
|
3
|
+
require 'haml-i18n-extractor/core-ext/hash'
|
4
|
+
require 'active_support/hash_with_indifferent_access'
|
5
|
+
|
6
|
+
module Haml
|
7
|
+
module I18n
|
8
|
+
class Extractor
|
9
|
+
class YamlTool
|
10
|
+
|
11
|
+
attr_accessor :locales_dir, :locale_hash
|
12
|
+
|
13
|
+
def initialize(locales_dir = nil)
|
14
|
+
if locales_dir
|
15
|
+
@locales_dir = locales_dir
|
16
|
+
else
|
17
|
+
# locales_dir = rails_mode? ? (Rails.root.to_s + "/config/locales") : "."
|
18
|
+
@locales_dir = File.expand_path(".")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# {:en => {:view_name => {:key_name => :string_name } } }
|
23
|
+
def yaml_hash
|
24
|
+
h = HashWithIndifferentAccess.recursive_init
|
25
|
+
@locale_hash.map do |line_no, info|
|
26
|
+
unless info[:keyname].nil?
|
27
|
+
h[i18n_scope][standardized_viewname(info[:path])][standarized_keyname(info[:keyname])] = info[:replaced_text]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
h
|
31
|
+
end
|
32
|
+
|
33
|
+
def locale_file
|
34
|
+
if @locale_hash
|
35
|
+
pth = @locale_hash.map{|_,h| h[:path] }.compact.first
|
36
|
+
if pth
|
37
|
+
full_path = Pathname.new(pth)
|
38
|
+
base_name = full_path.basename.to_s
|
39
|
+
File.expand_path(File.join( @locales_dir, standardized_viewname(full_path) + ".#{base_name}.yml"))
|
40
|
+
end
|
41
|
+
end.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
def write_file
|
45
|
+
f = File.open(locale_file, "w+")
|
46
|
+
f.puts yaml_hash.to_yaml
|
47
|
+
f.flush
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def i18n_scope
|
53
|
+
:en
|
54
|
+
end
|
55
|
+
|
56
|
+
# comes in like "t('.some_place')", return .some_place
|
57
|
+
def standarized_keyname(name)
|
58
|
+
name.match(/t\('\.(.*)'\)/)
|
59
|
+
$1
|
60
|
+
end
|
61
|
+
|
62
|
+
# assuming rails format, app/views/users/index.html.haml return users
|
63
|
+
def standardized_viewname(pth)
|
64
|
+
Pathname.new(pth).dirname.to_s.split("/").last
|
65
|
+
end
|
66
|
+
|
67
|
+
# def rails_mode?
|
68
|
+
# defined?(Rails)
|
69
|
+
# end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require "haml-i18n-extractor/version"
|
2
|
+
require "haml-i18n-extractor/text_finder"
|
3
|
+
require "haml-i18n-extractor/text_replacer"
|
4
|
+
require "haml-i18n-extractor/haml_reader"
|
5
|
+
require "haml-i18n-extractor/haml_writer"
|
6
|
+
require "haml-i18n-extractor/yaml_tool"
|
7
|
+
require "haml-i18n-extractor/extractor"
|