slimkeyfy 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +9 -1
- data/Gemfile.lock +23 -12
- data/LICENSE +3 -3
- data/README.md +35 -7
- data/lib/slimkeyfy.rb +2 -0
- data/lib/slimkeyfy/console/command_line.rb +12 -1
- data/lib/slimkeyfy/console/translate.rb +35 -1
- data/lib/slimkeyfy/slimutils/key_generator.rb +33 -2
- data/lib/slimkeyfy/transformer/whitespacer.rb +3 -8
- data/lib/slimkeyfy/version.rb +1 -1
- data/slimkeyfy.gemspec +5 -0
- metadata +44 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3060e269599400cb5c6f08f20765aa8a69544551
|
4
|
+
data.tar.gz: fa2fbd45f6a32e10dbcd730d52ddefc9c7354e12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41e0f4bab018c96e341fb79581f011760f4334f81c168e081317e267305886f31415b3319c4c9f5a5a581c31fbeeaf16a5c183e55732bb1f12228c3d909669e8
|
7
|
+
data.tar.gz: fe503865ca14b56af2fe288b91e6c2ccc3c032f5da5a4e875755b15919ec6bf08308cafbae7888ed434e4022cfc9fb6dd7f2b22537e8d4e92d6417a2ed97ee5c
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,22 +1,33 @@
|
|
1
1
|
GEM
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
-
diff-lcs (1.
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
rspec
|
10
|
-
rspec-
|
11
|
-
|
4
|
+
diff-lcs (1.3)
|
5
|
+
httparty (0.14.0)
|
6
|
+
multi_xml (>= 0.5.2)
|
7
|
+
i18n (0.8.1)
|
8
|
+
multi_xml (0.6.0)
|
9
|
+
rspec (3.5.0)
|
10
|
+
rspec-core (~> 3.5.0)
|
11
|
+
rspec-expectations (~> 3.5.0)
|
12
|
+
rspec-mocks (~> 3.5.0)
|
13
|
+
rspec-core (3.5.4)
|
14
|
+
rspec-support (~> 3.5.0)
|
15
|
+
rspec-expectations (3.5.0)
|
12
16
|
diff-lcs (>= 1.2.0, < 2.0)
|
13
|
-
rspec-support (~> 3.
|
14
|
-
rspec-mocks (3.0
|
15
|
-
|
16
|
-
|
17
|
+
rspec-support (~> 3.5.0)
|
18
|
+
rspec-mocks (3.5.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.5.0)
|
21
|
+
rspec-support (3.5.0)
|
22
|
+
russian (0.6.0)
|
23
|
+
i18n (>= 0.5.0)
|
24
|
+
yandex-translator (0.3.2)
|
25
|
+
httparty (>= 0.13.4)
|
17
26
|
|
18
27
|
PLATFORMS
|
19
28
|
ruby
|
20
29
|
|
21
30
|
DEPENDENCIES
|
22
31
|
rspec
|
32
|
+
russian
|
33
|
+
yandex-translator
|
data/LICENSE
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
MIT License
|
2
2
|
|
3
|
-
Copyright (c) 2014
|
3
|
+
Copyright (c) 2014 Dynport GmbH
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
Slimkeyfy
|
2
2
|
=
|
3
|
-
Extract plain Strings from .slim views and Rails controllers to replace them with I18n's t() method.
|
3
|
+
Extract plain Strings from .slim views and Rails controllers to replace them with I18n's t() method.
|
4
|
+
Keys with it's translations will be streamed to a YAML file.
|
5
|
+
Non english keys get translated with yandex translator gem ( you need to get your api first ).
|
6
|
+
Russian keys get transliterated in case API KEY is missed.
|
4
7
|
|
5
|
-
Read more in this blog post: [PhraseApp Blog: Make your Rails App localizable with Slimkeyfy](
|
8
|
+
Read more in this blog post: [PhraseApp Blog: Make your Rails App localizable with Slimkeyfy](https://phraseapp.com/blog/posts/slim-localize-your-slim-templates-in-a-second-with-slimkeyfy/)
|
6
9
|
|
7
10
|
```ruby
|
8
11
|
slimkeyfy app/views/users/show.html.slim en
|
@@ -31,7 +34,7 @@ group :development do
|
|
31
34
|
# Github
|
32
35
|
gem 'slimkeyfy', github: 'https://github.com/phrase/slimkeyfy.git'
|
33
36
|
# or with concrete version
|
34
|
-
gem 'slimkeyfy', '~> 0.
|
37
|
+
gem 'slimkeyfy', '~> 0.X'
|
35
38
|
end
|
36
39
|
```
|
37
40
|
|
@@ -48,6 +51,28 @@ slimkeyfy INPUT_FILENAME_OR_DIRECTORY LOCALE (e.g. en, fr) [YAML_FILE] [Options]
|
|
48
51
|
- If you provide one make sure that the top level locale matches your provided locale
|
49
52
|
- The YAML file will be loaded as a hash and deep_merged with the new found translations
|
50
53
|
|
54
|
+
I18n for keys addition
|
55
|
+
----------------------
|
56
|
+
( from alekseyl translator branch: https://github.com/alekseyl/slimkeyfy/tree/translator )
|
57
|
+
I18n keys better be in english so if you start I18n from other than en locale you cannot use original slikeyfy approach, you need to translate keys first
|
58
|
+
Two options added to CLI:
|
59
|
+
```unix
|
60
|
+
'-t', '--translator-api-key [API_KEY]', 'API key for Yandex Translator'
|
61
|
+
'-l', '--keys-from-locale LOCALE', 'translate keys from locale'
|
62
|
+
```
|
63
|
+
|
64
|
+
API key can be given directly in CLI or added with export:
|
65
|
+
|
66
|
+
```unix
|
67
|
+
export YANDEX_TRANSLATOR_API="YANDEX_TRANSLATOR_API_KEY"
|
68
|
+
```
|
69
|
+
|
70
|
+
How to get one:
|
71
|
+
Go to https://tech.yandex.com/keys/get/?service=trnsl
|
72
|
+
Click on "register an account"
|
73
|
+
Fill in the form, with your mobile phone number. Click on send code and Yandex will send you a 4 digit code to your mobile phone. Enter the code from your phone to complete registration. Yandex will then give you an API key
|
74
|
+
|
75
|
+
|
51
76
|
**Stream** - walks through the given file/files and if a regex hits you will be prompted to apply (y)es, discard (n)o, tag (x) (comments suggestions above the processed line) or (a)bort (only aborts the current file).
|
52
77
|
|
53
78
|
without YAML file
|
@@ -149,19 +174,22 @@ bundle exec rspec spec/
|
|
149
174
|
```
|
150
175
|
PhraseApp Integration
|
151
176
|
-
|
152
|
-
Now that you processed your views and moved the generated keys to your localization files it is quite easy to push it to PhraseApp. If you have not set up your account yet [take a look here](https://phraseapp.com/
|
177
|
+
Now that you processed your views and moved the generated keys to your localization files it is quite easy to push it to PhraseApp. If you have not set up your account yet [take a look here](https://phraseapp.com/). Make sure that the latest gem of PhraseApp is installed by simply typing:
|
153
178
|
```unix
|
154
179
|
> gem install phrase
|
155
180
|
```
|
156
|
-
If you are already familiar with the PhraseApp gem you can upload your translation/localization files now. For more details on your setup have a look at our [detailed guide](https://phraseapp.com/docs/
|
181
|
+
If you are already familiar with the PhraseApp gem you can upload your translation/localization files now. For more details on your setup have a look at our [detailed guide](https://phraseapp.com/docs/guides/setup/).
|
157
182
|
|
158
183
|
Todo
|
159
184
|
-
|
160
|
-
- YAML placeholders are currently not supported (We are working on it!).
|
161
185
|
- a dry run option where you can see what will happen if you convert.
|
162
186
|
- currently you are prompted for all hits - We would like to do 70-80% automatically and prompt for the 20-30% that cannot be decided upon.
|
163
187
|
- adding / generalizing Regular Expressions for better hit rate
|
164
188
|
|
189
|
+
Closed Todos
|
190
|
+
-
|
191
|
+
- YAML placeholders are supported - thx@joshblour
|
192
|
+
|
165
193
|
Issues
|
166
194
|
-
|
167
195
|
- If you choose to take a lot of files at one time make sure to go through with it. It is not an issue to completely rerun everything (already translated strings are ignored) but should be avoided.
|
@@ -172,4 +200,4 @@ References
|
|
172
200
|
* Other tools, not slim specific for this task is the [i15r gem](https://github.com/balinterdi/i15r). It can process .haml and .erb.
|
173
201
|
* I strongly recommend checking your translated app with the [i18n-tasks gem](https://github.com/glebm/i18n-tasks). It is a great tool in finding missing and unused translations.
|
174
202
|
* Always consult the [Official Rails Internationalization guide](http://guides.rubyonrails.org/i18n.html) if in doubt.
|
175
|
-
* Visit [PhraseApp on Github](https://github.com/phrase
|
203
|
+
* Visit [PhraseApp on Github](https://github.com/phrase)
|
data/lib/slimkeyfy.rb
CHANGED
@@ -2,7 +2,7 @@ require 'optparse'
|
|
2
2
|
|
3
3
|
class SlimKeyfy::Console::Commandline
|
4
4
|
def initialize(args)
|
5
|
-
@options = {}
|
5
|
+
@options = { translator: {} }
|
6
6
|
@args = args
|
7
7
|
OptionParser.new(&method(:opt_scan)).parse!(@args)
|
8
8
|
end
|
@@ -24,6 +24,15 @@ class SlimKeyfy::Console::Commandline
|
|
24
24
|
the following options are available:
|
25
25
|
|
26
26
|
"
|
27
|
+
opts.on_tail('-t', '--translator-api-key [API_KEY]', 'API key for Yandex Translator') do |value|
|
28
|
+
@options[:translator][:api] = value || ENV['YANDEX_TRANSLATOR_API']
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
opts.on_tail('-l', '--keys-from-locale LOCALE', 'translate keys from locale') do |from_locale|
|
33
|
+
@options[:translator][:from_locale] = from_locale
|
34
|
+
end
|
35
|
+
|
27
36
|
opts.on_tail('-h', '--help', 'Show this message') do
|
28
37
|
puts opts
|
29
38
|
exit
|
@@ -50,6 +59,8 @@ class SlimKeyfy::Console::Commandline
|
|
50
59
|
@options[:locale] = locale = @args.shift
|
51
60
|
@options[:yaml_output] = @args.shift
|
52
61
|
|
62
|
+
SlimKeyfy::Slimutils::TranslationKeyGenerator.translator_options = @options[:translator]
|
63
|
+
|
53
64
|
wrong_usage if (input.nil? or locale.nil?)
|
54
65
|
|
55
66
|
if File.directory?(input)
|
@@ -8,7 +8,7 @@ class SlimKeyfy::Console::Translate
|
|
8
8
|
@original_file_path = options[:input]
|
9
9
|
@no_backup = options.fetch(:no_backup, false)
|
10
10
|
@bak_path = SlimKeyfy::Slimutils::MFileUtils.backup(@original_file_path)
|
11
|
-
@content = SlimKeyfy::Slimutils::FileReader.read(@bak_path).split("\n")
|
11
|
+
@content = self.class.join_multiline( SlimKeyfy::Slimutils::FileReader.read(@bak_path).split("\n") )
|
12
12
|
@file_path = SlimKeyfy::Slimutils::MFileUtils.create_new_file(@original_file_path)
|
13
13
|
@key_base = generate_key_base
|
14
14
|
@yaml_processor = create_yaml_processor(options)
|
@@ -98,4 +98,38 @@ class SlimKeyfy::Console::Translate
|
|
98
98
|
def comment_tag
|
99
99
|
@transformer.slim? ? "//" : "#"
|
100
100
|
end
|
101
|
+
|
102
|
+
def self.join_multiline( strings_array )
|
103
|
+
result = []
|
104
|
+
joining_str = ''
|
105
|
+
indent_length = 0
|
106
|
+
long_str_start = /^[ ]+\|/
|
107
|
+
long_str_indent = /^[ ]+/
|
108
|
+
long_str_indent_with_vertical_bar = /^[ ]+\|/
|
109
|
+
strings_array.each do |str|
|
110
|
+
if joining_str.empty?
|
111
|
+
if str[long_str_start]
|
112
|
+
joining_str = str
|
113
|
+
indent_length = str[long_str_start].length
|
114
|
+
else
|
115
|
+
result << str
|
116
|
+
end
|
117
|
+
#multiline string continues with spaces
|
118
|
+
elsif ( str[long_str_indent] && str[long_str_indent].length.to_i >= indent_length )
|
119
|
+
joining_str << str.gsub( long_str_indent, ' ' )
|
120
|
+
#muliline string continues with spaces and vertical bar with same indentation
|
121
|
+
elsif str[long_str_indent_with_vertical_bar] && str[long_str_indent_with_vertical_bar].length.to_i == indent_length
|
122
|
+
joining_str << str.gsub( long_str_indent_with_vertical_bar, ' ' )
|
123
|
+
#multiline string ends
|
124
|
+
else
|
125
|
+
result << joining_str
|
126
|
+
joining_str = ''
|
127
|
+
indent_length = 0
|
128
|
+
result << str
|
129
|
+
end
|
130
|
+
end
|
131
|
+
result << joining_str unless joining_str.empty?
|
132
|
+
|
133
|
+
result
|
134
|
+
end
|
101
135
|
end
|
@@ -39,8 +39,39 @@ class SlimKeyfy::Slimutils::TranslationKeyGenerator
|
|
39
39
|
VALID = /[^0-9a-z]/i
|
40
40
|
DEFAULT_KEY_NAME = "default_key"
|
41
41
|
|
42
|
+
#class public methods
|
43
|
+
def self.translator_options
|
44
|
+
@translator_options ||= {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.translator_options=( new_options )
|
48
|
+
@translator_options = new_options
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.yt
|
52
|
+
@yt ||= Yandex::Translator.new( translator_options[:api] )
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.translate_key(translation)
|
56
|
+
if translator_options[:api] && translation && !translation.empty?
|
57
|
+
begin
|
58
|
+
# language can be detected automatically but this is noticeably slower than providing one.
|
59
|
+
yt.translate( translation, from: translator_options[:from_locale], to: :en )
|
60
|
+
rescue Exception => e
|
61
|
+
p e.inspect
|
62
|
+
return translation
|
63
|
+
end
|
64
|
+
elsif translator_options[:from_locale].to_s == 'ru'
|
65
|
+
::Russian.transliterate(translation)
|
66
|
+
else
|
67
|
+
translation
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
#instance methods
|
42
72
|
def initialize(translation)
|
43
|
-
|
73
|
+
# keys must be in english!
|
74
|
+
@translation = self.class.translate_key( translation )
|
44
75
|
end
|
45
76
|
|
46
77
|
def generate_key_name
|
@@ -66,5 +97,5 @@ class SlimKeyfy::Slimutils::TranslationKeyGenerator
|
|
66
97
|
end
|
67
98
|
s
|
68
99
|
end
|
69
|
-
end
|
70
100
|
|
101
|
+
end
|
@@ -24,22 +24,17 @@ class SlimKeyfy::Transformer::Whitespacer
|
|
24
24
|
lead = leading_nbsp(body)
|
25
25
|
trail = trailing_nsbp(body, tag)
|
26
26
|
|
27
|
-
|
28
|
-
body
|
29
|
-
|
30
|
-
tag = tag.gsub(tag, "#{tag}#{lead}#{trail}")
|
31
|
-
[body.gsub(" ", " "), tag.gsub("=><", "=<>")]
|
27
|
+
tag = tag.gsub(tag, "#{tag}#{lead.to_s}#{trail.to_s}")
|
28
|
+
[body.sub(/^ /, '').sub(/ $/, '').gsub(" ", " "), tag.gsub("=><", "=<>")]
|
32
29
|
end
|
33
30
|
|
34
31
|
def self.leading_nbsp(body)
|
35
32
|
return "<" if body.start_with?(" ")
|
36
|
-
""
|
37
33
|
end
|
38
34
|
|
39
35
|
def self.trailing_nsbp(body, tag)
|
40
|
-
return
|
36
|
+
return '' if tag.start_with?("=>")
|
41
37
|
return ">" if body.end_with?(" ")
|
42
|
-
""
|
43
38
|
end
|
44
39
|
|
45
40
|
def self.has_equals_tag?(s, html_tag)
|
data/lib/slimkeyfy/version.rb
CHANGED
data/slimkeyfy.gemspec
CHANGED
@@ -17,5 +17,10 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = s.files.grep(%r{^(spec)/})
|
18
18
|
s.executables = ["slimkeyfy"]
|
19
19
|
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "yandex-translator"
|
22
|
+
s.add_dependency "russian"
|
23
|
+
|
20
24
|
s.add_development_dependency('rspec')
|
25
|
+
s.add_development_dependency('pry-byebug')
|
21
26
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slimkeyfy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dynport GmbH
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: yandex-translator
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: russian
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: rspec
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -24,6 +52,20 @@ dependencies:
|
|
24
52
|
- - ">="
|
25
53
|
- !ruby/object:Gem::Version
|
26
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
27
69
|
description: Replace plain text strings in your slim templates and Rails controllers
|
28
70
|
with calls to I18n t() method. Keys and YAML output files will be automatically
|
29
71
|
generated and filled in.
|