rodauth-i18n 0.3.0 → 0.3.1

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
  SHA256:
3
- metadata.gz: 017c38a897546999927ec0acb752e2e5315bc895199ec95cdde3f2fd1b5ae85d
4
- data.tar.gz: 3173ef8d111825aea4e7f48433e72ad6b3dcfd1cf0453e6cb7f2e7a024903452
3
+ metadata.gz: ff0034b4e6b28f5822ecc9ee1698ab728995e0e063f78dbfbf0c9e8d0d819a84
4
+ data.tar.gz: 61899a57abb184b686676a27a215c89098a47b747801a02b651f097179055332
5
5
  SHA512:
6
- metadata.gz: cc1047e0a25041e178c839b680070352265d0aaf355b008912fac23f70ec17b4cbf7b8b663d95383cddecfe61f4f9a31818d1ddb6bf861fe72c74423882b487d
7
- data.tar.gz: 5a83eb687df9fb3080edea0fbc00079bb5b59b4d7c5c10a6e0507c25222dfa2bf13075be6c7110dcf3233295df817e3c87d9d6240d270978f5f5368694d7ecef
6
+ metadata.gz: 539e08db905e57c51b88d0b4619d9d330ec279dbb843dd4d042f2bfd20c81bcbd2d76a88b11e3796f4f0e37046467399547a5c97cf24a647d7dc660a18e11e77
7
+ data.tar.gz: b16c1407a6e3887297c52c938e4e4f1f0cf80db6e0a9f6d13390258130e67200969e82ea538e343c0944818d79fa8eeda400011e8fbfda21e3dc9ae7569fdd32
data/README.md CHANGED
@@ -9,7 +9,7 @@ It also includes built-in translations for various languages, which you are enco
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem "rodauth-i18n", "~> 0.2"
12
+ gem "rodauth-i18n", "~> 0.3"
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -82,6 +82,8 @@ $ rails generate rodauth:translations
82
82
  # imports translations for available locales
83
83
  ```
84
84
 
85
+ Only translations for currently enabled Rodauth features will be imported. Re-running the generator will import translations for any newly enabled features, remove translations for any disabled features, and keep any existing translations unchanged.
86
+
85
87
  On other web frameworks, you can copy the translation files directly from the `locales/` directory.
86
88
 
87
89
  ### Raising on missing translations
@@ -1,4 +1,5 @@
1
- require "rails/generators/base"
1
+ require "rails/generators"
2
+ require "active_support/core_ext/hash/slice"
2
3
 
3
4
  module Rodauth
4
5
  module Rails
@@ -10,17 +11,69 @@ module Rodauth
10
11
  desc: "List of locales to copy translation files for"
11
12
 
12
13
  def copy_locales
14
+ say "No locales specified!", :yellow if locales.empty?
15
+
13
16
  locales.each do |locale|
14
- copy_file("#{locale}.yml", "config/locales/rodauth.#{locale}.yml")
15
- end
17
+ translations = retrieve_translations(locale)
16
18
 
17
- say "No locales specified!", :yellow if locales.empty?
19
+ if translations.empty?
20
+ say "No translations for locale: #{locale}", :yellow
21
+ next
22
+ end
23
+
24
+ # retain translations the user potentially changed
25
+ translations.merge!(existing_translations(locale))
26
+ # keep only translations for currently enabled features
27
+ translations.slice!(*rodauth_methods)
28
+
29
+ create_file "config/locales/rodauth.#{locale}.yml", locale_content(locale, translations)
30
+ end
18
31
  end
19
32
 
20
33
  private
21
34
 
35
+ def retrieve_translations(locale)
36
+ files = translation_files(locale)
37
+ files.inject({}) do |translations, file|
38
+ translations.merge YAML.load_file(file)[locale]["rodauth"]
39
+ end
40
+ end
41
+
42
+ def existing_translations(locale)
43
+ destination = File.join(destination_root, "config/locales/rodauth.#{locale}.yml")
44
+
45
+ # try to load existing translations first
46
+ if File.exist?(destination)
47
+ YAML.load_file(destination)[locale]["rodauth"]
48
+ else
49
+ {}
50
+ end
51
+ end
52
+
53
+ def translation_files(locale)
54
+ # ensure Rodauth configuration ran in autoloaded environment
55
+ Rodauth::Rails.app
56
+
57
+ Rodauth::I18n.directories
58
+ .map { |directory| Dir["#{directory}/#{locale}.yml"] }
59
+ .inject(:+)
60
+ end
61
+
62
+ def rodauth_methods
63
+ rodauths
64
+ .flat_map { |rodauth| rodauth.instance_methods - Object.instance_methods }
65
+ .map(&:to_s)
66
+ .sort
67
+ end
68
+
69
+ def locale_content(locale, translations)
70
+ data = { locale => { "rodauth" => translations } }
71
+ yaml = YAML.dump(data, line_width: 10_000) # disable line wrapping
72
+ yaml.split("\n", 2).last # remove "---" header
73
+ end
74
+
22
75
  def locales
23
- selected_locales || available_locales
76
+ selected_locales || available_locales.map(&:to_s)
24
77
  end
25
78
 
26
79
  def available_locales
@@ -1,3 +1,4 @@
1
+ require "set"
1
2
  require "i18n"
2
3
 
3
4
  module Rodauth
@@ -16,6 +17,12 @@ module Rodauth
16
17
  :i18n_locale,
17
18
  )
18
19
 
20
+ @directories = Set.new
21
+
22
+ class << self
23
+ attr_reader :directories
24
+ end
25
+
19
26
  def post_configure
20
27
  super
21
28
  i18n_register File.expand_path("#{__dir__}/../../../locales")
@@ -61,6 +68,7 @@ module Rodauth
61
68
  files = i18n_files(directory)
62
69
  files.each { |file| i18n_add(file) }
63
70
  i18n_reload
71
+ Rodauth::I18n.directories << directory
64
72
  end
65
73
 
66
74
  # Returns list of translation files in given directory based on
data/rodauth-i18n.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "rodauth-i18n"
5
- spec.version = "0.3.0"
5
+ spec.version = "0.3.1"
6
6
  spec.authors = ["Janko Marohnić"]
7
7
  spec.email = ["janko@hey.com"]
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodauth-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-26 00:00:00.000000000 Z
11
+ date: 2022-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rodauth