translation 0.7 → 0.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41e631d4dfa6720834486b6db11f51c5e2f03bc8
4
- data.tar.gz: 6dee4a9aa3a7dbfc9c075d3235323225eb0b1030
3
+ metadata.gz: fca22384fded316e68e7e9066f3c253846815ef1
4
+ data.tar.gz: eb4a3c2aea1cf607d8371fe0e0a3c41678be7803
5
5
  SHA512:
6
- metadata.gz: 1b9dd3d10d0e9a8f9de9752ac4b2526edf45cfcd19c3f03b1a196ad2b89957b261bfbc30e6a39b9e3dc9b304634eb6140988b53a43db8fe5adc0512b23112bd8
7
- data.tar.gz: ca54b082e005bd1b439d18e9dfba66069f1578377546f31f1a5444dd06284e7fd1d797a8e93634ecf1dfd62e7128782f2dfbd4123e5df17b7bb98f5ef3e45392
6
+ metadata.gz: 3787c5f789f6b09f368d987b4359ba1c1cf1e6eee7b15dc2497d5f3ef4488d8bbcff8e5a9d172184b88d2c9f021fbcc506519699bb582826f85fef16e58f5fef
7
+ data.tar.gz: fb65686fe5d214d89914b9f77fdf22712a8cdcfa8577b4ef9c4d79c32e3dfcebb582b442941cba979cb7306ad61ad893957811a295d2020f4dd288318e7c12d6
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # gem for [Rails TranslationIO](http://rails.translation.io)
1
+ # Ruby/Rails gem for [Translation.io](http://translation.io).
2
2
 
3
3
  ## Description
4
4
 
5
- Add this gem to your [Rails](http://rubyonrails.org) app to translate it with [Rails TranslationIO](http://rails.translation.io).
5
+ Add this gem to your [Rails](http://rubyonrails.org) app to translate it with [Translation.io](http://translation.io).
6
6
 
7
7
  ## Installation
8
8
 
@@ -21,7 +21,7 @@ And finish by inititalizing your translation project with :
21
21
 
22
22
  ## Synchronization
23
23
 
24
- To get new translations from Rails TranslationIO and to send new translatable strings, simply run :
24
+ To get new translations from Translation.io and to send new translatable strings, simply run :
25
25
 
26
26
  bundle exec rake translation:sync
27
27
 
@@ -33,7 +33,7 @@ If you need to remove unused keys taking the current branch as reference :
33
33
 
34
34
  Note that this operation will also perform a sync at the same time.
35
35
 
36
- Warning : all keys that are not present in the current branch will be **permanently deleted both on Rails TranslationIO and in your app**.
36
+ Warning : all keys that are not present in the current branch will be **permanently deleted both on Translation.io and in your app**.
37
37
 
38
38
  ## Tests
39
39
 
@@ -47,4 +47,4 @@ To run the specs :
47
47
  The [translation gem](https://rubygems.org/gems/translation) in released under MIT license by [Aurélien Malisart](http://aurelien.malisart.be) and [Michaël Hoste](http://80limit.com) (see MIT-LICENSE
48
48
  file).
49
49
 
50
- (c) http://rails.translation.io
50
+ (c) http://translation.io
@@ -8,12 +8,11 @@ require 'gettext/text_domain_manager'
8
8
 
9
9
  module TranslationIO
10
10
  GETTEXT_METHODS = [
11
- :_, :n_, :p_, :s_, :np_, :ns_, :N_, :Nn,
12
- :gettext, :sgettext, :ngettext, :nsgettext, :pgettext, :npgettext
11
+ :nsgettext, :pgettext, :npgettext, :sgettext, :ngettext, :gettext,
12
+ :np_, :ns_, :Nn_, :n_, :p_, :s_, :N_, :_
13
13
  ]
14
14
 
15
- TEXT_DOMAIN = 'app'
16
- SOURCE_FILES_PATTERN = '**/*.{rb,erb,html.erb,xml.erb}'
15
+ TEXT_DOMAIN = 'app'
17
16
  end
18
17
 
19
18
  require 'translation_io/config'
@@ -23,6 +22,7 @@ require 'translation_io/flat_hash'
23
22
  require 'translation_io/yaml_conversion'
24
23
 
25
24
  require 'translation_io/controller'
25
+ require 'translation_io/extractor'
26
26
 
27
27
  module TranslationIO
28
28
  module Proxy
@@ -54,10 +54,6 @@ module TranslationIO
54
54
  return true
55
55
  end
56
56
 
57
- def pot_path
58
- File.join(TranslationIO.config.locales_path, "#{TEXT_DOMAIN}.pot")
59
- end
60
-
61
57
  def info(message, level = 0, verbose_level = 0)
62
58
  verbose = @config.try(:verbose) || 0
63
59
  if verbose >= verbose_level
@@ -10,8 +10,6 @@ module TranslationIO
10
10
  class BaseOperation
11
11
  attr_accessor :client, :params
12
12
 
13
- GETTEXT_ENTRY_RE = Regexp.new('(?:' + TranslationIO::GETTEXT_METHODS.join('|') + ')\(\[?(?:".+?"(?:\s*,\s*)?)+\]?(?:[^)]*)?\)')
14
-
15
13
  def initialize(client, perform_real_requests = true)
16
14
  @client = client
17
15
  @params = {
@@ -45,6 +43,10 @@ module TranslationIO
45
43
  $stderr.puts "[Error] Server not responding."
46
44
  end
47
45
  end
46
+
47
+ def cleanup
48
+ FileUtils.rm_rf(File.join('tmp', 'translation'))
49
+ end
48
50
  end
49
51
  end
50
52
  end
@@ -7,11 +7,13 @@ module TranslationIO
7
7
  end
8
8
 
9
9
  def run
10
- if @haml_source_files.any? && defined?(Haml)
10
+ if @haml_source_files.any?
11
11
  TranslationIO.info "Extracting Gettext entries from HAML files."
12
12
 
13
- File.open(File.join('tmp', 'translation-haml-gettext.rb'), 'w') do |file|
14
- extracted_gettext_entries.each do |entry|
13
+ FileUtils.mkdir_p(File.join('tmp', 'translation'))
14
+
15
+ extracted_gettext_entries.each_with_index do |entry, index|
16
+ File.open(File.join('tmp', 'translation', "haml-gettext-#{index.to_s.rjust(8,'0')}.rb"), 'w') do |file|
15
17
  file.puts "#{entry}"
16
18
  end
17
19
  end
@@ -27,16 +29,7 @@ module TranslationIO
27
29
  TranslationIO.info haml_file_path, 2, 2
28
30
 
29
31
  haml_data = File.read(haml_file_path)
30
-
31
- begin
32
- ruby_data = Haml::Engine.new(haml_data).precompiled
33
-
34
- ruby_data.scan(GETTEXT_ENTRY_RE).each do |entry|
35
- entries << entry
36
- end
37
- rescue Haml::SyntaxError
38
- TranslationIO.info "File cannot be parsed (SyntaxError): #{haml_file_path}", 1, 0
39
- end
32
+ entries += TranslationIO::Extractor.extract(haml_data)
40
33
  end
41
34
 
42
35
  TranslationIO.info "#{entries.size} entries found", 2, 2
@@ -7,11 +7,13 @@ module TranslationIO
7
7
  end
8
8
 
9
9
  def run
10
- if @slim_source_files.any? && defined?(Slim)
10
+ if @slim_source_files.any?
11
11
  TranslationIO.info "Extracting Gettext entries from SLIM files."
12
12
 
13
- File.open(File.join('tmp', 'translation-slim-gettext.rb'), 'w') do |file|
14
- extracted_gettext_entries.each do |entry|
13
+ FileUtils.mkdir_p(File.join('tmp', 'translation'))
14
+
15
+ extracted_gettext_entries.each_with_index do |entry, index|
16
+ File.open(File.join('tmp', 'translation', "slim-gettext-#{index.to_s.rjust(8,'0')}.rb"), 'w') do |file|
15
17
  file.puts "#{entry}"
16
18
  end
17
19
  end
@@ -26,15 +28,8 @@ module TranslationIO
26
28
  @slim_source_files.each do |slim_file_path|
27
29
  TranslationIO.info slim_file_path, 2, 2
28
30
 
29
- begin
30
- ruby_data = Slim::Template.new(slim_file_path, {}).precompiled_template
31
-
32
- ruby_data.scan(GETTEXT_ENTRY_RE).each do |entry|
33
- entries << entry
34
- end
35
- rescue Slim::Parser::SyntaxError
36
- TranslationIO.info "File cannot be parsed (SyntaxError): #{slim_file_path}", 1, 0
37
- end
31
+ slim_data = File.read(slim_file_path)
32
+ entries += TranslationIO::Extractor.extract(slim_data)
38
33
  end
39
34
 
40
35
  TranslationIO.info "#{entries.size} entries found", 2, 2
@@ -7,19 +7,21 @@ module TranslationIO
7
7
  class Client
8
8
  class InitOperation < BaseOperation
9
9
  def run
10
- haml_source_files = Dir['**/*.{haml}'].select { |p| !p.start_with?('vendor/') }
11
- slim_source_files = Dir['**/*.{slim}'].select { |p| !p.start_with?('vendor/') }
10
+ config = TranslationIO.config
11
+
12
+ haml_source_files = config.haml_source_files
13
+ slim_source_files = config.slim_source_files
12
14
 
13
15
  BaseOperation::DumpHamlGettextKeysStep.new(haml_source_files).run
14
16
  BaseOperation::DumpSlimGettextKeysStep.new(slim_source_files).run
15
17
 
16
- source_files = Dir[SOURCE_FILES_PATTERN].select { |p| !p.start_with?('vendor/') }
17
- pot_path = TranslationIO.pot_path
18
- source_locale = TranslationIO.config.source_locale
19
- target_locales = TranslationIO.config.target_locales
20
- locales_path = TranslationIO.config.locales_path
21
- yaml_locales_path = 'config/locales'
22
- yaml_file_paths = I18n.load_path
18
+ source_files = config.source_files
19
+ pot_path = config.pot_path
20
+ source_locale = config.source_locale
21
+ target_locales = config.target_locales
22
+ locales_path = config.locales_path
23
+ yaml_locales_path = config.yaml_locales_path
24
+ yaml_file_paths = config.yaml_file_paths
23
25
 
24
26
  UpdatePotFileStep.new(pot_path, source_files).run
25
27
  UpdateAndCollectPoFilesStep.new(target_locales, pot_path, locales_path).run(params)
@@ -36,6 +38,8 @@ module TranslationIO
36
38
  CleanupYamlFilesStep.new(source_locale, target_locales, yaml_file_paths, yaml_locales_path).run
37
39
  BaseOperation::CreateNewMoFilesStep.new(locales_path).run
38
40
  end
41
+
42
+ cleanup
39
43
  end
40
44
  end
41
45
  end
@@ -23,17 +23,19 @@ module TranslationIO
23
23
  end
24
24
 
25
25
  if in_project && !protected_file
26
- content_hash = YAML::load(File.read(locale_file_path))
27
- new_content_hash = content_hash.keep_if { |k| k.to_s == @source_locale.to_s }
26
+ if File.exist?(locale_file_path)
27
+ content_hash = YAML::load(File.read(locale_file_path))
28
+ new_content_hash = content_hash.keep_if { |k| k.to_s == @source_locale.to_s }
28
29
 
29
- if new_content_hash.keys.any?
30
- TranslationIO.info "Rewriting #{locale_file_path}", 2, 2
31
- File.open(locale_file_path, 'wb') do |file|
32
- file.write(new_content_hash.to_yaml)
30
+ if new_content_hash.keys.any?
31
+ TranslationIO.info "Rewriting #{locale_file_path}", 2, 2
32
+ File.open(locale_file_path, 'wb') do |file|
33
+ file.write(new_content_hash.to_yaml)
34
+ end
35
+ else
36
+ TranslationIO.info "Removing #{locale_file_path}", 2, 2
37
+ FileUtils.rm(locale_file_path)
33
38
  end
34
- else
35
- TranslationIO.info "Removing #{locale_file_path}", 2, 2
36
- FileUtils.rm(locale_file_path)
37
39
  end
38
40
  end
39
41
  end
@@ -5,19 +5,21 @@ module TranslationIO
5
5
  class Client
6
6
  class SyncOperation < BaseOperation
7
7
  def run(purge = false)
8
- haml_source_files = Dir['**/*.{haml}'].select { |p| !p.start_with?('vendor/') }
9
- slim_source_files = Dir['**/*.{slim}'].select { |p| !p.start_with?('vendor/') }
8
+ config = TranslationIO.config
9
+
10
+ haml_source_files = config.haml_source_files
11
+ slim_source_files = config.slim_source_files
10
12
 
11
13
  BaseOperation::DumpHamlGettextKeysStep.new(haml_source_files).run
12
14
  BaseOperation::DumpSlimGettextKeysStep.new(slim_source_files).run
13
15
 
14
- source_files = Dir[SOURCE_FILES_PATTERN].select { |p| !p.start_with?('vendor/') }
15
- pot_path = TranslationIO.pot_path
16
- source_locale = TranslationIO.config.source_locale
17
- target_locales = TranslationIO.config.target_locales
18
- locales_path = TranslationIO.config.locales_path
19
- yaml_locales_path = 'config/locales'
20
- yaml_file_paths = I18n.load_path
16
+ source_files = config.source_files
17
+ pot_path = config.pot_path
18
+ source_locale = config.source_locale
19
+ target_locales = config.target_locales
20
+ locales_path = config.locales_path
21
+ yaml_locales_path = config.yaml_locales_path
22
+ yaml_file_paths = config.yaml_file_paths
21
23
 
22
24
  UpdateAndCollectPotFileStep.new(pot_path, source_files).run(params)
23
25
  CreateYamlPotFileStep.new(source_locale, yaml_file_paths).run(params)
@@ -35,6 +37,8 @@ module TranslationIO
35
37
  BaseOperation::SaveNewYamlFilesStep.new(target_locales, yaml_locales_path, parsed_response).run
36
38
  BaseOperation::SaveSpecialYamlFilesStep.new(source_locale, target_locales, yaml_locales_path, yaml_file_paths).run
37
39
  end
40
+
41
+ cleanup
38
42
  end
39
43
  end
40
44
  end
@@ -13,6 +13,42 @@ module TranslationIO
13
13
  self.verbose = 1
14
14
  end
15
15
 
16
+ def pot_path
17
+ File.join(locales_path, "#{TEXT_DOMAIN}.pot")
18
+ end
19
+
20
+ def yaml_file_paths
21
+ I18n.load_path.select do |p|
22
+ File.exist?(p)
23
+ end
24
+ end
25
+
26
+ def source_files
27
+ paths = Dir['**/*.{rb,erb,html.erb,xml.erb}'].select do |p|
28
+ !p.start_with?('vendor/') && !p.start_with?('tmp/')
29
+ end
30
+
31
+ paths += Dir['tmp/translation/*.rb']
32
+
33
+ paths
34
+ end
35
+
36
+ def yaml_locales_path
37
+ 'config/locales'
38
+ end
39
+
40
+ def haml_source_files
41
+ Dir['**/*.{haml}'].select do |p|
42
+ !p.start_with?('vendor/') && !p.start_with?('tmp/')
43
+ end
44
+ end
45
+
46
+ def slim_source_files
47
+ Dir['**/*.{slim}'].select do |p|
48
+ !p.start_with?('vendor/') && !p.start_with?('tmp/')
49
+ end
50
+ end
51
+
16
52
  def to_s
17
53
  "#{api_key} - #{source_locale} => #{target_locales.join(' + ')}"
18
54
  end
@@ -0,0 +1,22 @@
1
+ module TranslationIO
2
+ module Extractor
3
+ # visual: https://www.debuggex.com/r/fYSQ-jwQfTjhhE6T
4
+ # .*? is non-greedy (lazy) match : http://stackoverflow.com/a/1919995/1243212
5
+ REGEXP_INSIDE_1 = '\s*(?:\[?(?:(?:(?:".*?")|(?:\'.*?\'))\s*?,?\s*?){1}\]?)\s*?'
6
+ REGEXP_INSIDE_2 = '\s*(?:\[?(?:(?:(?:".*?")|(?:\'.*?\'))\s*?,?\s*?){2}\]?),?\s*?.*?\s*'
7
+ REGEXP_INSIDE_2B = '\s*(?:\[?(?:(?:(?:".*?")|(?:\'.*?\'))\s*?,?\s*?){2}\]?)\s*?'
8
+ REGEXP_INSIDE_3 = '\s*(?:\[?(?:(?:(?:".*?")|(?:\'.*?\'))\s*?,?\s*?){3}\]?),?\s*?.*?\s*'
9
+
10
+ REGEXP_1 = '(?:sgettext|gettext|N_|s_|_)\s*(?:\(' + REGEXP_INSIDE_1 + '\)|' + REGEXP_INSIDE_1 + ')'
11
+ REGEXP_2 = '(?:nsgettext|ngettext|ns_|Nn_|n_)\s*(?:\(' + REGEXP_INSIDE_2 + '\)|' + REGEXP_INSIDE_2 + ')'
12
+ REGEXP_2B = '(?:pgettext|p_)\s*(?:\(' + REGEXP_INSIDE_2B + '\)|' + REGEXP_INSIDE_2B + ')'
13
+ REGEXP_3 = '(?:npgettext|np_)\s*(?:\(' + REGEXP_INSIDE_3 + '\)|' + REGEXP_INSIDE_3 + ')'
14
+
15
+ GETTEXT_REGEXP = Regexp.new('(?:' + REGEXP_3 + '|' + REGEXP_2B + '|' + REGEXP_2 + '|' + REGEXP_1 + ')')
16
+
17
+ def self.extract(code_to_parse)
18
+ code_to_parse.scan(GETTEXT_REGEXP)
19
+ end
20
+ end
21
+ end
22
+
@@ -2,29 +2,6 @@ module TranslationIO
2
2
  module YAMLConversion
3
3
  class << self
4
4
 
5
- def get_pot_data_from_yaml(source_locale = nil, yaml_file_paths = nil)
6
- source_locale = TranslationIO.config.source_locale if source_locale.blank?
7
- yaml_file_paths = I18n.load_path if yaml_file_paths.nil?
8
-
9
- source_translations = YAMLConversion.get_flat_translations_for_locale(source_locale, yaml_file_paths)
10
- pot_representation = GetText::PO.new
11
-
12
- source_translations.each_pair do |key, value|
13
- msgid = value
14
-
15
- unless msgid.blank?
16
- pot_entry = GetText::POEntry.new(:msgctxt)
17
- pot_entry.msgid = msgid
18
- pot_entry.msgctxt = key.split('.', 2).last
19
- #pot_entry.references = [ value[:locale_file_path] ]
20
-
21
- pot_representation[ pot_entry.msgctxt, pot_entry.msgid ] = pot_entry
22
- end
23
- end
24
-
25
- pot_representation.to_s
26
- end
27
-
28
5
  def get_yaml_data_from_po_data(po_data, target_locale)
29
6
  parser = GetText::POParser.new
30
7
  po_representation = GetText::PO.new
@@ -43,24 +20,6 @@ module TranslationIO
43
20
 
44
21
  # Shortcut methods
45
22
 
46
- def get_flat_translations_for_locale(locale, yaml_file_paths = nil)
47
- yaml_file_paths = I18n.load_path if yaml_file_paths.blank?
48
-
49
- all_flat_translations = {}
50
-
51
- yaml_file_paths.each do |yaml_file_path|
52
- content = File.read(yaml_file_path)
53
- translations = YAML::load(content)
54
-
55
- if translations.has_key?(locale.to_s)
56
- flat_translations = FlatHash.to_flat_hash(translations)
57
- all_flat_translations.merge!(flat_translations)
58
- end
59
- end
60
-
61
- return all_flat_translations
62
- end
63
-
64
23
  def get_flat_translations_for_yaml_file(file_path)
65
24
  yaml_data = File.read(file_path)
66
25
  return get_flat_translations_for_yaml_data(yaml_data)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translation
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: '0.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurelien Malisart
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-31 00:00:00.000000000 Z
12
+ date: 2014-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gettext
@@ -67,35 +67,9 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: haml
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: slim
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
- description: translation.io connector
70
+ description: Rails translation made _("simple"). Use "gem 'translation'" to translate
71
+ with _("plain text") or with t("lots.of.keys"). Type "rake translation:sync" to
72
+ synchronize with your translators on Translation.io
99
73
  email: contact@translation.io
100
74
  executables: []
101
75
  extensions: []
@@ -121,6 +95,7 @@ files:
121
95
  - lib/translation_io/client/sync_operation/update_and_collect_pot_file_step.rb
122
96
  - lib/translation_io/config.rb
123
97
  - lib/translation_io/controller.rb
98
+ - lib/translation_io/extractor.rb
124
99
  - lib/translation_io/flat_hash.rb
125
100
  - lib/translation_io/railtie.rb
126
101
  - lib/translation_io/tasks.rb
@@ -148,6 +123,5 @@ rubyforge_project:
148
123
  rubygems_version: 2.2.0
149
124
  signing_key:
150
125
  specification_version: 4
151
- summary: translation.io connector
126
+ summary: Rails translation made _("simple") with YAML and GetText.
152
127
  test_files: []
153
- has_rdoc: false