lit 0.2.6 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.md +93 -12
- data/Rakefile +1 -0
- data/app/assets/javascripts/lit/application.js +1 -2
- data/app/assets/javascripts/lit/{bootstrap.js.coffee → backend/bootstrap.js.coffee} +0 -0
- data/app/assets/javascripts/lit/{dashboard.js → backend/dashboard.js} +0 -0
- data/app/assets/javascripts/lit/{jquery-te-1.4.0.min.js → backend/jquery-te-1.4.0.min.js} +0 -0
- data/app/assets/javascripts/lit/{localizations.js.coffee → backend/localizations.js.coffee} +2 -0
- data/app/assets/javascripts/lit/backend/sources.js.coffee +24 -0
- data/app/assets/javascripts/lit/lit_frontend.js +103 -0
- data/app/assets/javascripts/lit/mousetrap.js +1044 -0
- data/app/assets/stylesheets/lit/application.css +7 -1
- data/app/assets/stylesheets/lit/{jquery-te-1.4.0.css.scss → backend/jquery-te-1.4.0.css.scss} +0 -0
- data/app/assets/stylesheets/lit/lit_frontend.css +86 -0
- data/app/controllers/lit/api/v1/base_controller.rb +1 -1
- data/app/controllers/lit/application_controller.rb +7 -3
- data/app/controllers/lit/concerns/request_info_store.rb +16 -0
- data/app/controllers/lit/concerns/request_keys_store.rb +16 -0
- data/app/controllers/lit/incomming_localizations_controller.rb +2 -2
- data/app/controllers/lit/locales_controller.rb +1 -1
- data/app/controllers/lit/localization_keys_controller.rb +22 -9
- data/app/controllers/lit/localizations_controller.rb +19 -6
- data/app/controllers/lit/sources_controller.rb +11 -1
- data/app/helpers/lit/frontend_helper.rb +71 -0
- data/app/helpers/lit/localization_keys_helper.rb +5 -0
- data/app/helpers/lit/sources_helper.rb +3 -0
- data/app/jobs/lit/synchronize_source_job.rb +11 -0
- data/app/models/lit/localization.rb +17 -10
- data/app/models/lit/localization_key.rb +6 -2
- data/app/models/lit/source.rb +4 -3
- data/app/views/layouts/lit/application.html.erb +6 -6
- data/app/views/lit/incomming_localizations/index.html.erb +22 -17
- data/app/views/lit/localization_keys/_localization_row.html.erb +1 -1
- data/app/views/lit/localization_keys/index.html.erb +27 -8
- data/app/views/lit/localizations/_form.html.erb +5 -5
- data/app/views/lit/localizations/_previous_versions_rows.html.erb +1 -1
- data/app/views/lit/localizations/edit.js.erb +2 -2
- data/app/views/lit/localizations/previous_versions.js.erb +1 -1
- data/app/views/lit/localizations/update.js.erb +2 -2
- data/config/routes.rb +12 -12
- data/db/migrate/20180101010101_lit_create_lit_locales.rb +16 -0
- data/db/migrate/20180101010102_lit_create_lit_localization_keys.rb +18 -0
- data/db/migrate/20180101010103_lit_create_lit_localizations.rb +24 -0
- data/db/migrate/20180101010104_lit_add_is_completed_and_is_starred_to_localization_keys.rb +17 -0
- data/db/migrate/20180101010105_lit_add_is_hidden_to_locales.rb +12 -0
- data/db/migrate/20180101010106_lit_create_lit_localization_versions.rb +20 -0
- data/db/migrate/20180101010107_lit_create_lit_sources.rb +19 -0
- data/db/migrate/20180101010108_lit_create_lit_incomming_localizations.rb +34 -0
- data/db/migrate/20180101010109_lit_add_sync_complete_to_lit_sources.rb +12 -0
- data/lib/generators/lit/install/templates/initializer.rb +17 -6
- data/lib/generators/lit/install_generator.rb +0 -7
- data/lib/lit.rb +15 -8
- data/lib/lit/adapters/hash_storage.rb +4 -0
- data/lib/lit/adapters/redis_storage.rb +9 -3
- data/lib/lit/cache.rb +126 -70
- data/lib/lit/engine.rb +20 -1
- data/lib/lit/i18n_backend.rb +66 -26
- data/lib/lit/version.rb +1 -1
- data/lib/tasks/lit_tasks.rake +21 -3
- metadata +67 -43
- data/app/assets/stylesheets/lit/dashboard.css +0 -4
- data/config/database.yml +0 -8
- data/db/migrate/20121103144612_create_lit_locales.rb +0 -9
- data/db/migrate/20121103174049_create_lit_localization_keys.rb +0 -10
- data/db/migrate/20121103182106_create_lit_localizations.rb +0 -15
- data/db/migrate/20121225112100_add_is_completed_and_is_starred_to_localization_keys.rb +0 -6
- data/db/migrate/20130511111904_add_is_hidden_to_locales.rb +0 -5
- data/db/migrate/20130921071304_create_lit_localization_versions.rb +0 -11
- data/db/migrate/20130923162141_create_lit_sources.rb +0 -12
- data/db/migrate/20130924151910_create_lit_incomming_localizations.rb +0 -21
@@ -0,0 +1,17 @@
|
|
1
|
+
class LitAddIsCompletedAndIsStarredToLocalizationKeys < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
unless column_exists?(:lit_localization_keys, :is_completed)
|
6
|
+
add_column :lit_localization_keys, :is_completed, :boolean, default: false
|
7
|
+
end
|
8
|
+
unless column_exists?(:lit_localization_keys, :is_starred)
|
9
|
+
add_column :lit_localization_keys, :is_starred, :boolean, default: false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def down
|
14
|
+
remove_column :lit_localization_keys, :is_completed
|
15
|
+
remove_column :lit_localization_keys, :is_starred
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class LitAddIsHiddenToLocales < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
return if column_exists?(:lit_locales, :is_hidden)
|
6
|
+
add_column :lit_locales, :is_hidden, :boolean, default: false
|
7
|
+
end
|
8
|
+
|
9
|
+
def down
|
10
|
+
remove_column :lit_locales, :is_hidden
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class LitCreateLitLocalizationVersions < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
return if table_exists?(:lit_localization_versions)
|
6
|
+
create_table :lit_localization_versions do |t|
|
7
|
+
t.text :translated_value
|
8
|
+
t.integer :localization_id
|
9
|
+
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :lit_localization_versions, :localization_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def down
|
17
|
+
remove_index :lit_localization_versions, :localization_id
|
18
|
+
drop_table :lit_localization_versions
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class LitCreateLitSources < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
return if table_exists?(:lit_sources)
|
6
|
+
create_table :lit_sources do |t|
|
7
|
+
t.string :identifier
|
8
|
+
t.string :url
|
9
|
+
t.string :api_key
|
10
|
+
t.datetime :last_updated_at
|
11
|
+
|
12
|
+
t.timestamps
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def down
|
17
|
+
drop_table :lit_sources
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class LitCreateLitIncommingLocalizations < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
return if table_exists?(:lit_incomming_localizations)
|
6
|
+
create_table :lit_incomming_localizations do |t|
|
7
|
+
t.text :translated_value
|
8
|
+
t.integer :locale_id
|
9
|
+
t.integer :localization_key_id
|
10
|
+
t.integer :localization_id
|
11
|
+
t.string :locale_str
|
12
|
+
t.string :localization_key_str
|
13
|
+
t.integer :source_id
|
14
|
+
t.integer :incomming_id
|
15
|
+
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
|
19
|
+
add_index :lit_incomming_localizations, :locale_id
|
20
|
+
add_index :lit_incomming_localizations, :localization_key_id
|
21
|
+
add_index :lit_incomming_localizations, :localization_id
|
22
|
+
add_index :lit_incomming_localizations, :source_id
|
23
|
+
add_index :lit_incomming_localizations, :incomming_id
|
24
|
+
end
|
25
|
+
|
26
|
+
def down
|
27
|
+
remove_index :lit_incomming_localizations, :locale_id
|
28
|
+
remove_index :lit_incomming_localizations, :localization_key_id
|
29
|
+
remove_index :lit_incomming_localizations, :localization_id
|
30
|
+
remove_index :lit_incomming_localizations, :source_id
|
31
|
+
remove_index :lit_incomming_localizations, :incomming_id
|
32
|
+
drop_table :lit_incomming_localizations
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class LitAddSyncCompleteToLitSources < Rails::VERSION::MAJOR >= 5 ?
|
2
|
+
ActiveRecord::Migration[4.2] :
|
3
|
+
ActiveRecord::Migration
|
4
|
+
def up
|
5
|
+
return if column_exists?(:lit_sources, :sync_complete)
|
6
|
+
add_column :lit_sources, :sync_complete, :boolean
|
7
|
+
end
|
8
|
+
|
9
|
+
def down
|
10
|
+
remove_column :lit_sources, :sync_complete
|
11
|
+
end
|
12
|
+
end
|
@@ -3,13 +3,18 @@
|
|
3
3
|
# `nil` will let everyone in.
|
4
4
|
Lit.authentication_function = <%= @authentication_function || 'nil' %>
|
5
5
|
|
6
|
+
# Which authentication verification function to use (ie. :user_signed_in)?
|
7
|
+
# This is used together with frontend translation module to determine, if
|
8
|
+
# helper button should be applied and translations wrapped in custom span
|
9
|
+
Lit.authentication_verification = <%= @authentication_verification || 'nil' %>
|
10
|
+
|
6
11
|
# Either 'redis' or 'hash'. Hash is the fastest, but will fail in multiprocess
|
7
12
|
# environment
|
8
|
-
Lit.key_value_engine =
|
13
|
+
Lit.key_value_engine = '<%= @key_value_engine %>'
|
9
14
|
|
10
15
|
# Pass extra options to key_value_neinge, ie. prefix for redis (only one
|
11
16
|
# supported at the moment)
|
12
|
-
# Lit.storage_options = { :
|
17
|
+
# Lit.storage_options = { prefix: "my_project" }
|
13
18
|
|
14
19
|
# If true all translations are returned as html_safe strings
|
15
20
|
Lit.all_translations_are_html_safe = false
|
@@ -20,21 +25,27 @@ Lit.all_translations_are_html_safe = false
|
|
20
25
|
# <span title="translation missing string"></span>
|
21
26
|
Lit.humanize_key = false
|
22
27
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
28
|
+
# If set to `true` will always parse yaml files upon startup and update cache
|
29
|
+
# values with ones with yaml - but only, if those keys haven't been changed via
|
30
|
+
# web ui before
|
31
|
+
Lit.ignore_yaml_on_startup = true
|
26
32
|
|
27
33
|
# API enabled? API allows third party browsing your translations, as well as
|
28
34
|
# synchronizing them between environments
|
29
35
|
Lit.api_enabled = false
|
30
36
|
|
31
37
|
# API key is required to authorize third party, if API is enabled
|
32
|
-
Lit.api_key =
|
38
|
+
Lit.api_key = '<%= @api_key %>'
|
33
39
|
|
34
40
|
# If true, last_updated_at column of synchronizaton source will be set to now
|
35
41
|
# upon record creation
|
36
42
|
Lit.set_last_updated_at_upon_creation = true
|
37
43
|
|
44
|
+
# Store request info - this will store in cache additional info about request
|
45
|
+
# path that triggered translation key to be displayed / accessed
|
46
|
+
# For more infor please check README.md
|
47
|
+
Lit.store_request_info = false
|
48
|
+
|
38
49
|
# Initialize lit
|
39
50
|
Lit.init
|
40
51
|
|
@@ -10,13 +10,6 @@ module Lit
|
|
10
10
|
|
11
11
|
desc 'Automates Lit installation'
|
12
12
|
|
13
|
-
def copy_migrations
|
14
|
-
puts 'Copying Lit migrations...'
|
15
|
-
Dir.chdir(::Rails.root) do
|
16
|
-
`rake lit:install:migrations`
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
13
|
def set_authentication_function
|
21
14
|
@authentication_function = options['authentication-function'].presence ||
|
22
15
|
ask("What's the authentication function, ie. :authenticate_user! :").presence ||
|
data/lib/lit.rb
CHANGED
@@ -3,29 +3,32 @@ require 'lit/loader'
|
|
3
3
|
|
4
4
|
module Lit
|
5
5
|
mattr_accessor :authentication_function
|
6
|
+
mattr_accessor :authentication_verification
|
6
7
|
mattr_accessor :key_value_engine
|
7
8
|
mattr_accessor :storage_options
|
8
9
|
mattr_accessor :humanize_key
|
9
10
|
mattr_accessor :ignored_keys
|
10
|
-
mattr_accessor :
|
11
|
+
mattr_accessor :ignore_yaml_on_startup
|
11
12
|
mattr_accessor :api_enabled
|
12
13
|
mattr_accessor :api_key
|
13
14
|
mattr_accessor :all_translations_are_html_safe
|
14
15
|
mattr_accessor :set_last_updated_at_upon_creation
|
16
|
+
mattr_accessor :store_request_info
|
15
17
|
|
16
18
|
class << self
|
17
19
|
attr_accessor :loader
|
18
20
|
end
|
21
|
+
|
19
22
|
def self.init
|
20
23
|
@@table_exists ||= check_if_table_exists
|
21
24
|
if loader.nil? && @@table_exists
|
22
25
|
self.loader ||= Loader.new
|
23
26
|
Lit.humanize_key = false if Lit.humanize_key.nil?
|
24
|
-
Lit.fallback = true if Lit.fallback.nil?
|
25
27
|
if Lit.ignored_keys.is_a?(String)
|
26
28
|
keys = Lit.ignored_keys.split(',').map(&:strip)
|
27
29
|
Lit.ignored_keys = keys
|
28
30
|
end
|
31
|
+
Lit.ignore_yaml_on_startup = true if Lit.ignore_yaml_on_startup.nil?
|
29
32
|
Lit.ignored_keys = [] unless Lit.ignored_keys.is_a?(Array)
|
30
33
|
# if loading all translations on start, migrations have to be already
|
31
34
|
# performed, fails on first deploy
|
@@ -43,14 +46,18 @@ module Lit
|
|
43
46
|
|
44
47
|
def self.get_key_value_engine
|
45
48
|
case Lit.key_value_engine
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
when 'redis'
|
50
|
+
require 'lit/adapters/redis_storage'
|
51
|
+
return RedisStorage.new
|
52
|
+
else
|
53
|
+
require 'lit/adapters/hash_storage'
|
54
|
+
return HashStorage.new
|
52
55
|
end
|
53
56
|
end
|
57
|
+
|
58
|
+
def self.fallback=(_value)
|
59
|
+
::Rails.logger.error "[DEPRECATION] Lit.fallback= has been deprecated, please use `config.i18n.fallbacks` instead"
|
60
|
+
end
|
54
61
|
end
|
55
62
|
|
56
63
|
if defined? Rails
|
@@ -2,6 +2,7 @@ require 'redis'
|
|
2
2
|
module Lit
|
3
3
|
extend self
|
4
4
|
def redis
|
5
|
+
$redis ||= nil
|
5
6
|
$redis = Redis.new(url: determine_redis_provider) unless $redis
|
6
7
|
$redis
|
7
8
|
end
|
@@ -47,7 +48,7 @@ module Lit
|
|
47
48
|
end
|
48
49
|
|
49
50
|
def clear
|
50
|
-
Lit.redis.del(keys)
|
51
|
+
Lit.redis.del(keys) unless keys.empty?
|
51
52
|
end
|
52
53
|
|
53
54
|
def keys
|
@@ -57,6 +58,7 @@ module Lit
|
|
57
58
|
def has_key?(key)
|
58
59
|
Lit.redis.exists(_prefixed_key(key))
|
59
60
|
end
|
61
|
+
alias key? has_key?
|
60
62
|
|
61
63
|
def incr(key)
|
62
64
|
Lit.redis.incr(_prefixed_key(key))
|
@@ -68,12 +70,16 @@ module Lit
|
|
68
70
|
end
|
69
71
|
end
|
70
72
|
|
73
|
+
def prefix
|
74
|
+
_prefix
|
75
|
+
end
|
76
|
+
|
71
77
|
private
|
72
78
|
|
73
79
|
def _prefix
|
74
80
|
prefix = 'lit:'
|
75
|
-
if Lit.storage_options.is_a?(Hash)
|
76
|
-
prefix += "#{Lit.storage_options[:prefix]}:"
|
81
|
+
if Lit.storage_options.is_a?(Hash) && Lit.storage_options.key?(:prefix)
|
82
|
+
prefix += "#{Lit.storage_options[:prefix]}:"
|
77
83
|
end
|
78
84
|
prefix
|
79
85
|
end
|
data/lib/lit/cache.rb
CHANGED
@@ -20,13 +20,18 @@ module Lit
|
|
20
20
|
class Cache
|
21
21
|
def initialize
|
22
22
|
@hits_counter = Lit.get_key_value_engine
|
23
|
+
@request_info_store = Lit.get_key_value_engine
|
23
24
|
@hits_counter_working = true
|
25
|
+
@keys = nil
|
24
26
|
end
|
25
27
|
|
26
28
|
def [](key)
|
29
|
+
key_without_locale = split_key(key).last
|
27
30
|
update_hits_count(key)
|
28
|
-
|
29
|
-
|
31
|
+
store_request_info(key_without_locale)
|
32
|
+
localization = localizations[key]
|
33
|
+
update_request_keys(key_without_locale, localization)
|
34
|
+
localization
|
30
35
|
end
|
31
36
|
|
32
37
|
def []=(key, value)
|
@@ -46,14 +51,20 @@ module Lit
|
|
46
51
|
end
|
47
52
|
|
48
53
|
def keys
|
49
|
-
|
54
|
+
return @keys if @keys.present?
|
55
|
+
@keys = localizations.keys
|
56
|
+
return @keys if localizations.prefix.nil?
|
57
|
+
@keys = @keys.map do |k|
|
58
|
+
k.gsub(/^#{localizations.prefix}/, '')
|
59
|
+
end
|
50
60
|
end
|
51
61
|
|
52
|
-
def update_locale(key, value, force_array = false)
|
62
|
+
def update_locale(key, value, force_array = false, startup_process = false)
|
53
63
|
key = key.to_s
|
54
64
|
locale_key, key_without_locale = split_key(key)
|
55
65
|
locale = find_locale(locale_key)
|
56
|
-
localization = find_localization(locale, key_without_locale, value, force_array, true)
|
66
|
+
localization = find_localization(locale, key_without_locale, value: value, force_array: force_array, update_value: true)
|
67
|
+
return localization.get_value if startup_process && localization.is_changed?
|
57
68
|
localizations[key] = localization.get_value if localization
|
58
69
|
end
|
59
70
|
|
@@ -64,17 +75,17 @@ module Lit
|
|
64
75
|
|
65
76
|
def delete_locale(key)
|
66
77
|
key = key.to_s
|
78
|
+
keys.delete(key)
|
67
79
|
locale_key, key_without_locale = split_key(key)
|
68
80
|
locale = find_locale(locale_key)
|
69
81
|
delete_localization(locale, key_without_locale)
|
70
82
|
end
|
71
83
|
|
72
84
|
def load_all_translations
|
73
|
-
first = Localization.order(
|
74
|
-
last = Localization.order(
|
75
|
-
if !first ||
|
85
|
+
first = Localization.order(id: :asc).first
|
86
|
+
last = Localization.order(id: :desc).first
|
87
|
+
if !first || (!localizations.has_key?(first.full_key) ||
|
76
88
|
!localizations.has_key?(last.full_key))
|
77
|
-
|
78
89
|
Localization.includes([:locale, :localization_key]).find_each do |l|
|
79
90
|
localizations[l.full_key] = l.get_value
|
80
91
|
end
|
@@ -85,7 +96,7 @@ module Lit
|
|
85
96
|
key = key.to_s
|
86
97
|
locale_key, key_without_locale = split_key(key)
|
87
98
|
locale = find_locale(locale_key)
|
88
|
-
localization = find_localization(locale, key_without_locale)
|
99
|
+
localization = find_localization(locale, key_without_locale, default_fallback: true)
|
89
100
|
localizations[key] = localization.get_value if localization
|
90
101
|
end
|
91
102
|
|
@@ -103,13 +114,12 @@ module Lit
|
|
103
114
|
localization_keys.clear
|
104
115
|
load_all_translations
|
105
116
|
end
|
106
|
-
|
107
117
|
alias_method :clear, :reset
|
108
118
|
|
109
119
|
def find_locale(locale_key)
|
110
120
|
locale_key = locale_key.to_s
|
111
121
|
@locale_cache ||= {}
|
112
|
-
unless @locale_cache.
|
122
|
+
unless @locale_cache.key?(locale_key)
|
113
123
|
locale = Lit::Locale.where(locale: locale_key).first_or_create!
|
114
124
|
@locale_cache[locale_key] = locale
|
115
125
|
end
|
@@ -129,8 +139,8 @@ module Lit
|
|
129
139
|
localizations_scope.find_each do |l|
|
130
140
|
db_localizations[l.full_key] = l.get_value
|
131
141
|
end
|
132
|
-
|
133
|
-
|
142
|
+
exported_keys = nested_string_keys_to_hash(db_localizations)
|
143
|
+
exported_keys.to_yaml
|
134
144
|
end
|
135
145
|
|
136
146
|
def nested_string_keys_to_hash(db_localizations)
|
@@ -141,13 +151,13 @@ module Lit
|
|
141
151
|
end
|
142
152
|
next o
|
143
153
|
end
|
144
|
-
|
154
|
+
nested_keys = {}
|
145
155
|
db_localizations.sort.each do |k, v|
|
146
156
|
key_parts = k.to_s.split('.')
|
147
157
|
converted = key_parts.reverse.reduce(v) { |a, n| { n => a } }
|
148
|
-
|
158
|
+
nested_keys.merge!(converted, &deep_proc)
|
149
159
|
end
|
150
|
-
|
160
|
+
nested_keys
|
151
161
|
end
|
152
162
|
|
153
163
|
def get_global_hits_counter(key)
|
@@ -176,64 +186,66 @@ module Lit
|
|
176
186
|
@localization_keys ||= Lit.get_key_value_engine
|
177
187
|
end
|
178
188
|
|
179
|
-
def find_localization(locale, key_without_locale, value
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
end
|
196
|
-
else
|
197
|
-
value = parse_value(value, locale) unless value.nil?
|
198
|
-
end
|
199
|
-
if value.nil?
|
200
|
-
if Lit.fallback
|
201
|
-
@locale_cache.keys.each do |lc|
|
202
|
-
if lc != locale.locale
|
203
|
-
nk = "#{lc}.#{key_without_locale}"
|
204
|
-
v = localizations[nk]
|
205
|
-
value = v if v.present? && value.nil?
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
if value.nil? && Lit.humanize_key
|
210
|
-
value = key_without_locale.split('.').last.humanize
|
211
|
-
end
|
189
|
+
def find_localization(locale, key_without_locale, value: nil, force_array: false, update_value: false, default_fallback: false)
|
190
|
+
return nil if value.is_a?(Hash)
|
191
|
+
ActiveRecord::Base.transaction do
|
192
|
+
localization_key = find_localization_key(key_without_locale)
|
193
|
+
localization = Lit::Localization.where(locale_id: locale.id). \
|
194
|
+
where(localization_key_id: localization_key.id).first_or_initialize
|
195
|
+
if update_value || localization.new_record?
|
196
|
+
if value.is_a?(Array)
|
197
|
+
value = parse_array_value(value) unless force_array
|
198
|
+
elsif !value.nil?
|
199
|
+
value = parse_value(value, locale)
|
200
|
+
else
|
201
|
+
if ::Rails.application.config.i18n.fallbacks
|
202
|
+
value = fallback_localization(locale, key_without_locale)
|
203
|
+
elsif default_fallback
|
204
|
+
value = fallback_to_default(localization_key, localization)
|
212
205
|
end
|
213
|
-
localization.update_default_value(value)
|
214
206
|
end
|
215
|
-
|
216
|
-
|
207
|
+
localization.update_default_value(value)
|
208
|
+
end
|
209
|
+
return localization
|
210
|
+
end
|
211
|
+
nil
|
212
|
+
end
|
213
|
+
|
214
|
+
# fallback to translation in different locale
|
215
|
+
def fallback_localization(locale, key_without_locale)
|
216
|
+
value = nil
|
217
|
+
return nil unless fallbacks = ::Rails.application.config.i18n.fallbacks
|
218
|
+
keys = fallbacks == true ? @locale_cache.keys : fallbacks
|
219
|
+
keys.map(&:to_s).each do |lc|
|
220
|
+
if lc != locale.locale && value.nil?
|
221
|
+
nk = "#{lc}.#{key_without_locale}"
|
222
|
+
v = localizations[nk]
|
223
|
+
value = v if v.present? && value.nil?
|
217
224
|
end
|
218
|
-
else
|
219
|
-
nil
|
220
225
|
end
|
226
|
+
value
|
227
|
+
end
|
228
|
+
|
229
|
+
# tries to get `default_value` from localization_key - checks other
|
230
|
+
# localizations
|
231
|
+
def fallback_to_default(localization_key, localization)
|
232
|
+
localization_key.localizations.where.not(default_value: nil). \
|
233
|
+
where.not(id: localization.id).first&.default_value
|
221
234
|
end
|
222
235
|
|
223
236
|
def find_localization_for_delete(locale, key_without_locale)
|
224
237
|
localization_key = find_localization_key_for_delete(key_without_locale)
|
225
238
|
return nil unless localization_key
|
226
|
-
Lit::Localization.
|
227
|
-
|
239
|
+
Lit::Localization.find_by(locale_id: locale.id,
|
240
|
+
localization_key_id: localization_key.id)
|
228
241
|
end
|
229
242
|
|
230
243
|
def delete_localization(locale, key_without_locale)
|
231
244
|
localization = find_localization_for_delete(locale, key_without_locale)
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
end
|
245
|
+
return unless localization
|
246
|
+
localizations.delete("#{locale.locale}.#{key_without_locale}")
|
247
|
+
localization_keys.delete(key_without_locale)
|
248
|
+
localization.destroy # or localization.default_value = nil; localization.save!
|
237
249
|
end
|
238
250
|
|
239
251
|
## checks parameter type and returns value basing on it
|
@@ -263,16 +275,26 @@ module Lit
|
|
263
275
|
new_value
|
264
276
|
end
|
265
277
|
|
278
|
+
def parse_array_value(value)
|
279
|
+
new_value = nil
|
280
|
+
value_clone = value.dup
|
281
|
+
while (v = value_clone.shift) && v.present?
|
282
|
+
pv = parse_value(v, locale)
|
283
|
+
new_value = pv unless pv.nil?
|
284
|
+
end
|
285
|
+
new_value
|
286
|
+
end
|
287
|
+
|
266
288
|
def find_localization_key(key_without_locale)
|
267
|
-
unless localization_keys.
|
289
|
+
unless localization_keys.key?(key_without_locale)
|
268
290
|
find_or_create_localization_key(key_without_locale)
|
269
291
|
else
|
270
|
-
Lit::LocalizationKey.
|
292
|
+
Lit::LocalizationKey.find_by(id: localization_keys[key_without_locale]) || find_or_create_localization_key(key_without_locale)
|
271
293
|
end
|
272
294
|
end
|
273
295
|
|
274
296
|
def find_localization_key_for_delete(key_without_locale)
|
275
|
-
lk = Lit::LocalizationKey.
|
297
|
+
lk = Lit::LocalizationKey.find_by(id: localization_keys[key_without_locale]) if localization_keys.has_key?(key_without_locale)
|
276
298
|
lk || Lit::LocalizationKey.where(localization_key: key_without_locale).first
|
277
299
|
end
|
278
300
|
|
@@ -287,14 +309,48 @@ module Lit
|
|
287
309
|
end
|
288
310
|
|
289
311
|
def update_hits_count(key)
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
312
|
+
return unless @hits_counter_working
|
313
|
+
key_without_locale = split_key(key).last
|
314
|
+
@hits_counter.incr('hits_counter.' + key)
|
315
|
+
@hits_counter.incr('global_hits_counter.' + key_without_locale)
|
316
|
+
end
|
317
|
+
|
318
|
+
def store_request_info(key_without_locale)
|
319
|
+
return unless Lit.store_request_info
|
320
|
+
return unless Thread.current[:lit_current_request_path].present?
|
321
|
+
info = get_request_info(key_without_locale)
|
322
|
+
parts = info.split(' ').push(Thread.current[:lit_current_request_path]).uniq
|
323
|
+
parts.shift if parts.count > 10
|
324
|
+
@request_info_store['request_info.' + key_without_locale] = parts.join ' '
|
325
|
+
end
|
326
|
+
|
327
|
+
def update_request_keys(key_without_locale, localization)
|
328
|
+
return if Thread.current[:lit_request_keys].nil?
|
329
|
+
Thread.current[:lit_request_keys] ||= {}
|
330
|
+
Thread.current[:lit_request_keys][key_without_locale] = localization
|
331
|
+
end
|
332
|
+
|
333
|
+
def request_keys
|
334
|
+
Thread.current[:lit_request_keys] || {}
|
335
|
+
end
|
336
|
+
public :request_keys
|
337
|
+
|
338
|
+
def get_request_info(key_without_locale)
|
339
|
+
@request_info_store['request_info.' + key_without_locale].to_s
|
295
340
|
end
|
341
|
+
public :get_request_info
|
342
|
+
|
296
343
|
def self.split_key(key)
|
297
344
|
key.split('.', 2)
|
298
345
|
end
|
346
|
+
|
347
|
+
def self.flatten_hash(hash_to_flatten, parent = [])
|
348
|
+
hash_to_flatten.flat_map do |key, value|
|
349
|
+
case value
|
350
|
+
when Hash then flatten_hash(value, parent + [key])
|
351
|
+
else [(parent + [key]).join('.'), value]
|
352
|
+
end
|
353
|
+
end
|
354
|
+
end
|
299
355
|
end
|
300
356
|
end
|