From 4b4468cae574be7eb1f1fb3719d6d601bc6b050a Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:22:43 +0100 Subject: [PATCH 01/16] new format --- lib/lit/export.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 25a28541..3e77240b 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -3,7 +3,7 @@ module Lit class Export def self.call(locale_keys:, format:, include_hits_count: false) - raise ArgumentError, "format must be yaml or csv" if %i[yaml csv].exclude?(format) + raise ArgumentError, "format must be yaml or csv" if %i[yaml_js yaml csv].exclude?(format) Lit.loader.cache.load_all_translations localizations_scope = Lit::Localization.active if locale_keys.present? @@ -19,7 +19,10 @@ def self.call(locale_keys:, format:, include_hits_count: false) when :yaml exported_keys = nested_string_keys_to_hash(db_localizations) exported_keys.to_yaml - when :csv + when :yaml_js + exported_keys = nested_string_keys_to_hash(db_localizations, "javascript") + exported_keys.to_yaml + when :csv relevant_locales = locale_keys.presence || I18n.available_locales.map(&:to_s) CSV.generate do |csv| csv << ['key', *relevant_locales, ('hits' if include_hits_count)].compact @@ -55,7 +58,7 @@ def self.call(locale_keys:, format:, include_hits_count: false) end end - private_class_method def self.nested_string_keys_to_hash(db_localizations) + private_class_method def self.nested_string_keys_to_hash(db_localizations, key_selector=nil) # http://subtech.g.hatena.ne.jp/cho45/20061122 deep_proc = proc do |_k, s, o| if s.is_a?(Hash) && o.is_a?(Hash) @@ -66,8 +69,10 @@ def self.call(locale_keys:, format:, include_hits_count: false) nested_keys = {} db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') - converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } - nested_keys.merge!(converted, &deep_proc) + if (key_selector.present? && key_parts[0] == key_selector) || key_selector.nil? + converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } + nested_keys.merge!(converted, &deep_proc) + end end nested_keys end From e9cb9850b5fbf0990801887b4e700b48ba358730 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:30:31 +0100 Subject: [PATCH 02/16] new format --- lib/lit/export.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 3e77240b..a8899930 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -3,7 +3,7 @@ module Lit class Export def self.call(locale_keys:, format:, include_hits_count: false) - raise ArgumentError, "format must be yaml or csv" if %i[yaml_js yaml csv].exclude?(format) + raise ArgumentError, "format must be yaml, yaml_js or csv" if %i[yaml_js yaml csv].exclude?(format) Lit.loader.cache.load_all_translations localizations_scope = Lit::Localization.active if locale_keys.present? @@ -21,7 +21,7 @@ def self.call(locale_keys:, format:, include_hits_count: false) exported_keys.to_yaml when :yaml_js exported_keys = nested_string_keys_to_hash(db_localizations, "javascript") - exported_keys.to_yaml + exported_keys.to_json when :csv relevant_locales = locale_keys.presence || I18n.available_locales.map(&:to_s) CSV.generate do |csv| @@ -69,7 +69,8 @@ def self.call(locale_keys:, format:, include_hits_count: false) nested_keys = {} db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') - if (key_selector.present? && key_parts[0] == key_selector) || key_selector.nil? + puts "-------------_> #{key_parts.inspect}" + if (key_selector.present? && key_parts[1] == key_selector) || key_selector.nil? converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } nested_keys.merge!(converted, &deep_proc) end From aad3a6d78ec4148f1d15ecc25c30070309e81e88 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:34:07 +0100 Subject: [PATCH 03/16] remove debug code --- lib/lit/export.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index a8899930..9e2b0663 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -69,7 +69,6 @@ def self.call(locale_keys:, format:, include_hits_count: false) nested_keys = {} db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') - puts "-------------_> #{key_parts.inspect}" if (key_selector.present? && key_parts[1] == key_selector) || key_selector.nil? converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } nested_keys.merge!(converted, &deep_proc) From c47427aea501fee7c8f59d812394ed9dd4a9eed8 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:38:22 +0100 Subject: [PATCH 04/16] change format name --- lib/lit/export.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 9e2b0663..80c9ddc7 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -3,7 +3,7 @@ module Lit class Export def self.call(locale_keys:, format:, include_hits_count: false) - raise ArgumentError, "format must be yaml, yaml_js or csv" if %i[yaml_js yaml csv].exclude?(format) + raise ArgumentError, "format must be yaml, json_js or csv" if %i[json_js yaml csv].exclude?(format) Lit.loader.cache.load_all_translations localizations_scope = Lit::Localization.active if locale_keys.present? @@ -19,7 +19,7 @@ def self.call(locale_keys:, format:, include_hits_count: false) when :yaml exported_keys = nested_string_keys_to_hash(db_localizations) exported_keys.to_yaml - when :yaml_js + when :json_js exported_keys = nested_string_keys_to_hash(db_localizations, "javascript") exported_keys.to_json when :csv From eaae4d5a11ca4d7a46e5c61b5a25558c8aa18616 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:49:54 +0100 Subject: [PATCH 05/16] add js prefix --- lib/lit/export.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 80c9ddc7..100ccc5a 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -70,11 +70,16 @@ def self.call(locale_keys:, format:, include_hits_count: false) db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') if (key_selector.present? && key_parts[1] == key_selector) || key_selector.nil? + key_parts.drop(2) if key_selector.present? converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } nested_keys.merge!(converted, &deep_proc) end end - nested_keys + if (key_selector.present? && key_parts[1] == key_selector) + "var js_locale= #{nested_keys}" + else + nested_keys + end end # This is like Array#transpose but ignores size differences between inner arrays. From 3aaed71ca7f5c53d33616b953e06a7ddcae0ed23 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 14:54:26 +0100 Subject: [PATCH 06/16] add js prefix --- lib/lit/export.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 100ccc5a..1c55719b 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -75,7 +75,7 @@ def self.call(locale_keys:, format:, include_hits_count: false) nested_keys.merge!(converted, &deep_proc) end end - if (key_selector.present? && key_parts[1] == key_selector) + if key_selector.present? "var js_locale= #{nested_keys}" else nested_keys From c7d87ef99eeddc8576455eddcbf0beb5bba856e8 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Wed, 19 Feb 2020 15:16:22 +0100 Subject: [PATCH 07/16] add js prefix --- lib/lit/export.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/lit/export.rb b/lib/lit/export.rb index 1c55719b..c2a6d0a9 100644 --- a/lib/lit/export.rb +++ b/lib/lit/export.rb @@ -21,7 +21,7 @@ def self.call(locale_keys:, format:, include_hits_count: false) exported_keys.to_yaml when :json_js exported_keys = nested_string_keys_to_hash(db_localizations, "javascript") - exported_keys.to_json + "var js_locale= " + exported_keys.to_json when :csv relevant_locales = locale_keys.presence || I18n.available_locales.map(&:to_s) CSV.generate do |csv| @@ -70,16 +70,12 @@ def self.call(locale_keys:, format:, include_hits_count: false) db_localizations.sort.each do |k, v| key_parts = k.to_s.split('.') if (key_selector.present? && key_parts[1] == key_selector) || key_selector.nil? - key_parts.drop(2) if key_selector.present? + key_parts = key_parts.drop(2) if key_selector.present? converted = key_parts.reverse.reduce(v) { |a, n| { n => a } } nested_keys.merge!(converted, &deep_proc) end end - if key_selector.present? - "var js_locale= #{nested_keys}" - else - nested_keys - end + nested_keys end # This is like Array#transpose but ignores size differences between inner arrays. From 6bf7b880287bd16eaff41332a81193c0951c6bf6 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Thu, 27 Feb 2020 10:09:30 +0100 Subject: [PATCH 08/16] add possibility to disable lit --- lib/generators/lit/install/templates/initializer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/lit/install/templates/initializer.rb b/lib/generators/lit/install/templates/initializer.rb index edf471b6..ec181aac 100644 --- a/lib/generators/lit/install/templates/initializer.rb +++ b/lib/generators/lit/install/templates/initializer.rb @@ -50,7 +50,7 @@ # For more information please check the README.md Lit.store_request_info = false -# Initialize lit -Lit.init +# Initialize lit unless it ist disabled by setting the SKIP_LIT enviroment variable. Disabling is usefull to speed up the startup, for example to execute migrations +Lit.init unless ENV["SKIP_LIT"] == "1" From a9db5d62aaf428d9b30008cc00d100d2762be5f2 Mon Sep 17 00:00:00 2001 From: Thorsten Eggert Date: Thu, 27 Feb 2020 10:10:29 +0100 Subject: [PATCH 09/16] log key usage to db --- .../lit/localization_keys_controller.rb | 10 ++++++++++ app/models/lit/localization_key.rb | 2 ++ app/views/lit/dashboard/index.html.erb | 1 + app/views/lit/localization_keys/_sidebar.html.erb | 14 +++++++++++++- app/views/lit/localization_keys/not_used.html.erb | 11 +++++++++++ app/views/lit/localization_keys/used.html.erb | 11 +++++++++++ config/routes.rb | 2 ++ ...nt_and_used_last_at_to_lit_localization_keys.rb | 8 ++++++++ .../lit/install/templates/initializer.rb | 3 +++ lib/lit.rb | 1 + lib/lit/cache.rb | 12 ++++++++++++ 11 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 app/views/lit/localization_keys/not_used.html.erb create mode 100644 app/views/lit/localization_keys/used.html.erb create mode 100644 db/migrate/20200227082719_lit_add_usage_count_and_used_last_at_to_lit_localization_keys.rb diff --git a/app/controllers/lit/localization_keys_controller.rb b/app/controllers/lit/localization_keys_controller.rb index a55f9644..bf80d0ef 100644 --- a/app/controllers/lit/localization_keys_controller.rb +++ b/app/controllers/lit/localization_keys_controller.rb @@ -9,6 +9,16 @@ def index get_localization_keys end + def not_used + @scope = @scope.not_used + get_localization_keys + end + + def used + @scope = @scope.used + get_localization_keys + end + def not_translated @scope = @scope.not_completed get_localization_keys diff --git a/app/models/lit/localization_key.rb b/app/models/lit/localization_key.rb index b3d3bf3e..41836354 100644 --- a/app/models/lit/localization_key.rb +++ b/app/models/lit/localization_key.rb @@ -3,6 +3,8 @@ class LocalizationKey < Lit::Base attr_accessor :interpolated_key ## SCOPES + scope :used, -> { where.not(used_last_at: nil) } + scope :not_used, -> { where(used_last_at: nil) } scope :completed, -> { where(is_completed: true) } scope :not_completed, -> { where(is_completed: false) } scope :starred, -> { where(is_starred: true) } diff --git a/app/views/lit/dashboard/index.html.erb b/app/views/lit/dashboard/index.html.erb index f9dcd4d4..c04d8feb 100644 --- a/app/views/lit/dashboard/index.html.erb +++ b/app/views/lit/dashboard/index.html.erb @@ -1,4 +1,5 @@ All localization keys <%= Lit::LocalizationKey.active.count(:id) %>
+Used localization keys <%= Lit::LocalizationKey.where.not(used_last_at: nil).count(:id) %>
<% @locales.each do |l| %> <%= EmojiFlag.new(l.locale) %> <%= I18n.t("lit.locale_to_languages.#{l.locale}", :default=>l.locale) %>: "><%= l.translated_percentage %>%
<% end %> diff --git a/app/views/lit/localization_keys/_sidebar.html.erb b/app/views/lit/localization_keys/_sidebar.html.erb index 7de3365e..26a8ee2b 100644 --- a/app/views/lit/localization_keys/_sidebar.html.erb +++ b/app/views/lit/localization_keys/_sidebar.html.erb @@ -12,7 +12,19 @@ <% end %>