i18n_tasks-plugin-view_component 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97221f5ca2ce493a3bb6e7a6c49e1f9aa0019a0f3582e7b0dc17908bb051e99a
4
+ data.tar.gz: 55e0f101dd42197a6d90b164c06d20b3454e5a303474150d32162969c2765d0f
5
+ SHA512:
6
+ metadata.gz: 253bf83083cb7eb66eb397f7348ff188fb9548f14fd13859b7b614a4f85e85a82d3601428ee318662189b2713c4425fe61207337f0adacbe03fbbd78df6e8d85
7
+ data.tar.gz: fac6873b47c98fe35d1202ba2c73b01b927af48b3ccac00eba12d89c557ce2f213e2769edbb127ef801231e77ed9a886c528c2dacd679c6796084503c12d49e8
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+
5
+ Layout/LineLength:
6
+ Max: 120
7
+
8
+ Metrics/BlockLength:
9
+ AllowedMethods:
10
+ - RSpec.describe
11
+ - describe
12
+
13
+ Style/StringLiterals:
14
+ Enabled: true
15
+ EnforcedStyle: single_quotes
16
+
17
+ Style/StringLiteralsInInterpolation:
18
+ Enabled: true
19
+ EnforcedStyle: double_quotes
20
+
21
+ Style/Documentation:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-11
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in i18n_tasks-plugin-view_component.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.68'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Jochen Lutz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # I18nTasks::Plugin::ViewComponent
2
+
3
+ This gem adds support for [View Component](https://viewcomponent.org/)
4
+ to [i18n-tasks](https://github.com/glebm/i18n-tasks).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'i18n_tasks-plugin-view_component', require: false
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install i18n_tasks-plugin-view_component
21
+
22
+ ## Usage
23
+
24
+ Include the gem in your `i18n-tasks.yml.erb`. If you have a `i18n-tasks.yml`, just rename it.
25
+
26
+ ```erb
27
+ <% require 'i18n_tasks/plugin/view_component' %>
28
+ ```
29
+
30
+ Add the following config to the `data:` section.
31
+
32
+ ```yaml
33
+ adapter: I18nTasks::Plugin::ViewComponent::Filesystem
34
+ ```
35
+
36
+ If you store your components somewhere different from the default `app/components`,
37
+ add the base directory also to the `data:` section.
38
+
39
+ ```
40
+ view_component_root: app/components
41
+ ```
42
+
43
+ ## Compatibility Details
44
+
45
+ While translations in the View Components's ruby code work (since the addition of
46
+ `relative_exclude_method_name_paths` in i18n-tasks version 1.0.10), putting
47
+ the locale files with the component code does not work, as these locale files
48
+ put all strings directly beneath the language key:
49
+
50
+ ```
51
+ # app/components/demo_app/example_component.yml
52
+ en:
53
+ hello: "Hello world!"
54
+ ```
55
+
56
+ I18n-tasks scans this as `hello`, while ViewComponent prefixes all strings with
57
+ the component's fully qualified classname: `demo_app.example_component.hello`.
58
+ This scope `demo_app.example_component` is used to resolve all relative i18n
59
+ keys.
60
+
61
+ This plugin adds logic to i18n-tasks to put the locale strings in the correct
62
+ scope while loading the data.
63
+
64
+ Additionally, it also adds a scanner for `.erb` files, that correctly handles
65
+ files in sidecar directories, where vanilla i18n-tasks resolves the scope with
66
+ the component's name duplicated. E.g. `t('.hello')` in
67
+ `app/components/demo_app/example_component/example_component.html.erb` leads to
68
+ `demo_app.example_component.example_component.hello` with vanilla i18n-tasks,
69
+ while with this plugin, it resolves to `demo_app.example_component.hello`,
70
+ which ViewComponent uses internally.
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/ionos-cloud/i18n-tasks-view-component).
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'i18n_tasks/plugin/view_component'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/i18n_tasks/plugin/view_component/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'i18n_tasks-plugin-view_component'
7
+ spec.version = I18nTasks::Plugin::ViewComponent::VERSION
8
+ spec.authors = ['Jochen Lutz']
9
+ spec.email = ['jochen.lutz@ionos.com']
10
+
11
+ spec.summary = 'Integrate ViewComponent with I18n-Tasks'
12
+ spec.description = 'support for View Component (https://viewcomponent.org/) in i18n-tasks (https://github.com/glebm/i18n-tasks)'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.7.0'
15
+
16
+ spec.metadata['source_code_uri'] = 'https://github.com/ionos-cloud/i18n-tasks-view-component'
17
+ spec.homepage = spec.metadata['source_code_uri']
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'i18n-tasks', '~> 1.0'
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ spec.metadata['rubygems_mfa_required'] = 'true'
36
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nTasks
4
+ module Plugin
5
+ module ViewComponent
6
+ class ComponentErbScanner < I18n::Tasks::Scanners::ErbAstScanner
7
+ def absolute_key(key, path, roots: config[:relative_roots],
8
+ exclude_method_name_paths: config[:relative_exclude_method_name_paths],
9
+ calling_method: nil)
10
+ result = super
11
+
12
+ result.gsub(/(\w+)\.(?=\1#{key})/, '')
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n/tasks'
4
+
5
+ module I18nTasks
6
+ module Plugin
7
+ module ViewComponent
8
+ class Filesystem < ::I18n::Tasks::Data::FileSystemBase
9
+ register_adapter :yaml, '*.yml', ::I18n::Tasks::Data::Adapter::YamlAdapter
10
+ register_adapter :json, '*.json', ::I18n::Tasks::Data::Adapter::JsonAdapter
11
+
12
+ def initialize(config) # rubocop:disable Metrics/MethodLength
13
+ @view_component_root = config.fetch(:view_component_root, 'app/components').gsub(%r{/$}, '')
14
+
15
+ erb_conf = I18n::Tasks::UsedKeys::SEARCH_DEFAULTS[:scanners].find do |s|
16
+ s[0] == '::I18n::Tasks::Scanners::ErbAstScanner'
17
+ end
18
+
19
+ if erb_conf
20
+ erb_conf[1][:exclude] ||= []
21
+ erb_conf[1][:exclude].push(component_root_erb_pattern)
22
+ end
23
+
24
+ super
25
+
26
+ I18n::Tasks.add_scanner(
27
+ 'I18nTasks::Plugin::ViewComponent::ComponentErbScanner',
28
+ only: [component_root_erb_pattern]
29
+ )
30
+ end
31
+
32
+ ##
33
+ # Overloaded from `I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)
34
+ #
35
+ # In component-local locale files, it adds the component's module hierarchy to the locale scope,
36
+ # as does `ViewComponent::Translatable`.
37
+ #
38
+ # Locale files outside the ViewComponent directory are not modified.
39
+ #
40
+ def load_file(path)
41
+ result = super
42
+
43
+ return result unless component_locale_data?(path)
44
+
45
+ locale = result.keys.first
46
+ key_parts = [locale, *component_keys(path)]
47
+
48
+ key_parts.reverse.inject(result[locale]) { |akk, key| { key => akk } }
49
+ end
50
+
51
+ ##
52
+ # Overloaded from `I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)
53
+ #
54
+ # This is the reverse method of `#load_file` above.
55
+ # It takes a locale tree and removes the scope of the component.
56
+ #
57
+ # As above, locale files outside the ViewComponent directory are not modified.
58
+ #
59
+ def normalized?(path, tree)
60
+ if component_locale_data?(path)
61
+ super(path, tree_without_component_keys(path, tree))
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ ##
68
+ # Overloaded from `I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)
69
+ #
70
+ # This is needed to reverse the effects i18n-task's standard router has in locale files inside ViewComponent's
71
+ # directories when invoking `i18n-tasks normalize`.
72
+ #
73
+ # As above, locale files outside the ViewComponent directory are not modified.
74
+ #
75
+ def write_tree(path, tree, sort = nil)
76
+ if component_locale_data?(path)
77
+ super(path, tree_without_component_keys(path, tree))
78
+ else
79
+ super
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def component_locale_data?(path)
86
+ path.start_with?(@view_component_root)
87
+ end
88
+
89
+ def component_keys(path)
90
+ root_dir_levels = @view_component_root.split('/').size
91
+
92
+ components = File.dirname(path).split('/')[root_dir_levels..]
93
+
94
+ basename = File.basename(path).gsub(/(\.\w{2})?\.yml$/, '')
95
+
96
+ components << basename unless components.last == basename
97
+
98
+ components
99
+ end
100
+
101
+ def tree_without_component_keys(path, tree)
102
+ new_tree = tree.dup
103
+
104
+ new_tree.list.first.children = component_keys(path).inject(new_tree.list.first) { |akk, el| akk[el] }.children
105
+
106
+ new_tree
107
+ end
108
+
109
+ def component_root_erb_pattern
110
+ @component_root_erb_pattern ||= "#{@view_component_root}/**/*.erb"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nTasks
4
+ module Plugin
5
+ module ViewComponent
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'view_component/filesystem'
4
+ require_relative 'view_component/component_erb_scanner'
5
+ require_relative 'view_component/version'
6
+
7
+ module I18nTasks
8
+ module Plugin
9
+ module ViewComponent
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module I18nTasks
2
+ module Plugin
3
+ module ViewComponent
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_tasks-plugin-view_component
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jochen Lutz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n-tasks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: support for View Component (https://viewcomponent.org/) in i18n-tasks
28
+ (https://github.com/glebm/i18n-tasks)
29
+ email:
30
+ - jochen.lutz@ionos.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - i18n_tasks-plugin-view_component.gemspec
45
+ - lib/i18n_tasks/plugin/view_component.rb
46
+ - lib/i18n_tasks/plugin/view_component/component_erb_scanner.rb
47
+ - lib/i18n_tasks/plugin/view_component/filesystem.rb
48
+ - lib/i18n_tasks/plugin/view_component/version.rb
49
+ - sig/i18n_tasks/plugin/view_component.rbs
50
+ homepage: https://github.com/ionos-cloud/i18n-tasks-view-component
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ source_code_uri: https://github.com/ionos-cloud/i18n-tasks-view-component
55
+ homepage_uri: https://github.com/ionos-cloud/i18n-tasks-view-component
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.7.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.6
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Integrate ViewComponent with I18n-Tasks
76
+ test_files: []