tr8n_core 4.0.1 → 4.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +163 -33
- data/lib/tr8n/application.rb +29 -27
- data/lib/tr8n/base.rb +2 -22
- data/lib/tr8n/cache.rb +9 -9
- data/lib/tr8n/cache_adapters/cdb.rb +2 -2
- data/lib/tr8n/cache_adapters/file.rb +2 -2
- data/lib/tr8n/cache_adapters/memcache.rb +3 -3
- data/lib/tr8n/cache_adapters/redis.rb +3 -3
- data/lib/tr8n/component.rb +1 -1
- data/lib/tr8n/config.rb +256 -202
- data/lib/tr8n/decorators/base.rb +1 -1
- data/lib/tr8n/decorators/default.rb +1 -1
- data/lib/tr8n/decorators/html.rb +4 -4
- data/lib/tr8n/exception.rb +1 -1
- data/lib/tr8n/language.rb +17 -27
- data/lib/tr8n/language_case.rb +4 -3
- data/lib/tr8n/language_case_rule.rb +1 -1
- data/lib/tr8n/language_context.rb +4 -2
- data/lib/tr8n/language_context_rule.rb +1 -1
- data/lib/tr8n/logger.rb +8 -3
- data/lib/tr8n/rules_engine/evaluator.rb +4 -7
- data/lib/tr8n/rules_engine/parser.rb +1 -28
- data/lib/tr8n/session.rb +87 -0
- data/lib/tr8n/source.rb +1 -1
- data/lib/tr8n/tokens/data.rb +2 -2
- data/lib/tr8n/tokens/data_tokenizer.rb +1 -1
- data/lib/tr8n/tokens/decoration_tokenizer.rb +1 -1
- data/lib/tr8n/tokens/hidden.rb +1 -1
- data/lib/tr8n/tokens/method.rb +1 -1
- data/lib/tr8n/tokens/transform.rb +2 -2
- data/lib/tr8n/translation.rb +2 -2
- data/lib/tr8n/translation_key.rb +4 -4
- data/lib/tr8n/translator.rb +1 -1
- data/lib/tr8n/utils.rb +1 -1
- data/lib/tr8n_core/ext/array.rb +4 -4
- data/lib/tr8n_core/ext/date.rb +38 -13
- data/lib/tr8n_core/ext/fixnum.rb +3 -3
- data/lib/tr8n_core/ext/hash.rb +1 -1
- data/lib/tr8n_core/ext/string.rb +3 -3
- data/lib/tr8n_core/ext/time.rb +30 -24
- data/lib/tr8n_core/generators/cache/base.rb +4 -4
- data/lib/tr8n_core/generators/cache/cdb.rb +1 -1
- data/lib/tr8n_core/generators/cache/file.rb +4 -3
- data/lib/tr8n_core/version.rb +2 -2
- data/lib/tr8n_core.rb +1 -1
- data/spec/application_spec.rb +5 -192
- data/spec/base_spec.rb +1 -1
- data/spec/config_spec.rb +21 -5
- data/spec/decorator_spec.rb +1 -1
- data/spec/decorators/base_spec.rb +1 -1
- data/spec/decorators/default_spec.rb +1 -1
- data/spec/decorators/html_spec.rb +8 -8
- data/spec/fixtures/languages/ru.json +36 -4
- data/spec/language_case_rule_spec.rb +1 -1
- data/spec/language_case_spec.rb +1 -1
- data/spec/language_context_rule_spec.rb +1 -1
- data/spec/language_context_spec.rb +1 -1
- data/spec/language_spec.rb +603 -4
- data/spec/rules_engine/evaluator_spec.rb +1 -1
- data/spec/rules_engine/parser_spec.rb +1 -1
- data/spec/{helper.rb → spec_helper.rb} +15 -7
- data/spec/tokens/data_spec.rb +2 -2
- data/spec/tokens/data_tokenizer_spec.rb +1 -1
- data/spec/tokens/decoration_tokenizer_spec.rb +1 -1
- data/spec/tokens/hidden_spec.rb +1 -1
- data/spec/tokens/method_spec.rb +1 -1
- data/spec/tokens/transform_spec.rb +1 -1
- data/spec/translation_key_spec.rb +1 -1
- data/spec/translation_spec.rb +1 -1
- data/spec/utils_spec.rb +1 -3
- metadata +5 -8
- data/config/config.yml +0 -34
- data/config/tokens/data.yml +0 -45
- data/config/tokens/decorations.yml +0 -37
- data/lib/tr8n_core/modules/logger.rb +0 -43
data/lib/tr8n/language_case.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -95,7 +95,8 @@ class Tr8n::LanguageCase < Tr8n::Base
|
|
95
95
|
def decorate(word, case_value, case_rule, options = {})
|
96
96
|
return case_value if options[:skip_decorations]
|
97
97
|
return case_value if language.default?
|
98
|
-
return case_value unless Tr8n.
|
98
|
+
return case_value unless Tr8n.session.current_translator
|
99
|
+
return case_value unless Tr8n.session.current_translator.inline?
|
99
100
|
|
100
101
|
"<span class='tr8n_language_case' data-case_id='#{id}' data-rule_id='#{case_rule ? case_rule.id : ''}' data-case_key='#{word.gsub("'", "\'")}'>#{case_value}</span>"
|
101
102
|
end
|
@@ -105,7 +106,7 @@ class Tr8n::LanguageCase < Tr8n::Base
|
|
105
106
|
#######################################################################################################
|
106
107
|
|
107
108
|
def to_cache_hash
|
108
|
-
hash = to_hash(:id, :keyword, :description, :latin_name, :native_name)
|
109
|
+
hash = to_hash(:id, :keyword, :description, :latin_name, :native_name, :application)
|
109
110
|
hash["rules"] = []
|
110
111
|
rules.each do |rule|
|
111
112
|
hash["rules"] << rule.to_cache_hash
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -40,7 +40,7 @@ class Tr8n::LanguageContext < Tr8n::Base
|
|
40
40
|
|
41
41
|
def config
|
42
42
|
context_rules = Tr8n.config.context_rules
|
43
|
-
hash_value(context_rules, keyword) || {}
|
43
|
+
hash_value(context_rules, keyword.to_sym) || {}
|
44
44
|
end
|
45
45
|
|
46
46
|
def token_expression
|
@@ -86,6 +86,8 @@ class Tr8n::LanguageContext < Tr8n::Base
|
|
86
86
|
else
|
87
87
|
vars[key] = obj
|
88
88
|
end
|
89
|
+
|
90
|
+
vars[key] = vars[key].to_s if vars[key].is_a?(Symbol)
|
89
91
|
end
|
90
92
|
vars
|
91
93
|
end
|
data/lib/tr8n/logger.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -27,7 +27,7 @@ module Tr8n
|
|
27
27
|
|
28
28
|
def self.logger
|
29
29
|
@logger ||= begin
|
30
|
-
logfile_path = File.expand_path(Tr8n.config.
|
30
|
+
logfile_path = File.expand_path(Tr8n.config.logger[:path])
|
31
31
|
logfile_dir = logfile_path.split("/")[0..-2].join("/")
|
32
32
|
FileUtils.mkdir_p(logfile_dir) unless File.exist?(logfile_dir)
|
33
33
|
logfile = File.open(logfile_path, 'a')
|
@@ -39,11 +39,13 @@ module Tr8n
|
|
39
39
|
class Logger < ::Logger
|
40
40
|
|
41
41
|
def format_message(severity, timestamp, progname, msg)
|
42
|
+
return "" unless Tr8n.config.logger[:enabled]
|
43
|
+
# TODO: check for severity/level
|
42
44
|
"[#{timestamp.strftime("%D %T")}]: #{" " * stack.size}#{msg}\n"
|
43
45
|
end
|
44
46
|
|
45
47
|
def add(severity, message = nil, progname = nil, &block)
|
46
|
-
return unless Tr8n.config.
|
48
|
+
return unless Tr8n.config.logger[:enabled]
|
47
49
|
super
|
48
50
|
end
|
49
51
|
|
@@ -52,6 +54,9 @@ module Tr8n
|
|
52
54
|
end
|
53
55
|
|
54
56
|
def trace_api_call(path, params)
|
57
|
+
[:client_secret, :access_token].each do |param|
|
58
|
+
params = params.merge(param => "##filtered##")
|
59
|
+
end
|
55
60
|
debug("api: [/#{path}] #{params.inspect}")
|
56
61
|
stack.push(caller)
|
57
62
|
t0 = Time.now
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -31,7 +31,7 @@ module Tr8n
|
|
31
31
|
@vars = {}
|
32
32
|
@env = {
|
33
33
|
# McCarthy's Elementary S-functions and Predicates
|
34
|
-
'label' => lambda { |l, r| @
|
34
|
+
'label' => lambda { |l, r| @vars[l] = r },
|
35
35
|
'quote' => lambda { |expr| expr },
|
36
36
|
'car' => lambda { |list| list[1] },
|
37
37
|
'cdr' => lambda { |list| list.drop(1) },
|
@@ -58,7 +58,7 @@ module Tr8n
|
|
58
58
|
'||' => lambda { |*expr| expr.any?{|e| evaluate(e)} }, # ['||', [], [], ...]
|
59
59
|
'or' => lambda { |*expr| expr.any?{|e| evaluate(e)} }, # ['or', [], [], ...]
|
60
60
|
'if' => lambda { |c, t, f| evaluate(c) ? evaluate(t) : evaluate(f) }, # ['if', 'cond', 'true', 'false']
|
61
|
-
'let' => lambda { |l, r| @
|
61
|
+
'let' => lambda { |l, r| @vars[l] = r }, # ['let', 'n', 5]
|
62
62
|
'true' => lambda { true }, # ['true']
|
63
63
|
'false' => lambda { false }, # ['false']
|
64
64
|
|
@@ -125,9 +125,6 @@ module Tr8n
|
|
125
125
|
end
|
126
126
|
|
127
127
|
def reset!
|
128
|
-
@vars.each do |var|
|
129
|
-
@env.delete(var)
|
130
|
-
end
|
131
128
|
@vars = {}
|
132
129
|
end
|
133
130
|
|
@@ -138,7 +135,7 @@ module Tr8n
|
|
138
135
|
|
139
136
|
def evaluate(expr)
|
140
137
|
if @env['atom'].call(expr)
|
141
|
-
return @
|
138
|
+
return @vars[expr] if expr.is_a?(String) and @vars[expr]
|
142
139
|
return expr
|
143
140
|
end
|
144
141
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -50,33 +50,6 @@ module Tr8n
|
|
50
50
|
tokens.shift
|
51
51
|
list
|
52
52
|
end
|
53
|
-
|
54
|
-
def class_for(token)
|
55
|
-
{
|
56
|
-
/^[\(]$/ => 'open_paren',
|
57
|
-
/^[\)]$/ => 'close_paren',
|
58
|
-
/^['|"]/ => 'string',
|
59
|
-
/^@/ => 'variable',
|
60
|
-
/^[\d|.]+$/ => 'number',
|
61
|
-
}.each do |regexp, cls|
|
62
|
-
return cls if regexp.match(token)
|
63
|
-
end
|
64
|
-
'symbol'
|
65
|
-
end
|
66
|
-
|
67
|
-
def decorate
|
68
|
-
html = ["<span class='tr8n_sexp'>"]
|
69
|
-
if tokens
|
70
|
-
html << tokens.collect do |token|
|
71
|
-
"<span class='#{class_for(token)}'>#{token}</span>"
|
72
|
-
end.join('')
|
73
|
-
else
|
74
|
-
html << "<span class='#{class_for(expression)}'>#{expression}</span>"
|
75
|
-
end
|
76
|
-
html << '</span>'
|
77
|
-
html.join('').html_safe
|
78
|
-
end
|
79
|
-
|
80
53
|
end
|
81
54
|
|
82
55
|
end
|
data/lib/tr8n/session.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
module Tr8n
|
25
|
+
|
26
|
+
def self.session
|
27
|
+
Thread.current[:session] ||= Tr8n::Session.new
|
28
|
+
end
|
29
|
+
|
30
|
+
# Acts as a global singleton that holds all Tr8n configuration
|
31
|
+
# The class can be extended with a different implementation, as long as the interface is supported
|
32
|
+
class Session
|
33
|
+
# Session Attributes - Move to Session
|
34
|
+
attr_accessor :application, :current_user, :current_language, :current_translator,
|
35
|
+
:current_source, :current_component, :block_options
|
36
|
+
|
37
|
+
def init
|
38
|
+
Tr8n::Application.init(Tr8n.config.application[:key], Tr8n.config.application[:secret], Tr8n.config.application[:host])
|
39
|
+
self.current_source = "/tr8n/core"
|
40
|
+
end
|
41
|
+
|
42
|
+
def reset
|
43
|
+
self.application= nil
|
44
|
+
self.current_user= nil
|
45
|
+
self.current_language= nil
|
46
|
+
self.current_translator= nil
|
47
|
+
self.current_source= nil
|
48
|
+
self.current_component= nil
|
49
|
+
self.block_options= nil
|
50
|
+
end
|
51
|
+
|
52
|
+
#########################################################
|
53
|
+
## Block Options
|
54
|
+
#########################################################
|
55
|
+
|
56
|
+
def block_options
|
57
|
+
(@block_options ||= []).last || {}
|
58
|
+
end
|
59
|
+
|
60
|
+
def with_block_options(opts)
|
61
|
+
@block_options ||= []
|
62
|
+
@block_options.push(opts)
|
63
|
+
if block_given?
|
64
|
+
ret = yield
|
65
|
+
end
|
66
|
+
@block_options.pop
|
67
|
+
ret
|
68
|
+
end
|
69
|
+
|
70
|
+
def current_source_from_block_options
|
71
|
+
arr = @block_options || []
|
72
|
+
arr.reverse.each do |opts|
|
73
|
+
return application.source_by_key(opts[:source]) unless opts[:source].blank?
|
74
|
+
end
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def current_component_from_block_options
|
79
|
+
arr = @block_options || []
|
80
|
+
arr.reverse.each do |opts|
|
81
|
+
return application.component_by_key(opts[:component]) unless opts[:component].blank?
|
82
|
+
end
|
83
|
+
Tr8n.config.current_component
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
data/lib/tr8n/source.rb
CHANGED
data/lib/tr8n/tokens/data.rb
CHANGED
@@ -92,13 +92,13 @@ class Tr8n::Tokens::Data < Tr8n::Base
|
|
92
92
|
def sanitize(object, value, options, language)
|
93
93
|
value = "#{value.to_s}" unless value.is_a?(String)
|
94
94
|
|
95
|
-
unless Tr8n.
|
95
|
+
unless Tr8n.session.block_options[:skip_html_escaping]
|
96
96
|
if options[:sanitize_values]
|
97
97
|
value = ERB::Util.html_escape(value)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
if Tr8n.
|
101
|
+
if Tr8n.session.application and not Tr8n.session.application.feature_enabled?(:language_cases)
|
102
102
|
return value
|
103
103
|
end
|
104
104
|
|
data/lib/tr8n/tokens/hidden.rb
CHANGED
data/lib/tr8n/tokens/method.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -137,7 +137,7 @@ class Tr8n::Tokens::Transform < Tr8n::Tokens::Data
|
|
137
137
|
|
138
138
|
# apply settings cases
|
139
139
|
value = params[index]
|
140
|
-
if Tr8n.
|
140
|
+
if Tr8n.session.application.feature_enabled?(:language_cases)
|
141
141
|
parts[1..-1].each do |case_key|
|
142
142
|
lcase = context.language.language_case_by_keyword(case_key)
|
143
143
|
unless lcase
|
data/lib/tr8n/translation.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -98,7 +98,7 @@ class Tr8n::Translation < Tr8n::Base
|
|
98
98
|
#######################################################################################################
|
99
99
|
|
100
100
|
def to_cache_hash
|
101
|
-
to_hash(:label, :context)
|
101
|
+
to_hash(:locale, :label, :context, :precedence)
|
102
102
|
end
|
103
103
|
|
104
104
|
end
|
data/lib/tr8n/translation_key.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -32,7 +32,7 @@ class Tr8n::TranslationKey < Tr8n::Base
|
|
32
32
|
super
|
33
33
|
|
34
34
|
self.attributes[:key] ||= self.class.generate_key(label, description)
|
35
|
-
self.attributes[:locale] ||= Tr8n.
|
35
|
+
self.attributes[:locale] ||= Tr8n.session.block_options[:locale] || application.default_locale
|
36
36
|
self.attributes[:language] ||= application.language(locale)
|
37
37
|
self.attributes[:translations] = {}
|
38
38
|
|
@@ -61,7 +61,7 @@ class Tr8n::TranslationKey < Tr8n::Base
|
|
61
61
|
def fetch_translations(language, options = {})
|
62
62
|
return self if self.id and has_translations_for_language?(language)
|
63
63
|
|
64
|
-
if options[:dry] or Tr8n.
|
64
|
+
if options[:dry] or Tr8n.session.block_options[:dry]
|
65
65
|
return application.cache_translation_key(self)
|
66
66
|
end
|
67
67
|
|
@@ -192,7 +192,7 @@ class Tr8n::TranslationKey < Tr8n::Base
|
|
192
192
|
end
|
193
193
|
|
194
194
|
def to_cache_hash
|
195
|
-
hash = to_hash(:id, :key, :label, :description, :locale, :level)
|
195
|
+
hash = to_hash(:id, :key, :label, :description, :locale, :level, :locked)
|
196
196
|
if translations and translations.any?
|
197
197
|
hash["translations"] = {}
|
198
198
|
translations.each do |locale, locale_translations|
|
data/lib/tr8n/translator.rb
CHANGED
data/lib/tr8n/utils.rb
CHANGED
data/lib/tr8n_core/ext/array.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -24,7 +24,7 @@
|
|
24
24
|
class Array
|
25
25
|
|
26
26
|
# translates an array of options for a select tag
|
27
|
-
def tro(description = "", options = {}, language = Tr8n.
|
27
|
+
def tro(description = "", options = {}, language = Tr8n.session.current_language)
|
28
28
|
return [] if empty?
|
29
29
|
|
30
30
|
collect do |opt|
|
@@ -39,7 +39,7 @@ class Array
|
|
39
39
|
end
|
40
40
|
|
41
41
|
# translate array values
|
42
|
-
def trl(description = "", options = {}, language = Tr8n.
|
42
|
+
def trl(description = "", options = {}, language = Tr8n.session.current_language)
|
43
43
|
return [] if empty?
|
44
44
|
|
45
45
|
collect do |opt|
|
@@ -52,7 +52,7 @@ class Array
|
|
52
52
|
end
|
53
53
|
|
54
54
|
# creates a sentence with tr "and" joiner
|
55
|
-
def tr_sentence(options = {}, language = Tr8n.
|
55
|
+
def tr_sentence(options = {}, language = Tr8n.session.current_language)
|
56
56
|
return "" if empty?
|
57
57
|
return first if size == 1
|
58
58
|
|
data/lib/tr8n_core/ext/date.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -23,18 +23,43 @@
|
|
23
23
|
|
24
24
|
class Date
|
25
25
|
|
26
|
-
|
26
|
+
# The translation of the date can use one of the formats:
|
27
|
+
#
|
28
|
+
# Using the Symbol to pull the default format defined in the config:
|
29
|
+
#
|
30
|
+
# date.translate(:verbose)
|
31
|
+
#
|
32
|
+
# Using a string with format with native Date elements
|
33
|
+
#
|
34
|
+
# date.translate("%m/%d/%Y")
|
35
|
+
#
|
36
|
+
# Or using a string with tokens:
|
37
|
+
#
|
38
|
+
# date.translate("{month_name} {days::ord}")
|
39
|
+
#
|
40
|
+
# The last allows you to use language cases to get something like "on March 1st".
|
41
|
+
|
42
|
+
def translate(format = :default, language = Tr8n.session.current_language, options = {})
|
27
43
|
label = (format.is_a?(String) ? format.clone : Tr8n.config.default_date_formats[format].clone)
|
28
|
-
symbols = label.scan(/(%\w)/).flatten.uniq
|
29
44
|
|
30
45
|
selected_tokens = []
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
selected_tokens
|
35
|
-
|
46
|
+
using_tokens = label.index('{')
|
47
|
+
|
48
|
+
if using_tokens
|
49
|
+
selected_tokens = Tr8n::Tokens::Data.parse(label).collect{ |token| token.name(:parens => true) }
|
50
|
+
|
51
|
+
else
|
52
|
+
symbols = label.scan(/(%\w)/).flatten.uniq
|
53
|
+
|
54
|
+
symbols.each do |symbol|
|
55
|
+
token = Tr8n.config.strftime_symbol_to_token(symbol)
|
56
|
+
next unless token
|
57
|
+
selected_tokens << token
|
58
|
+
label.gsub!(symbol, token)
|
59
|
+
end
|
36
60
|
end
|
37
61
|
|
62
|
+
|
38
63
|
tokens = {}
|
39
64
|
selected_tokens.each do |token|
|
40
65
|
case token
|
@@ -45,10 +70,10 @@ class Date
|
|
45
70
|
when "{week_days}" then tokens[:week_days] = strftime("%w")
|
46
71
|
when "{short_years}" then tokens[:short_years] = strftime("%y")
|
47
72
|
when "{years}" then tokens[:years] = year
|
48
|
-
when "{short_week_day_name}" then tokens[:short_week_day_name] = language.tr(Tr8n.config.
|
49
|
-
when "{week_day_name}" then tokens[:week_day_name] = language.tr(Tr8n.config.
|
50
|
-
when "{short_month_name}" then tokens[:short_month_name] = language.tr(Tr8n.config.
|
51
|
-
when "{month_name}" then tokens[:month_name] = language.tr(Tr8n.config.
|
73
|
+
when "{short_week_day_name}" then tokens[:short_week_day_name] = language.tr(Tr8n.config.default_abbr_day_name(wday), "Short name for a day of a week", {}, options)
|
74
|
+
when "{week_day_name}" then tokens[:week_day_name] = language.tr(Tr8n.config.default_day_name(wday), "Day of a week", {}, options)
|
75
|
+
when "{short_month_name}" then tokens[:short_month_name] = language.tr(Tr8n.config.default_abbr_month_name(month - 1), "Short month name", {}, options)
|
76
|
+
when "{month_name}" then tokens[:month_name] = language.tr(Tr8n.config.default_month_name(month - 1), "Month name", {}, options)
|
52
77
|
when "{day_of_month}" then tokens[:day_of_month] = strftime("%e")
|
53
78
|
end
|
54
79
|
end
|
@@ -57,7 +82,7 @@ class Date
|
|
57
82
|
end
|
58
83
|
alias :tr :translate
|
59
84
|
|
60
|
-
def trl(format = :default, language = Tr8n.
|
85
|
+
def trl(format = :default, language = Tr8n.session.current_language, options = {})
|
61
86
|
tr(format, language, options.merge!(:skip_decorations => true))
|
62
87
|
end
|
63
88
|
end
|
data/lib/tr8n_core/ext/fixnum.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -27,12 +27,12 @@ class Fixnum
|
|
27
27
|
(to_i < 10 ? "0#{to_s}" : to_s)
|
28
28
|
end
|
29
29
|
|
30
|
-
def translate(desc = "", tokens = {}, options = {}, language = Tr8n.
|
30
|
+
def translate(desc = "", tokens = {}, options = {}, language = Tr8n.session.current_language)
|
31
31
|
to_s.translate(desc, tokens, options, language)
|
32
32
|
end
|
33
33
|
alias tr translate
|
34
34
|
|
35
|
-
def trl(desc = "", tokens = {}, options = {}, language = Tr8n.
|
35
|
+
def trl(desc = "", tokens = {}, options = {}, language = Tr8n.session.current_language)
|
36
36
|
to_s.trl(desc, tokens, options, language)
|
37
37
|
end
|
38
38
|
|
data/lib/tr8n_core/ext/hash.rb
CHANGED
data/lib/tr8n_core/ext/string.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c)
|
2
|
+
# Copyright (c) 2014 Michael Berkovich, tr8nhub.com
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -23,11 +23,11 @@
|
|
23
23
|
|
24
24
|
class String
|
25
25
|
|
26
|
-
def translate(desc = "", tokens = {}, options = {}, language = Tr8n.
|
26
|
+
def translate(desc = "", tokens = {}, options = {}, language = Tr8n.session.current_language)
|
27
27
|
language.translate(self, desc, tokens, options)
|
28
28
|
end
|
29
29
|
|
30
|
-
def trl(desc = "", tokens = {}, options = {}, language = Tr8n.
|
30
|
+
def trl(desc = "", tokens = {}, options = {}, language = Tr8n.session.current_language)
|
31
31
|
translate(desc, tokens, options.merge(:skip_decorations => true), language)
|
32
32
|
end
|
33
33
|
|