i18n-magic 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de350a396ad7f6f8eff10f7edd1b57d57989afe5
4
- data.tar.gz: e3d886339500d887b33f8c9fe8895c4195c8e438
3
+ metadata.gz: 1ceb29c304d96f0d2c975648c3476a9be2305fce
4
+ data.tar.gz: e117982cdee614cf719a94f88f898fc97a0a55da
5
5
  SHA512:
6
- metadata.gz: cb3aa4e5ab2058617e4a8d17acecae15ad4b3fb01d5032a161ce72e5cf04a6a40835ee55ced670528fd85c4b6b48ea790e952ec4e108da1f93c78485f63d0fd3
7
- data.tar.gz: 52d85e60d58ea38dac54c1ac7d74646f47479170fca048ae0bbb03baff87ff3a22cfe3de41044aa0c6d5473fe68cca0a15a63f567480f4f983c1f587a3c72019
6
+ metadata.gz: 63e0859c2b34f58878a09a8e3a6726289d9b1d22d1f13539d418907cd47cb246c56ff7a68d3b18c4d7cbf6d36970708b39ddd3b3141e860cdfaacf6e3d5719fd
7
+ data.tar.gz: e5ab85abb44a4bc3351f16a22f7ac7c60e1b0719c8c6a5d161b6aa62a6c22b228c90a939450e36ba2c902c005d81bbfb68b18421733e1ebc461c68442991736a
@@ -1,21 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- i18n-magic (1.0.0)
4
+ i18n-magic (1.1.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.1.5)
10
+ activesupport (5.1.6)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (~> 0.7)
12
+ i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
15
  ast (2.4.0)
16
16
  coderay (1.1.2)
17
17
  concurrent-ruby (1.0.5)
18
18
  diff-lcs (1.3)
19
+ docile (1.3.0)
19
20
  ffi (1.9.23)
20
21
  formatador (0.2.5)
21
22
  guard (2.14.2)
@@ -35,13 +36,14 @@ GEM
35
36
  guard-rubocop (1.3.0)
36
37
  guard (~> 2.0)
37
38
  rubocop (~> 0.20)
38
- i18n (0.9.5)
39
+ i18n (1.0.0)
39
40
  concurrent-ruby (~> 1.0)
41
+ json (2.1.0)
40
42
  listen (3.1.5)
41
43
  rb-fsevent (~> 0.9, >= 0.9.4)
42
44
  rb-inotify (~> 0.9, >= 0.9.7)
43
45
  ruby_dep (~> 1.2)
44
- lumberjack (1.0.12)
46
+ lumberjack (1.0.13)
45
47
  method_source (0.9.0)
46
48
  minitest (5.11.3)
47
49
  nenv (0.3.0)
@@ -49,7 +51,7 @@ GEM
49
51
  nenv (~> 0.1)
50
52
  shellany (~> 0.0)
51
53
  parallel (1.12.1)
52
- parser (2.5.0.4)
54
+ parser (2.5.0.5)
53
55
  ast (~> 2.4.0)
54
56
  powerpack (0.1.1)
55
57
  pry (0.11.3)
@@ -72,8 +74,8 @@ GEM
72
74
  rspec-mocks (3.7.0)
73
75
  diff-lcs (>= 1.2.0, < 2.0)
74
76
  rspec-support (~> 3.7.0)
75
- rspec-support (3.7.0)
76
- rubocop (0.53.0)
77
+ rspec-support (3.7.1)
78
+ rubocop (0.54.0)
77
79
  parallel (~> 1.10)
78
80
  parser (>= 2.5)
79
81
  powerpack (~> 0.1)
@@ -83,6 +85,11 @@ GEM
83
85
  ruby-progressbar (1.9.0)
84
86
  ruby_dep (1.5.0)
85
87
  shellany (0.0.1)
88
+ simplecov (0.16.1)
89
+ docile (~> 1.1)
90
+ json (>= 1.8, < 3)
91
+ simplecov-html (~> 0.10.0)
92
+ simplecov-html (0.10.2)
86
93
  thor (0.20.0)
87
94
  thread_safe (0.3.6)
88
95
  tzinfo (1.2.5)
@@ -100,6 +107,7 @@ DEPENDENCIES
100
107
  i18n-magic!
101
108
  rake (~> 10.0)
102
109
  rspec (~> 3.0)
110
+ simplecov
103
111
 
104
112
  BUNDLED WITH
105
113
  1.16.1
data/Guardfile CHANGED
@@ -69,7 +69,7 @@ guard :rspec, cmd: "bundle exec rspec" do
69
69
  end
70
70
  end
71
71
 
72
- guard :rubocop do
73
- watch(%r{.+\.rb$})
74
- watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
75
- end
72
+ # guard :rubocop do
73
+ # watch(%r{.+\.rb$})
74
+ # watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
75
+ # end
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # I18n::Magic
2
2
 
3
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/479e81db41acc3f00d92/maintainability)](https://codeclimate.com/github/a-smadi/i18n-magic/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/479e81db41acc3f00d92/test_coverage)](https://codeclimate.com/github/a-smadi/i18n-magic/test_coverage)
4
5
  [![Build Status](https://travis-ci.org/a-smadi/i18n-magic.svg?branch=master)](https://travis-ci.org/a-smadi/i18n-magic)
5
6
 
6
7
  Easy localization management for Ruby projects that are using the i18n gem.
@@ -31,7 +32,39 @@ You can run tests using `rake` or `rake spec`
31
32
 
32
33
  ## Usage
33
34
 
34
- This section will be updated as features roll out.
35
+ Currently theres only a single command available; `add` which automatically adds the translation record alphabetically ordered into the file it reckons suitable.
36
+
37
+ The `add` command may be used in 2 different ways:
38
+
39
+ 1- Adding translations as input after running the command. (useful when adding translations with special characters)
40
+
41
+ ```
42
+ i18n-magic add hello_world
43
+ ```
44
+ and the user will be prompted to enter translations for that key after running the command.
45
+
46
+ PS: The key can be in snake_case or CamelCase.
47
+
48
+ ```
49
+ i18n-magic add hello_world
50
+ ```
51
+ and
52
+ ```
53
+ i18n-magic add HelloWorld
54
+ ```
55
+ are both acceptable.
56
+
57
+
58
+ 2- Adding multiple translation values using a single command. (experimental feature / may be faulty for projects handling locales with somewhat similar alphabets)
59
+
60
+ ```
61
+ i18n-magic add hello_world "Hello, World!" "مرحبا"
62
+ ```
63
+ PS: precednece of values doesn't have any effect. As long as the key comes first, i18n-magic will automatically recognise the translation values locales, so
64
+ ```
65
+ i18n-magic add hello_world "مرحبا" "Hello, World!"
66
+ ```
67
+ is also acceptable.
35
68
 
36
69
  ## License
37
70
 
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "guard"
37
37
  spec.add_development_dependency "guard-rspec"
38
38
  spec.add_development_dependency "guard-rubocop"
39
+ spec.add_development_dependency "simplecov"
39
40
 
40
41
  spec.add_dependency "activesupport"
41
42
  end
@@ -63,7 +63,7 @@ module I18n
63
63
  end
64
64
 
65
65
  def add
66
- I18n::Magic::Commands::Add.new(key: @args[1], values: @args[2..-1]).execute
66
+ I18n::Magic::Commands::Add.new(key: @args[1], values: @args[2..-1], locale_files_path: I18n::Magic::Helpers::Environment::LOCALE_FILES_PATH).execute
67
67
  end
68
68
 
69
69
  def server
@@ -20,12 +20,12 @@ module I18n
20
20
  private
21
21
 
22
22
  def add_values_from_input
23
- I18n::Magic::Helpers::Environment.locales.each do |locale|
24
- print "#{locale} for #{@options[:key]} (empty to add skip) : "
23
+ I18n::Magic::Helpers::Environment.locales(@options[:locale_files_path]).each do |locale|
24
+ print "#{locale} value for #{@options[:key]} (empty to skip) : "
25
25
  value = STDIN.gets
26
26
  record = I18n::Magic::Entity::TranslationRecord.new(@options[:key], value)
27
27
  next unless record.valid?
28
- translation_file = I18n::Magic::Entity::TranslationFile.new(locale)
28
+ translation_file = I18n::Magic::Entity::TranslationFile.new(locale, @options[:locale_files_path])
29
29
  abort('translation file does not exist !') unless translation_file.exists?
30
30
  translation_file.add(record)
31
31
  end
@@ -36,8 +36,8 @@ module I18n
36
36
  next unless value.present?
37
37
  record = I18n::Magic::Entity::TranslationRecord.new(@options[:key], value)
38
38
  abort('invalid key,value pair !') unless record.valid?
39
- locale = I18n::Magic::Helpers::StringOps.locale(value)
40
- translation_file = I18n::Magic::Entity::TranslationFile.new(locale)
39
+ locale = I18n::Magic::Helpers::StringOps.locale(value, @options[:locale_files_path])
40
+ translation_file = I18n::Magic::Entity::TranslationFile.new(locale, @options[:locale_files_path])
41
41
  abort('translation file does not exist !') unless translation_file.exists?
42
42
  translation_file.add(record)
43
43
  end
@@ -6,8 +6,19 @@ module I18n
6
6
  class Alphabet
7
7
  attr_reader :letters
8
8
 
9
- def initialize
10
- @letters = %w[]
9
+ EN_LETTERS = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
10
+ AR_LETTERS = %w[ا ب ج د ه و ز ح ط ي ك ل لا م ن س ع ف ص ق ر ش ت ث خ ذ ض ظ غ].freeze
11
+ RU_LETTERS = %w[а б в г д е ё ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ыь э ю я і ѳ ѣ ѵ ѕ ѯ ѱ ѡ ѫ ѧ ѭ ѩ Ѩ Ѭ Ѧ Ѫ Ѡ Ѱ Ѯ Ѕ Ѵ Ѣ Ѳ І Я Ю Э Ь Ы Ъ Щ Ш Ч Ц Х Ф У П Л Й И З Ж Ё Д Г Б].freeze
12
+ DE_LETTERS = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z ä ö ü Ä Ö Ü ß].freeze
13
+ FR_LETTERS = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z é è ç ë ò ô ö ù à â].freeze
14
+ ES_LETTERS = %w[a b c d e é f g h i í j k l m n ñ o ó p q r s t u ú v w x y z].freeze
15
+ HE_LETTERS = %w[א בּ ב ג ד ה ו ז ח ט י כּ כ ךּ ך ל מ ם נ ן ס ע פּ פ ף צ ץ ק ר שׁ שׂ תּ ת].freeze
16
+ ZH_LETTERS = %w[ㄅ ㄆ ㄇ ㄈ ㄉ ㄊ ㄋ ㄌ ㄍ ㄎ ㄏ ㄐ ㄑ ㄒ ㄓ ㄔ ㄕ ㄖ ㄗ ㄘ ㄙ ㄚ ㄛ ㄜ ㄝ ㄞ ㄟ ㄠ ㄡ ㄢ ㄣ ㄤ ㄥ ㄦ ㄧ ㄨ ㄩ ㄭ].freeze
17
+ JA_LETTERS = %w[あ い う え お か き く け こ が ぎ ぐ げ ご さ し す せ そ ざ じ ず ぜ ぞ た ち つ て と だ ぢ づ で ど な に ぬ ね の は ひ ふ へ ほ ば び ぶ べ ぼ ぱ ぴ ぷ ぺ ぽ ま み む め も や ゆ よ ら り る れ ろ わ を ん].freeze
18
+ TR_LETTERS = %w[a b c ç d e f g ğ h ı i j k l m n o ö p r s ş t u ü v y z Ü Ş Ö Ğ Ç].freeze
19
+
20
+ def initialize(locale = nil)
21
+ @letters = eval("#{locale.upcase}_LETTERS") rescue %w[]
11
22
  end
12
23
 
13
24
  def learn(sample_text)
@@ -6,9 +6,9 @@ module I18n
6
6
  module Magic
7
7
  module Entity
8
8
  class TranslationFile
9
- def initialize(locale)
9
+ def initialize(locale, locale_files_path = I18n::Magic::Helpers::Environment::LOCALE_FILES_PATH)
10
10
  @locale = locale
11
- @filename = "config/locales/#{locale.downcase}.yml"
11
+ @filename = "#{locale_files_path}#{locale.downcase}.yml"
12
12
  end
13
13
 
14
14
  def exists?
@@ -40,6 +40,14 @@ module I18n
40
40
  end
41
41
 
42
42
  def compatibility_score(text)
43
+ locale_alphabet = I18n::Magic::Entity::Alphabet.new(@locale)
44
+ locale_alphabet = learned_alphabet unless locale_alphabet.letters.any?
45
+ locale_alphabet.belonging_score(text)
46
+ end
47
+
48
+ private
49
+
50
+ def learned_alphabet
43
51
  locale_alphabet = I18n::Magic::Entity::Alphabet.new
44
52
  open(@filename, 'r+') do |file|
45
53
  %w[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z].each do |letter|
@@ -53,11 +61,9 @@ module I18n
53
61
  locale_alphabet.learn(sample_text)
54
62
  end
55
63
  end
56
- locale_alphabet.belonging_score(text)
64
+ locale_alphabet
57
65
  end
58
66
 
59
- private
60
-
61
67
  def find(keyword)
62
68
  count = 0
63
69
  File.open(@filename, 'r') do |file|
@@ -2,9 +2,12 @@ module I18n
2
2
  module Magic
3
3
  module Helpers
4
4
  class Environment
5
- def self.locales
6
- locale_files = Dir['config/locales/*.yml'].select { |f| f =~ %r/\/[a-z]{2}.yml/ }
7
- locale_files.map { |f| f.remove('config/locales/').remove('.yml').upcase }
5
+ LOCALE_FILES_PATH = 'config/locales/'.freeze
6
+ LOCALE_FILES_SAMPLE_PATH = 'spec/sample/config/locales/'.freeze
7
+
8
+ def self.locales(locale_files_path = LOCALE_FILES_PATH)
9
+ locale_files = Dir["#{locale_files_path}*.yml"].select { |f| f =~ %r/\/[a-z]{2}.yml/ }
10
+ locale_files.map { |f| f.remove(locale_files_path).remove('.yml').upcase }
8
11
  end
9
12
  end
10
13
  end
@@ -8,12 +8,12 @@ module I18n
8
8
  text.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
9
9
  end
10
10
 
11
- def self.locale(text)
12
- locales = I18n::Magic::Helpers::Environment.locales
11
+ def self.locale(text, locale_files_path = I18n::Magic::Helpers::Environment::LOCALE_FILES_PATH)
12
+ locales = I18n::Magic::Helpers::Environment.locales(locale_files_path)
13
13
  text_locale = locales[0]
14
14
  score = 0
15
15
  locales.each do |locale|
16
- locale_score = I18n::Magic::Entity::TranslationFile.new(locale).compatibility_score(text)
16
+ locale_score = I18n::Magic::Entity::TranslationFile.new(locale, locale_files_path).compatibility_score(text)
17
17
  if locale_score > score
18
18
  text_locale = locale
19
19
  score = locale_score
@@ -23,36 +23,7 @@ module I18n
23
23
  end
24
24
 
25
25
  def self.letters_only(text)
26
- text.tr('0-9', '')
27
- .tr('!', '')
28
- .tr('?', '')
29
- .tr('@', '')
30
- .tr('#', '')
31
- .tr('$', '')
32
- .tr('%', '')
33
- .tr('^', '')
34
- .tr('&', '')
35
- .tr('*', '')
36
- .tr('(', '')
37
- .tr(')', '')
38
- .tr('[', '')
39
- .tr(']', '')
40
- .tr('{', '')
41
- .tr('}', '')
42
- .tr(':', '')
43
- .tr(';', '')
44
- .tr('\'', '')
45
- .tr('"', '')
46
- .tr('`', '')
47
- .tr('\\', '')
48
- .tr('/', '')
49
- .tr('<', '')
50
- .tr('>', '')
51
- .tr('.', '')
52
- .tr(',', '')
53
- .tr('|', '')
54
- .tr(' ', '')
55
- .strip.downcase
26
+ text.gsub(%r/[0-9]|\^|\[|\]|\\|\||\/|[ !?.,@#$%&*(){}:'"`<>]/, '').strip.downcase
56
27
  end
57
28
  end
58
29
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Magic
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Smadi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-23 00:00:00.000000000 Z
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: activesupport
99
113
  requirement: !ruby/object:Gem::Requirement