translation 0.8.1 → 0.9

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: 3fbd1c4748d503e002b873b162bfd48a1aa12a28
4
- data.tar.gz: ef72d0d68caafc7178c3b62f542d951182a66e72
3
+ metadata.gz: 96b136aa820f7455c6c3af78bf5098fd73393a8e
4
+ data.tar.gz: 1eb107b37045580973bed903b04a8744fed5911b
5
5
  SHA512:
6
- metadata.gz: 0c5d25c298206303bf8fdd694ff7e224d249fde50527b012b28461600eeb4bd8366b3c429c3119dc2ea6769e1d40181f7339e1c9aa6f30ce6ccf3cb0f940a247
7
- data.tar.gz: 4356d86b960d5f5a62a7e28eae8a52837bc5088113daf16cff7f454e88f0168feada78b0d6fa7fea634623ff1fe6b23210955b6d14932f4fe6933ec30a3ea446
6
+ metadata.gz: 39064ef60e74ac879135ca2742f6193aa9531bc30f84393481934b301eace673089c9358242daf9fe211e886363683eeafad88267957c899aa7230b8a80450c4
7
+ data.tar.gz: 47dd370a7fce5fa558f005d7fa9bda3b7e9f60d7b716ee9ac7a4f959cfad4deba5ab1dcd29b7abfd0bb59f97ef75b44de3bfe14dd669946f9b4b528fe567262a
data/lib/translation.rb CHANGED
@@ -23,6 +23,7 @@ require 'translation_io/yaml_conversion'
23
23
 
24
24
  require 'translation_io/controller'
25
25
  require 'translation_io/extractor'
26
+ require 'translation_io/yaml_entry'
26
27
 
27
28
  module TranslationIO
28
29
  module Proxy
@@ -33,6 +34,9 @@ module TranslationIO
33
34
  attr_reader :config, :client
34
35
 
35
36
  def configure(&block)
37
+ ENV['LANG'] = 'en_US.UTF-8' if ENV['LANG'].blank?
38
+ ENV['LC_CTYPE'] = 'UTF-8' if ENV['LC_CTYPE'].blank?
39
+
36
40
  if Rails.env.development?
37
41
  GetText::TextDomainManager.cached = false
38
42
  end
@@ -10,19 +10,21 @@ module TranslationIO
10
10
  class BaseOperation
11
11
  attr_accessor :client, :params
12
12
 
13
- def initialize(client, perform_real_requests = true)
13
+ def initialize(client)
14
14
  @client = client
15
- @params = {
16
- 'gem_version' => TranslationIO.version,
17
- 'source_language' => TranslationIO.config.source_locale.to_s,
18
- 'target_languages[]' => TranslationIO.config.target_locales.map(&:to_s)
19
- }
15
+ @params = {}
20
16
  end
21
17
 
22
18
  private
23
19
 
24
- def perform_request(uri, params)
20
+ def self.perform_request(uri, params)
25
21
  begin
22
+ params.merge!({
23
+ 'gem_version' => TranslationIO.version,
24
+ 'source_language' => TranslationIO.config.source_locale.to_s,
25
+ 'target_languages[]' => TranslationIO.config.target_locales.map(&:to_s)
26
+ })
27
+
26
28
  http = Net::HTTP.new(uri.host, uri.port)
27
29
  http.read_timeout = 500
28
30
 
@@ -21,11 +21,11 @@ module TranslationIO
21
21
  end
22
22
 
23
23
  all_flat_special_translations = all_flat_translations.select do |key, value|
24
- !value.is_a?(String) && !value.nil?
24
+ YamlEntry.localization?(key, value)
25
25
  end
26
26
 
27
27
  source_flat_special_translations = all_flat_special_translations.select do |key|
28
- key.start_with?("#{@source_locale}.")
28
+ YamlEntry.from_locale?(key, @source_locale) && !YamlEntry.ignored?(key)
29
29
  end
30
30
 
31
31
  @target_locales.each do |target_locale|
@@ -45,6 +45,29 @@ module TranslationIO
45
45
  file.write(yaml_data)
46
46
  end
47
47
  end
48
+
49
+ if not TranslationIO.config.test
50
+ # Get YAML localization entries
51
+ params = {}
52
+ @target_locales.each do |target_locale|
53
+ yaml_path = File.join(@yaml_locales_path, "localization.#{target_locale}.yml")
54
+ params["yaml_data_#{target_locale}"] = File.read(yaml_path)
55
+ end
56
+
57
+ TranslationIO.info "Collecting YAML localization entries"
58
+ uri = URI("http://#{TranslationIO.client.endpoint}/projects/#{TranslationIO.client.api_key}/fill_yaml_localizations")
59
+ parsed_response = BaseOperation.perform_request(uri, params)
60
+
61
+ unless parsed_response.nil?
62
+ @target_locales.each do |target_locale|
63
+ yaml_path = File.join(@yaml_locales_path, "localization.#{target_locale}.yml")
64
+
65
+ File.open(yaml_path, 'wb') do |file|
66
+ file.write(parsed_response["yaml_data_#{target_locale}"])
67
+ end
68
+ end
69
+ end
70
+ end
48
71
  end
49
72
 
50
73
  end
@@ -29,7 +29,7 @@ module TranslationIO
29
29
 
30
30
  TranslationIO.info "Sending data to server"
31
31
  uri = URI("http://#{client.endpoint}/projects/#{client.api_key}/init")
32
- parsed_response = perform_request(uri, params)
32
+ parsed_response = BaseOperation.perform_request(uri, params)
33
33
 
34
34
  unless parsed_response.nil?
35
35
  BaseOperation::SaveNewPoFilesStep.new(target_locales, locales_path, parsed_response).run
@@ -11,18 +11,7 @@ module TranslationIO
11
11
 
12
12
  def run
13
13
  @yaml_file_paths.each do |locale_file_path|
14
- in_project = locale_file_path_in_project?(locale_file_path)
15
-
16
- protected_file = @target_locales.any? do |target_locale|
17
- paths = [
18
- File.join(@yaml_locales_path, "translation.#{target_locale}.yml").to_s,
19
- File.join(@yaml_locales_path, "localization.#{target_locale}.yml").to_s
20
- ]
21
-
22
- paths.include?(locale_file_path)
23
- end
24
-
25
- if in_project && !protected_file
14
+ if locale_file_removable?(locale_file_path)
26
15
  if File.exist?(locale_file_path)
27
16
  content_hash = YAML::load(File.read(locale_file_path))
28
17
  new_content_hash = content_hash.keep_if { |k| k.to_s == @source_locale.to_s }
@@ -43,6 +32,21 @@ module TranslationIO
43
32
 
44
33
  private
45
34
 
35
+ def locale_file_removable?(locale_file_path)
36
+ in_project = locale_file_path_in_project?(locale_file_path)
37
+
38
+ protected_file = @target_locales.any? do |target_locale|
39
+ paths = [
40
+ TranslationIO.normalize_path(File.join(@yaml_locales_path, "translation.#{target_locale}.yml" ).to_s),
41
+ TranslationIO.normalize_path(File.join(@yaml_locales_path, "localization.#{target_locale}.yml").to_s)
42
+ ]
43
+
44
+ paths.include?(TranslationIO.normalize_path(locale_file_path))
45
+ end
46
+
47
+ in_project && !protected_file
48
+ end
49
+
46
50
  def locale_file_path_in_project?(locale_file_path)
47
51
  TranslationIO.normalize_path(locale_file_path).start_with?(
48
52
  TranslationIO.normalize_path(@yaml_locales_path)
@@ -18,11 +18,11 @@ module TranslationIO
18
18
  end
19
19
 
20
20
  all_flat_string_translations = all_flat_translations.select do |key, value|
21
- key.present? && value.is_a?(String) && !value.nil?
21
+ YamlEntry.string?(key, value) && !YamlEntry.localization?(key, value)
22
22
  end
23
23
 
24
24
  source_flat_string_tanslations = all_flat_string_translations.select do |key|
25
- key.start_with?("#{@source_locale}.") && !key.start_with?("#{@source_locale}.faker.")
25
+ YamlEntry.from_locale?(key, @source_locale) && !YamlEntry.ignored?(key)
26
26
  end
27
27
 
28
28
  @target_locales.each do |target_locale|
@@ -33,7 +33,7 @@ module TranslationIO
33
33
  msgid = value
34
34
  msgstr = all_flat_string_translations[target_key]
35
35
 
36
- unless msgid.blank?
36
+ unless msgid.to_s.empty?
37
37
  po_entry = GetText::POEntry.new(:msgctxt)
38
38
  po_entry.msgid = msgid
39
39
  po_entry.msgstr = msgstr
@@ -29,7 +29,7 @@ module TranslationIO
29
29
  end
30
30
 
31
31
  uri = URI("http://#{client.endpoint}/projects/#{client.api_key}/sync")
32
- parsed_response = perform_request(uri, params)
32
+ parsed_response = BaseOperation.perform_request(uri, params)
33
33
 
34
34
  unless parsed_response.nil?
35
35
  BaseOperation::SaveNewPoFilesStep.new(target_locales, locales_path, parsed_response).run
@@ -19,7 +19,7 @@ module TranslationIO
19
19
  end
20
20
 
21
21
  source_flat_string_tanslations = all_flat_translations.select do |key, value|
22
- value.is_a?(String) && key.start_with?("#{@source_locale}.") && !key.start_with?("#{@source_locale}.faker.")
22
+ YamlEntry.string?(key, value) && YamlEntry.from_locale?(key, @source_locale) && !YamlEntry.ignored?(key) && !YamlEntry.localization?(key, value)
23
23
  end
24
24
 
25
25
  pot_representation = GetText::PO.new
@@ -27,7 +27,7 @@ module TranslationIO
27
27
  source_flat_string_tanslations.each_pair do |key, value|
28
28
  msgid = value
29
29
 
30
- unless msgid.blank?
30
+ unless msgid.to_s.empty?
31
31
  pot_entry = GetText::POEntry.new(:msgctxt)
32
32
  pot_entry.msgid = msgid
33
33
  pot_entry.msgstr = ''
@@ -4,6 +4,7 @@ module TranslationIO
4
4
  attr_accessor :source_locale, :target_locales
5
5
  attr_accessor :endpoint
6
6
  attr_accessor :verbose
7
+ attr_accessor :test
7
8
 
8
9
  def initialize
9
10
  self.locales_path = File.join('config', 'locales', 'gettext')
@@ -11,6 +12,7 @@ module TranslationIO
11
12
  self.target_locales = []
12
13
  self.endpoint = 'api.translation.io/api'
13
14
  self.verbose = 1
15
+ self.test = false
14
16
  end
15
17
 
16
18
  def pot_path
@@ -19,7 +21,7 @@ module TranslationIO
19
21
 
20
22
  def yaml_file_paths
21
23
  I18n.load_path.select do |p|
22
- File.exist?(p)
24
+ File.exist?(p) && (File.extname(p) == '.yml' || File.extname(p) == '.yaml')
23
25
  end
24
26
  end
25
27
 
@@ -7,7 +7,7 @@ module TranslationIO
7
7
  request.env['HTTP_ACCEPT_LANGUAGE'] ||
8
8
  I18n.default_locale
9
9
 
10
- if requested_locale.to_sym.in?(I18n.available_locales)
10
+ if I18n.available_locales.include?(requested_locale.to_sym)
11
11
  session[:locale] = requested_locale
12
12
  I18n.locale = requested_locale
13
13
  else
@@ -2,6 +2,7 @@ module TranslationIO
2
2
  module FlatHash
3
3
  class << self
4
4
  def to_flat_hash(hash)
5
+ hash = brackets_to_joker(hash)
5
6
  get_flat_hash_for_level(hash)
6
7
  end
7
8
 
@@ -12,7 +13,7 @@ module TranslationIO
12
13
  build_hash_with_flat(hash, key, value)
13
14
  end
14
15
 
15
- hash
16
+ joker_to_brackets(hash)
16
17
  end
17
18
 
18
19
  private
@@ -106,6 +107,30 @@ module TranslationIO
106
107
  flat_hash
107
108
  end
108
109
 
110
+ def brackets_to_joker(h)
111
+ gsub_keys!(h, '[', ']', '<@~<', '>@~>')
112
+ end
113
+
114
+ def joker_to_brackets(h)
115
+ gsub_keys!(h, '<@~<', '>@~>', '[', ']')
116
+ end
117
+
118
+ def gsub_keys!(h, from_1, from_2, to_1, to_2)
119
+ if h.is_a?(Hash)
120
+ h.keys.each do |key|
121
+ if key.to_s.include?(from_1) || key.to_s.include?(from_2)
122
+ new_key = key.to_s.gsub(from_1, to_1).gsub(from_2, to_2)
123
+ else
124
+ new_key = key
125
+ end
126
+ h[new_key] = h.delete(key)
127
+ gsub_keys!(h[new_key], from_1, from_2, to_1, to_2)
128
+ end
129
+ elsif h.respond_to?(:each)
130
+ h.each { |e| gsub_keys!(e, from_1, from_2, to_1, to_2) }
131
+ end
132
+ h
133
+ end
109
134
  end
110
135
  end
111
136
  end
@@ -0,0 +1,51 @@
1
+ module YamlEntry
2
+
3
+ IGNORED_KEY_PREFIXES = [
4
+ 'faker.'
5
+ ]
6
+
7
+ LOCALIZATION_KEY_PREFIXES = [
8
+ 'date.formats',
9
+ 'date.order',
10
+ 'time.formats',
11
+ 'support.array',
12
+ 'number.format',
13
+ 'number.currency',
14
+ 'number.percentage',
15
+ 'number.precision',
16
+ 'number.human.format',
17
+ 'number.human.storage_units.format',
18
+ 'number.human.decimal_units.format',
19
+ 'number.human.decimal_units.units.unit'
20
+ ]
21
+
22
+ class << self
23
+ def string?(key, value)
24
+ key.present? && value.is_a?(String)
25
+ end
26
+
27
+ def from_locale?(key, locale)
28
+ key.present? && key.start_with?("#{locale}.")
29
+ end
30
+
31
+ def ignored?(key)
32
+ key.present? && IGNORED_KEY_PREFIXES.any? { |p| key_without_locale(key).start_with?(p) }
33
+ end
34
+
35
+ def localization?(key, value)
36
+ key.present? && (localization_prefix?(key) || (!YamlEntry.string?(key, value) && !value.nil?))
37
+ end
38
+
39
+ def localization_prefix?(key)
40
+ LOCALIZATION_KEY_PREFIXES.any? do |prefix|
41
+ key_without_locale(key).start_with?(prefix)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def key_without_locale(key)
48
+ key.split('.', 2).last
49
+ end
50
+ end
51
+ end
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.8.1
4
+ version: '0.9'
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-08-19 00:00:00.000000000 Z
12
+ date: 2014-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gettext
@@ -98,6 +98,7 @@ files:
98
98
  - lib/translation_io/railtie.rb
99
99
  - lib/translation_io/tasks.rb
100
100
  - lib/translation_io/yaml_conversion.rb
101
+ - lib/translation_io/yaml_entry.rb
101
102
  homepage: http://rails.translation.io
102
103
  licenses:
103
104
  - MIT
@@ -123,3 +124,4 @@ signing_key:
123
124
  specification_version: 4
124
125
  summary: Rails translation made _("simple") with YAML and GetText.
125
126
  test_files: []
127
+ has_rdoc: false