phrase 0.2.0.beta.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,20 +11,34 @@ class Phrase::Backend::PhraseService
11
11
  end
12
12
 
13
13
  def translate(*args)
14
- key = lookup_normalized_key(*args)
15
- if Phrase.disabled? or key_is_blacklisted?(key[:key]) or (args.last.is_a?(Hash) and args.last[:resolve] == false)
14
+ if to_be_translated_without_phrase?(args)
16
15
  I18n.translate_without_phrase(*args)
17
16
  else
18
- phrase_delegate_for(key[:key], *args)
17
+ phrase_delegate_for(args)
19
18
  end
20
19
  end
21
20
 
22
21
  protected
23
- def key_is_blacklisted?(key)
22
+ def to_be_translated_without_phrase?(args)
23
+ Phrase.disabled? or has_been_given_blacklisted_key?(args) or has_been_forced_to_resolve_with_phrase?(args)
24
+ end
25
+
26
+ def has_been_given_blacklisted_key?(args)
27
+ key = given_key_from_args(args)
28
+ has_blacklist_entry_for_key?(key)
29
+ end
30
+
31
+ def has_been_forced_to_resolve_with_phrase?(args)
32
+ (args.last.is_a?(Hash) and args.last[:resolve] == false)
33
+ end
34
+
35
+ def given_key_from_args(args)
36
+ extract_normalized_key_from_args(args)
37
+ end
38
+
39
+ def has_blacklist_entry_for_key?(key)
24
40
  blacklisted_keys.each do |blacklisted_key|
25
- if key.to_s[/\A#{blacklisted_key.gsub("*", ".*")}\Z/].present?
26
- return true
27
- end
41
+ return true if present?(key.to_s[/\A#{blacklisted_key.gsub("*", ".*")}\Z/])
28
42
  end
29
43
  false
30
44
  end
@@ -37,66 +51,85 @@ protected
37
51
  @api_client ||= Phrase::Api::Client.new(Phrase.auth_token)
38
52
  end
39
53
 
40
- def phrase_delegate_for(key=nil, *args)
41
- return nil unless key.present?
54
+ def phrase_delegate_for(args)
55
+ key = given_key_from_args(args)
56
+ return nil unless present?(key)
42
57
  options = args[1].nil? ? {} : args[1]
43
58
  Phrase::Delegate.new(key, options)
44
59
  end
45
60
 
46
- def lookup_normalized_key(*args)
47
- caller = identify_caller
48
- duped_args = args.map { |item| item.is_a?(Symbol) ? item : item.dup }
61
+ def extract_normalized_key_from_args(args)
62
+ transformed_args = transform_args(args)
63
+ normalized_key(transformed_args)
64
+ end
65
+
66
+ def transform_args(args)
67
+ duped_args = args.map { |item| (item.is_a?(Symbol) or item.nil?) ? item : item.dup }
68
+ transform_args_based_on_caller(duped_args)
69
+ end
70
+
71
+
72
+ def normalized_key(duped_args)
73
+ splitted_args = split_args(duped_args)
74
+ key = I18n::Backend::Flatten.normalize_flat_keys(*splitted_args)
75
+ key.gsub!("..", ".")
76
+ key
77
+ end
78
+
79
+ def split_args(args)
80
+ options = options_from_args(args)
81
+ key ||= args.shift
82
+ locale = options.delete(:locale) || I18n.locale
83
+ return [locale, key, options[:scope], nil]
84
+ end
85
+
86
+ def options_from_args(args)
87
+ args.last.is_a?(Hash) ? args.pop : {}
88
+ end
89
+
90
+ def transform_args_based_on_caller(args)
91
+ translation_caller = identify_caller
49
92
 
50
- if caller && duped_args.first =~ /^\./
51
- duped_args = transform_args_for_caller(caller, *duped_args)
52
- end
93
+ if translation_caller and args.first =~ /^\./
94
+ options = options_from_args(args)
53
95
 
54
- new_args = split_args(*duped_args)
96
+ if not present?(options[:scope]) and present?(translation_caller)
97
+ options[:scope] = translation_caller
98
+ end
55
99
 
56
- normalized_key = I18n::Backend::Flatten.normalize_flat_keys(*new_args)
57
- normalized_key.gsub!("..", ".")
100
+ args.push(options)
101
+ parts = args.first.to_s.split(".").select { |e| not blank?(e) }
102
+ args[0] = parts[0] if parts.size == 1
103
+ end
58
104
 
59
- {:key => normalized_key}
105
+ args
60
106
  end
61
107
 
62
108
  def identify_caller
63
- caller = nil
64
- send(:caller)[0..6].each do |string|
65
- caller = match_caller(string) unless caller
109
+ translation_caller = nil
110
+ send(:caller)[0..6].each do |intermediate_caller|
111
+ translation_caller = calling_template(intermediate_caller) unless translation_caller
66
112
  end
67
113
 
68
- if caller && caller.present?
69
- find_lookup_scope(caller)
114
+ if present?(translation_caller)
115
+ find_lookup_scope(translation_caller)
70
116
  else
71
117
  nil
72
118
  end
73
119
  end
74
120
 
75
- def match_caller(string)
121
+ def calling_template(string)
76
122
  string.match(/(views)(\/.+)(?>:[0-9]+:in)/)
77
123
  end
78
124
 
79
- def split_args(*args)
80
- options = args.last.is_a?(Hash) ? args.pop : {}
81
- key ||= args.shift
82
- locale = options.delete(:locale) || I18n.locale
83
- return [locale, key, options[:scope], nil]
125
+ def blank?(str)
126
+ raise "blank?(str) can only be given a String or nil" unless str.is_a?(String) or str.nil?
127
+ str.nil? or str == ''
84
128
  end
85
129
 
86
- def transform_args_for_caller(caller, *args)
87
- _scope = caller
88
-
89
- options = args.last.is_a?(Hash) ? args.pop : {}
90
-
91
- if !options[:scope].presence && _scope.presence
92
- options[:scope] = _scope
93
- end
94
-
95
- args.push(options)
96
- parts = args.first.to_s.split(".").select { |e| !e.blank? }
97
- args[0] = parts[0] if parts.size == 1
98
-
99
- return args
130
+ def present?(str)
131
+ raise "present?(str) can only be given a String or nil" unless str.is_a?(String) or str.nil?
132
+ not blank?(str)
100
133
  end
101
134
 
102
135
  def find_lookup_scope(caller)
data/phrase.gemspec CHANGED
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "phrase"
7
- s.version = "0.2.0.beta.8"
7
+ s.version = "0.2.0"
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Dynport GmbH"]
10
10
  s.email = ["info@phraseapp.com"]
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.beta.8
5
- prerelease: 6
4
+ version: 0.2.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dynport GmbH
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-03 00:00:00.000000000 Z
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70300432260120 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70300432260120
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: addressable
27
- requirement: &70300432259400 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: 2.2.8
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70300432259400
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.2.8
36
46
  description: phrase allows you to edit translations inline, on the page itself. More
37
47
  information at phraseapp.com
38
48
  email:
@@ -83,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
93
  version: 1.3.6
84
94
  requirements: []
85
95
  rubyforge_project: phrase
86
- rubygems_version: 1.8.17
96
+ rubygems_version: 1.8.24
87
97
  signing_key:
88
98
  specification_version: 3
89
99
  summary: The best way to manage i18n.