phrase 0.0.1 → 0.1

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.
@@ -1,115 +1,86 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module Phrase::Backend::Base
4
- PREFIX = "{{__phrase_"
5
- SUFFIX = "__}}"
6
-
7
- attr_accessor :config
8
4
 
9
5
  def translate(*args)
10
- key = lookup_normalized_key(*args)
11
- case key[:translation]
12
- when String, nil, "" then decorate_translation(key[:key])
13
- else key[:translation]
6
+ if Phrase.enabled?
7
+ key = lookup_normalized_key(*args)
8
+ decorate_translation(key[:key])
9
+ else
10
+ I18n.translate_without_phrase(*args)
14
11
  end
15
12
  end
16
-
17
- def load_translations(*args)
18
- raise NotImplementedError
19
- end
20
-
21
- def store_translation(locale, key, translation, options = {})
22
- I18n.backend.load_translations unless I18n.backend.initialized?
23
- I18n.backend.store_translations(locale, key.expand_to_hash(translation))
24
- end
25
-
26
- def raw_lookup(locale, key, scope=[], options={})
27
- I18n.backend.load_translations unless I18n.backend.initialized?
28
- keys = I18n.normalize_keys(locale, key, scope, options[:separator])
29
- keys.inject(I18n.backend.send(:translations)) do |result, _key|
30
- _key = _key.to_sym
31
- return nil unless result.is_a?(Hash) && result.has_key?(_key)
32
- result[_key]
33
- end
34
- end
35
-
13
+
36
14
  protected
37
15
  def decorate_translation(key=nil)
38
16
  return nil unless key.presence
39
- "#{PREFIX}#{key}#{SUFFIX}"
17
+ "#{Phrase.prefix}phrase_#{key}#{Phrase.suffix}"
40
18
  end
41
19
 
42
- def translation_presence(*args)
43
- I18n.translate!(*args)
44
- rescue I18n::MissingTranslationData => e
45
- nil
46
- end
47
-
48
20
  def lookup_normalized_key(*args)
49
- translation = translation_presence(*args)
50
21
  caller = identify_caller
51
-
52
- if (translation.nil? || translation.is_a?(Hash)) && caller && args.first =~ /^\./
22
+
23
+ if caller && args.first =~ /^\./
53
24
  args = transform_args_for_caller(caller, *args)
54
- translation = translation_presence(*args)
55
25
  end
56
-
26
+
57
27
  new_args = split_args(*args)
58
-
28
+
59
29
  normalized_key = I18n::Backend::Flatten.normalize_flat_keys(*new_args)
60
30
  normalized_key.gsub!("..", ".")
61
-
62
- {:key => normalized_key, :translation => translation}
31
+
32
+ {:key => normalized_key}
63
33
  end
64
34
 
65
35
  def identify_caller
66
36
  caller = nil
67
- send(:caller)[0..6].each { |string| caller = match_caller(string) unless caller }
68
- caller.present? ? find_lookup_scope(caller) : nil
37
+ send(:caller)[0..6].each do |string|
38
+ caller = match_caller(string) unless caller
39
+ end
40
+
41
+ if caller && caller.present?
42
+ find_lookup_scope(caller)
43
+ else
44
+ nil
45
+ end
69
46
  end
70
-
47
+
71
48
  def match_caller(string)
72
49
  string.match(/(views)(\/.+)(?>:[0-9]+:in)/)
73
50
  end
74
-
75
- def extract_required_vars(*args)
76
- excluded = ["scope", "locale"]
77
- required_vars = args.last.is_a?(Hash) ? args.pop.keys.sort { |a, b| a.to_s <=> b.to_s } : []
78
- required_vars.delete_if { |var| excluded.include?(var.to_s) }
79
- end
80
-
51
+
81
52
  def split_args(*args)
82
53
  options = args.last.is_a?(Hash) ? args.pop : {}
83
54
  key ||= args.shift
84
55
  locale = options.delete(:locale) || I18n.locale
85
56
  return [locale, key, options[:scope], nil]
86
57
  end
87
-
58
+
88
59
  def transform_args_for_caller(caller, *args)
89
60
  _scope = caller
90
-
61
+
91
62
  options = args.last.is_a?(Hash) ? args.pop : {}
92
-
63
+
93
64
  if !options[:scope].presence && _scope.presence
94
65
  options[:scope] = _scope
95
66
  end
96
-
67
+
97
68
  args.push(options)
98
69
  parts = args.first.to_s.split(".").select { |e| !e.blank? }
99
70
  args[0] = parts[0] if parts.size == 1
100
-
71
+
101
72
  return args
102
73
  end
103
-
74
+
104
75
  def find_lookup_scope(caller)
105
76
  split_path = caller[2][1..-1].split(".")[0].split("/")
106
-
77
+
107
78
  template_or_partial = remove_underscore_form_partial(split_path[-1])
108
79
  split_path[-1] = template_or_partial
109
-
80
+
110
81
  split_path.map!(&:to_sym)
111
82
  end
112
-
83
+
113
84
  def remove_underscore_form_partial(template_or_partial)
114
85
  if template_or_partial.to_s[0,1] == "_"
115
86
  template_or_partial.to_s[1..-1]
@@ -117,4 +88,4 @@ module Phrase::Backend::Base
117
88
  template_or_partial.to_s
118
89
  end
119
90
  end
120
- end
91
+ end
@@ -5,10 +5,8 @@ module Phrase::Backend
5
5
  include Base
6
6
 
7
7
  def initialize(args = {})
8
- self.config = { "locales" => {}, "strings" => {} }
9
- Phrase.available_locales = self.config["locales"].keys.map(&:to_sym)
10
8
  self
11
9
  end
12
10
 
13
11
  end
14
- end
12
+ end
@@ -1,134 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module Phrase::Backend
4
- # require_relative 'backend/base'
5
- module Phrase::Backend::Base
6
- PREFIX = "{{__phrase_"
7
- SUFFIX = "__}}"
8
-
9
- # attr_accessor :config
10
-
11
- def translate(*args)
12
- key = lookup_normalized_key(*args)
13
- case key[:translation]
14
- when String, nil, "" then decorate_translation(key[:key])
15
- else key[:translation]
16
- end
17
- end
18
-
19
- # def load_translations(*args)
20
- # raise NotImplementedError
21
- # end
22
- #
23
- # def raw_lookup(locale, key, scope=[], options={})
24
- # I18n.backend.load_translations unless I18n.backend.initialized?
25
- # keys = I18n.normalize_keys(locale, key, scope, options[:separator])
26
- # keys.inject(I18n.backend.send(:translations)) do |result, _key|
27
- # _key = _key.to_sym
28
- # return nil unless result.is_a?(Hash) && result.has_key?(_key)
29
- # result[_key]
30
- # end
31
- # end
32
-
33
- protected
34
- def decorate_translation(key=nil)
35
- return nil unless key.presence
36
- "#{PREFIX}#{key}#{SUFFIX}"
37
- end
38
-
39
- def translation_presence(*args)
40
- I18n.translate!(*args)
41
- rescue I18n::MissingTranslationData => e
42
- nil
43
- end
44
-
45
- def lookup_normalized_key(*args)
46
- translation = translation_presence(*args)
47
- caller = identify_caller
48
-
49
- if (translation.nil? || translation.is_a?(Hash)) && caller && args.first =~ /^\./
50
- args = transform_args_for_caller(caller, *args)
51
- translation = translation_presence(*args)
52
- end
53
-
54
- new_args = split_args(*args)
55
-
56
- normalized_key = I18n::Backend::Flatten.normalize_flat_keys(*new_args)
57
- normalized_key.gsub!("..", ".")
58
-
59
- {:key => normalized_key, :translation => translation}
60
- end
61
-
62
- def identify_caller
63
- caller = nil
64
- send(:caller)[0..6].each { |string| caller = match_caller(string) unless caller }
65
- caller.present? ? find_lookup_scope(caller) : nil
66
- end
67
-
68
- def match_caller(string)
69
- string.match(/(views)(\/.+)(?>:[0-9]+:in)/)
70
- end
71
-
72
- # def extract_required_vars(*args)
73
- # excluded = ["scope", "locale"]
74
- # required_vars = args.last.is_a?(Hash) ? args.pop.keys.sort { |a, b| a.to_s <=> b.to_s } : []
75
- # required_vars.delete_if { |var| excluded.include?(var.to_s) }
76
- # end
77
-
78
- def split_args(*args)
79
- options = args.last.is_a?(Hash) ? args.pop : {}
80
- key ||= args.shift
81
- locale = options.delete(:locale) || I18n.locale
82
- return [locale, key, options[:scope], nil]
83
- end
84
-
85
- def transform_args_for_caller(caller, *args)
86
- _scope = caller
87
-
88
- options = args.last.is_a?(Hash) ? args.pop : {}
89
-
90
- if !options[:scope].presence && _scope.presence
91
- options[:scope] = _scope
92
- end
93
-
94
- args.push(options)
95
- parts = args.first.to_s.split(".").select { |e| !e.blank? }
96
- args[0] = parts[0] if parts.size == 1
97
-
98
- return args
99
- end
100
-
101
- def find_lookup_scope(caller)
102
- split_path = caller[2][1..-1].split(".")[0].split("/")
103
-
104
- template_or_partial = remove_underscore_form_partial(split_path[-1])
105
- split_path[-1] = template_or_partial
106
-
107
- split_path.map!(&:to_sym)
108
- end
109
-
110
- def remove_underscore_form_partial(template_or_partial)
111
- if template_or_partial.to_s[0,1] == "_"
112
- template_or_partial.to_s[1..-1]
113
- else
114
- template_or_partial.to_s
115
- end
116
- end
117
4
  end
118
5
 
119
- # require_relative 'backend/phrase_service'
120
- module Phrase::Backend
121
- class PhraseService
122
- include Base
123
-
124
- def initialize(args = {})
125
- # self.config = { "locales" => {}, "strings" => {} }
126
- # Phrase.available_locales = self.config["locales"].keys.map(&:to_sym)
127
- self
128
- end
129
-
130
- end
131
- end
132
- # LOCALE in keys mit schreiben, hilft der GUI!? -> manuel
133
-
134
- end
6
+ require File.join(File.dirname(__FILE__), 'backend/base')
7
+ require File.join(File.dirname(__FILE__), 'backend/phrase_service')
data/lib/phrase/config.rb CHANGED
@@ -1,16 +1,27 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  module Phrase
3
4
  class Config
4
- def enabled?
5
- ENV['TRANSLATABLE'] == "true" || (defined?(Rails) == true && Rails.env == "staging")
5
+ def client_version
6
+ Phrase::CLIENT_VERSION
6
7
  end
7
8
 
8
- def locale
9
- I18n.locale
9
+ def auth_token
10
+ @@auth_token = "" if !defined? @@auth_token or @@auth_token.nil?
11
+ @@auth_token
10
12
  end
11
-
12
- def default_locale
13
- @@default_locale ||= I18n.default_locale
13
+
14
+ def auth_token=(auth_token)
15
+ @@auth_token = auth_token
16
+ end
17
+
18
+ def enabled
19
+ @@enabled = true if !defined? @@enabled or @@enabled.nil?
20
+ @@enabled
21
+ end
22
+
23
+ def enabled=(enabled)
24
+ @@enabled = enabled
14
25
  end
15
26
 
16
27
  def backend
@@ -21,14 +32,37 @@ module Phrase
21
32
  @@backend = backend
22
33
  end
23
34
 
24
- def available_locales
25
- @@available_locales ||= nil
26
- @@available_locales ||= I18n.available_locales
35
+ def prefix
36
+ @@prefix ||= "{{__"
27
37
  end
28
-
29
- def available_locales=(locales)
30
- @@available_locales = Array(locales).map { |locale| locale.to_sym }
31
- @@available_locales = nil if @@available_locales.empty?
38
+
39
+ def prefix=(prefix)
40
+ @@prefix = prefix
41
+ end
42
+
43
+ def suffix
44
+ @@suffix ||= "__}}"
45
+ end
46
+
47
+ def suffix=(suffix)
48
+ @@suffix = suffix
49
+ end
50
+
51
+ def js_host
52
+ @@js_host ||= 'phraseapp.com'
53
+ end
54
+
55
+ def js_host=(js_host)
56
+ @@js_host = js_host
57
+ end
58
+
59
+ def js_use_ssl
60
+ @@js_use_ssl = true if !defined? @@js_use_ssl or @@js_use_ssl.nil?
61
+ @@js_use_ssl
62
+ end
63
+
64
+ def js_use_ssl=(js_use_ssl)
65
+ @@js_use_ssl = js_use_ssl
32
66
  end
33
67
  end
34
- end
68
+ end
data/lib/phrase/engine.rb CHANGED
@@ -1,18 +1,23 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  require 'phrase'
3
- require 'rails'
4
+ require 'i18n'
4
5
 
5
- module Phrase
6
- class Engine < Rails::Engine
6
+ if defined? Rails
7
+ module Phrase
8
+ class Engine < Rails::Engine
7
9
 
8
- initializer 'phrase' do |app|
9
- ActiveSupport.on_load(:action_view) do
10
- ::ActionView::Base.send :include, Phrase::Extensions::Base
11
- end
10
+ initializer 'phrase' do |app|
11
+ ActiveSupport.on_load(:action_view) do
12
+ ::ActionView::Base.send :include, Phrase::Extensions::Base
13
+ end
12
14
 
13
- ActiveSupport.on_load(:action_controller) do
14
- ::ActionController::Base.send :include, Phrase::Extensions::Base
15
+ ActiveSupport.on_load(:action_controller) do
16
+ ::ActionController::Base.send :include, Phrase::Extensions::Base
17
+ end
18
+
19
+ ActionView::Base.send :include, Phrase::ViewHelpers
15
20
  end
16
21
  end
17
22
  end
18
- end
23
+ end
@@ -1,4 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  module Phrase::Extensions::Base
3
4
  extend ActiveSupport::Concern
4
5
 
@@ -1,4 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  module Phrase::Extensions::Hash
3
4
  extend ActiveSupport::Concern
4
5
 
@@ -1,4 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  module Phrase::Extensions::String
3
4
  extend ActiveSupport::Concern
4
5
 
@@ -1,7 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+
1
3
  module Phrase::Extensions
2
4
  end
3
5
 
4
- require_relative 'extensions/hash'
5
- require_relative 'extensions/string'
6
- require_relative 'extensions/base'
6
+ require File.join(File.dirname(__FILE__), 'extensions/hash')
7
+ require File.join(File.dirname(__FILE__), 'extensions/string')
8
+ require File.join(File.dirname(__FILE__), 'extensions/base')
7
9