fedux_org-stdlib 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b853439520dde85108744f24b94463130791d82
4
- data.tar.gz: 8f97edebdae65cd7cf58fd3a402ad3feb81d98f9
3
+ metadata.gz: 6072d2ee52852b69d67004d425cce3c294e77e73
4
+ data.tar.gz: 17112604482c98ef7b65824892a5388b29558111
5
5
  SHA512:
6
- metadata.gz: 09192109cee44cc63e2364cb48954f75779dcf1d5b23bf537bff096c874b52da9ced615594720363e9775eb460da5162d1ad3ee00996e8ed679ae5e0170c77b3
7
- data.tar.gz: 05671174c5b4691e01b25a364e8471cfbcd093d5d95862a28e068ec810999f8f85372b3d6688df3f6a392e2cfdac05ed68c8877b46ecf7b355b6fa4f0276577b
6
+ metadata.gz: 0532c2ef208d17396ded1f68fe9eea9916c286c6c6abca81ba9eda8e40cc8a54607748386defd17bcc91a5bbb62de7a5e4e27f0976d24187ccaed5caded1798c
7
+ data.tar.gz: 46854ae94ace7bd1950f2336601ae1a0412b40c608c9c848f7e2166847396cc7ef40bf2ba03bd55dfb172dc1ae569460280dc8a684fcc8b4127e89570755b102
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fedux_org-stdlib (0.8.3)
4
+ fedux_org-stdlib (0.8.4)
5
5
  activesupport
6
6
 
7
7
  PATH
@@ -0,0 +1,90 @@
1
+ require 'fedux_org_stdlib/shell_language_detector'
2
+ require 'fedux_org_stdlib/logging/logger'
3
+ require_library %w(i18n)
4
+
5
+ # encoding: utf-8
6
+ module FeduxOrgStdlib
7
+ # Configuration for language on cli
8
+ #
9
+ # Make sure you have translations for
10
+ # errors:
11
+ # use_empty_locale:
12
+ # use_invalid_locale:
13
+ class LocaleConfigurator
14
+ private
15
+
16
+ attr_reader :pattern, :detector, :detected_locale, :logger
17
+
18
+ public
19
+
20
+ def initialize(
21
+ path:,
22
+ default_locale: nil,
23
+ enforce_available_locales: true,
24
+ detector: FeduxOrgStdlib::ShellLanguageDetector.new,
25
+ logger: FeduxOrgStdlib::Logging::Logger.new
26
+ )
27
+ @pattern = File.join(path, '*.yml')
28
+ @detector = detector
29
+ @detected_locale = detect_locale(overwrite: default_locale)
30
+
31
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
32
+ I18n.load_path = locale_files
33
+ I18n.backend.load_translations
34
+
35
+ I18n.default_locale = @detected_locale
36
+ I18n.available_locales = available_locales
37
+ I18n.enforce_available_locales = enforce_available_locales
38
+ end
39
+
40
+ def use_locale(l)
41
+ l = l.to_sym
42
+
43
+ if l.blank?
44
+ logger.warn I18n.t('errors.use_empty_locale', codes: available_locales.to_list, fallback_locale: detected_locale)
45
+ return
46
+ end
47
+
48
+ unless valid_locale?(l)
49
+ logger.warn I18n.t('errors.use_invalid_locale', codes: available_locales.to_list, fallback_locale: detected_locale)
50
+ return
51
+ end
52
+
53
+ I18n.locale = l
54
+ end
55
+
56
+ def available_locales
57
+ locale_files.map { |f| File.basename(f, '.yml').to_sym }
58
+ end
59
+
60
+ def t(*args, &block)
61
+ I18n.t(*args, &block)
62
+ end
63
+
64
+ def validate_and_return_locale(l)
65
+ return l.to_sym if valid_locale? l
66
+
67
+ logger.warn I18n.t('errors.use_invalid_locale', codes: available_locales.to_list, fallback_locale: detected_locale)
68
+
69
+ detected_locale
70
+ end
71
+
72
+ private
73
+
74
+ def valid_locale?(l)
75
+ available_locales.include? l.to_sym
76
+ end
77
+
78
+ def detect_locale(overwrite:)
79
+ options = {}
80
+ options[:allowed] = available_locales
81
+ options[:overwrite] = overwrite.to_sym if overwrite
82
+
83
+ detector.detect(**options).language_code
84
+ end
85
+
86
+ def locale_files
87
+ @locale_files ||= Dir.glob(pattern)
88
+ end
89
+ end
90
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  # FeduxOrgStdlib
3
3
  module FeduxOrgStdlib
4
- VERSION = '0.8.4'
4
+ VERSION = '0.8.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fedux_org-stdlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Meyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -90,6 +90,7 @@ files:
90
90
  - lib/fedux_org_stdlib/gem_plugins/plugin.rb
91
91
  - lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb
92
92
  - lib/fedux_org_stdlib/list.rb
93
+ - lib/fedux_org_stdlib/locale_configurator.rb
93
94
  - lib/fedux_org_stdlib/logging.rb
94
95
  - lib/fedux_org_stdlib/logging/logger.rb
95
96
  - lib/fedux_org_stdlib/logging/severity.rb