fractual_i18n 0.1.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 129ab31c55042e1c874a3bfc3bf2fd5138b05ca12096422a3560df6996a92c5a
4
- data.tar.gz: 65fbb964162111aeb79513a2d6400ea89bac32c58d47e00f101f64d54df5d117
3
+ metadata.gz: 93973ed14052dc7838096589ba14a8a9df64aec2ccc9dbcbbaa4af772d8042c5
4
+ data.tar.gz: bd05da1a510e772a97fa79309b6d23e301893bf738c91855e166fff9eb8fcce8
5
5
  SHA512:
6
- metadata.gz: 6838fecf53905311dc16fdc8172499cf3d12dc684114956815723c0f9e452e7837b77c43c60dc8a6ea1ebf2f5dbec245654eaddcc5b195054d11a7307b26f9c1
7
- data.tar.gz: 922b001301f42058c7f7b0c52518426a0d7daff6e1c5fa799105a9a32c744e86e336324d8b49105dc60dc1a81cd9e7e20336258ab415d864fac8435f4176b7f4
6
+ metadata.gz: 4b5bdb966bd270f157678c4bb9eb879c0489d4c8b0923eaacb4aadf9e7d95e7fa48fa4ef43360b629acb981167482ebf63216768a4716d8a10d4fe0c42e115a9
7
+ data.tar.gz: a2cc062386cceb7d85492a361f7b3bb1c1a9c1e26cce2c1d01cb9673d286709359ade856df010f185f7e6cb6e08db1bf9454b1043c690da548b975a5416acf0e
data/Gemfile.lock CHANGED
@@ -1,16 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fractual_i18n (0.1.0)
4
+ fractual_i18n (0.4.0)
5
5
  i18n
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- concurrent-ruby (1.1.7)
11
- i18n (1.8.5)
10
+ concurrent-ruby (1.1.9)
11
+ i18n (1.10.0)
12
12
  concurrent-ruby (~> 1.0)
13
- minitest (5.14.1)
13
+ minitest (5.15.0)
14
14
  rake (12.3.3)
15
15
 
16
16
  PLATFORMS
@@ -22,4 +22,4 @@ DEPENDENCIES
22
22
  rake (~> 12.0)
23
23
 
24
24
  BUNDLED WITH
25
- 2.1.4
25
+ 2.3.4
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FractualI18n::Backend
2
4
  def load_yml(filename)
3
5
  if (fractual_path = FractualI18n.configuration.fractual_paths.find { |path| filename.starts_with?(path) })
4
- content = YAML.load_file(filename)
6
+ content = load_yml_file_unsafely(filename)
5
7
  keys = filename.delete_prefix(fractual_path).delete_prefix("/").split("/")
6
8
  last_key = keys.pop.delete_suffix(".yml")
7
9
  keys << last_key
@@ -12,9 +14,19 @@ module FractualI18n::Backend
12
14
  translations[locale] = keys.reverse.inject(content[locale.to_s]) { |translation, key| { key => translation } }
13
15
  end
14
16
  else # default
15
- YAML.load_file(filename)
17
+ load_yml_file_unsafely(filename)
16
18
  end
17
19
  rescue TypeError, ScriptError, StandardError => e
18
20
  raise I18n::InvalidLocaleData.new(filename, e.inspect)
19
21
  end
22
+
23
+ private
24
+
25
+ def load_yml_file_unsafely(filename)
26
+ if YAML.respond_to?(:unsafe_load_file) # Psych 4.0 way
27
+ YAML.unsafe_load_file(filename)
28
+ else
29
+ YAML.load_file(filename)
30
+ end
31
+ end
20
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class FractualI18n::Configuration
2
4
  attr_accessor :available_locales, :fractual_paths
3
5
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FractualI18n::Phrases
4
+ def joined(locale:)
5
+ original_load_path = I18n.load_path
6
+
7
+ I18n.load_path = fractual_files
8
+ I18n.with_locale(locale) { I18n.t(".") }
9
+ ensure
10
+ I18n.load_path = original_load_path
11
+ end
12
+
13
+ def store(translations, locale:)
14
+ fractual_files.each do |filename|
15
+ content = YAML.load_file(filename)
16
+
17
+ fractual_path = FractualI18n.configuration.fractual_paths.find { |path| filename.starts_with?(path) }
18
+ keys = filename.delete_prefix(fractual_path).delete_prefix("/").split("/")
19
+ last_key = keys.pop.delete_suffix(".yml")
20
+ keys << last_key
21
+ keys.map! { |key| key.delete_prefix("_") } # remove underscore from partial name
22
+
23
+ if new_content = translations.dig(locale.to_s, *keys)
24
+ content[locale.to_s] = content.fetch(locale.to_s, {}).merge(new_content)
25
+ end
26
+
27
+ File.write(filename, content.to_yaml(line_width: 200))
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def fractual_files
34
+ I18n.load_path.select do |path|
35
+ FractualI18n.configuration.fractual_paths.any? { |fp| path.starts_with?(fp) }
36
+ end
37
+ end
38
+ end
@@ -1,7 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/railtie"
2
4
 
3
5
  class FractualI18n::Railtie < Rails::Railtie
4
6
  initializer "fractual_i18n.set_fractual_paths" do |app|
5
7
  FractualI18n.configuration.fractual_paths = app.config.paths["app/views"].existent
6
8
  end
9
+
10
+ rake_tasks do
11
+ load "tasks/fractual_i18n.rake"
12
+ end
7
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FractualI18n
2
- VERSION = "0.1.0"
4
+ VERSION = "0.4.0"
3
5
  end
data/lib/fractual_i18n.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "i18n"
2
4
  require "fractual_i18n/version"
3
5
  require "fractual_i18n/configuration"
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :fractual_i18n do
4
+ desc "Export view translations to single file"
5
+ task :export, [:to] => :environment do |_task, args|
6
+ require "fractual_i18n/phrases"
7
+ destination = args[:to] || "tmp/fractual_i18n/exported"
8
+
9
+ FileUtils.mkdir_p(destination)
10
+ FractualI18n.configuration.available_locales.each do |locale|
11
+ translations = FractualI18n::Phrases.new.joined(locale: locale)
12
+ Dir.chdir(destination) do
13
+ content = {locale.to_s => translations.deep_stringify_keys}.to_yaml(line_width: 200)
14
+ File.write("#{locale}.yml", content)
15
+ puts "#{destination}/#{locale}.yml exported"
16
+ end
17
+ end
18
+ end
19
+
20
+ desc "Import view translations from single file"
21
+ task :import, [:from] => :environment do |_task, args|
22
+ require "fractual_i18n/phrases"
23
+ destination = args[:from] || "tmp/fractual_i18n/import"
24
+
25
+ Dir.each_child(destination) do |file|
26
+ content = YAML.load_file("#{destination}/#{file}")
27
+ locale = content.keys.first
28
+
29
+ FractualI18n::Phrases.new.store(content, locale: locale)
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fractual_i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Sęk
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-08-26 00:00:00.000000000 Z
12
+ date: 2022-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
@@ -44,8 +44,10 @@ files:
44
44
  - lib/fractual_i18n.rb
45
45
  - lib/fractual_i18n/backend.rb
46
46
  - lib/fractual_i18n/configuration.rb
47
+ - lib/fractual_i18n/phrases.rb
47
48
  - lib/fractual_i18n/railtie.rb
48
49
  - lib/fractual_i18n/version.rb
50
+ - lib/tasks/fractual_i18n.rake
49
51
  homepage: https://github.com/tiramizoo/fractual_i18n
50
52
  licenses:
51
53
  - MIT
@@ -67,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
69
  - !ruby/object:Gem::Version
68
70
  version: '0'
69
71
  requirements: []
70
- rubygems_version: 3.1.2
72
+ rubygems_version: 3.3.4
71
73
  signing_key:
72
74
  specification_version: 4
73
75
  summary: Supports loading translation files from views folder