localized_controllers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +114 -0
- data/Rakefile +29 -0
- data/lib/generators/localized_controllers/USAGE +17 -0
- data/lib/generators/localized_controllers/localized_controllers_generator.rb +30 -0
- data/lib/generators/localized_controllers/templates/controller.rb.tt +5 -0
- data/lib/generators/localized_controllers/templates/view.html.erb.tt +1 -0
- data/lib/localized_controllers.rb +32 -0
- data/lib/localized_controllers/version.rb +5 -0
- data/lib/tasks/localized_controllers_tasks.rake +5 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6048fd7855dcaaddc0b0c6ebd1c009399e3571301e453b5661a314139483bce
|
4
|
+
data.tar.gz: 25ef63b0874e083bf7d56aff9d50d90526f976168b5d0c651ab07c737155a791
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b89fb98cb5786ba3cdf1fd0df6dbd9d6c9fd75256a4baa2b9823fd35c1aafe55d30610eff0c217aecd5ae473ebfde14538bd53cd2602ae5cc489f8df95b7461
|
7
|
+
data.tar.gz: 5961bf0932ac5d9f16de219e55e9a743b6dcce90787b3cea19fd1dad6945d89a67ffce7263771d154f1c132e9065a2ae1038ea279e13f25d1b68483621683efe
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2020
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
[![Build Status](https://travis-ci.com/hamuyuuki/localized_controllers.svg?branch=main)](https://travis-ci.com/hamuyuuki/localized_controllers)
|
2
|
+
[![CI](https://github.com/hamuyuuki/localized_controllers/actions/workflows/ci.yml/badge.svg)](https://github.com/hamuyuuki/localized_controllers/actions/workflows/ci.yml)
|
3
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/22ce36bcfc386745e3b1/maintainability)](https://codeclimate.com/github/hamuyuuki/localized_controllers/maintainability)
|
4
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/22ce36bcfc386745e3b1/test_coverage)](https://codeclimate.com/github/hamuyuuki/localized_controllers/test_coverage)
|
5
|
+
|
6
|
+
# localized_controllers
|
7
|
+
|
8
|
+
`localized_controllers` brings _automagic_ localization to controllers. That's inspired by [Localized Views](https://guides.rubyonrails.org/i18n.html#localized-views) in Rails.
|
9
|
+
|
10
|
+
## Advance preparation
|
11
|
+
|
12
|
+
`localized_controllers` supports only _Setting the Locale from URL Params_ way to resolve the locale now. Please refer to https://guides.rubyonrails.org/i18n.html#setting-the-locale-from-url-params.
|
13
|
+
|
14
|
+
## Getting Started
|
15
|
+
|
16
|
+
Install `localized_controllers` at the command prompt:
|
17
|
+
|
18
|
+
```sh
|
19
|
+
gem install localized_controllers
|
20
|
+
```
|
21
|
+
|
22
|
+
Or add `localized_controllers` to your Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem "localized_controllers"
|
26
|
+
```
|
27
|
+
|
28
|
+
## How to use
|
29
|
+
|
30
|
+
For example, You have `/(en|ja)/localizable_resources` endpoint and route it to `LocalizableResources#index`.
|
31
|
+
|
32
|
+
`config/routes.rb`:
|
33
|
+
|
34
|
+
```rb
|
35
|
+
Rails
|
36
|
+
.application
|
37
|
+
.routes
|
38
|
+
.draw do
|
39
|
+
scope "(:locale)", locale: /en|ja/ do
|
40
|
+
get "localizable_resources", to: "localizable_resources#index"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
`app/controllers/localizable_resources_controller.rb`:
|
46
|
+
|
47
|
+
```rb
|
48
|
+
class LocalizableResourcesController < ApplicationController
|
49
|
+
def index; end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
If you'd like to localize `LocalizableResources#index` to `en` locale, You can generate the controller and its views with `rails generate localized_controllers` command. Then you can serve with `LocalizableResourcesEn#index` when `/en/localizable_resources` endpoint is requested.
|
54
|
+
|
55
|
+
```sh
|
56
|
+
rails generate localized_controllers LocalizableResources en index
|
57
|
+
```
|
58
|
+
|
59
|
+
`app/controllers/localizable_resources_en_controller.rb`:
|
60
|
+
|
61
|
+
```rb
|
62
|
+
class LocalizableResourcesEnController < ApplicationController
|
63
|
+
def index; end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
`app/views/localizable_resources/index.en.html.erb`:
|
68
|
+
|
69
|
+
```html
|
70
|
+
This view is the LocalizableResources#index for the en locale.
|
71
|
+
```
|
72
|
+
|
73
|
+
For more information of `rails generate localized_controllers` command, please see the following:
|
74
|
+
|
75
|
+
```sh
|
76
|
+
$ rails generate localized_controllers
|
77
|
+
Usage:
|
78
|
+
rails generate localized_controllers NAME LOCALE [action action] [options]
|
79
|
+
|
80
|
+
Options:
|
81
|
+
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
|
82
|
+
|
83
|
+
Runtime options:
|
84
|
+
-f, [--force] # Overwrite files that already exist
|
85
|
+
-p, [--pretend], [--no-pretend] # Run but do not make any changes
|
86
|
+
-q, [--quiet], [--no-quiet] # Suppress status output
|
87
|
+
-s, [--skip], [--no-skip] # Skip files that already exist
|
88
|
+
|
89
|
+
Description:
|
90
|
+
Stubs out a new localized controller and its views. Pass the controller name,
|
91
|
+
either CamelCased or under_scored, the locale name and a list of views as
|
92
|
+
arguments.
|
93
|
+
|
94
|
+
To create a localized controller within a module, specify the controller name
|
95
|
+
as a path like 'parent_module/controller_name'.
|
96
|
+
|
97
|
+
This generates a localized controller class in app/controllers and template
|
98
|
+
engine.
|
99
|
+
|
100
|
+
Example:
|
101
|
+
`rails generate localized_controllers LocalizableResources en index show`
|
102
|
+
|
103
|
+
LocalizableResourcesEn controller with URLs like /localizable_resources.
|
104
|
+
Controller: app/controllers/localizable_resources_en_controller.rb
|
105
|
+
Views: app/views/localizable_resources/index.en.html.erb [...]
|
106
|
+
```
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hamuyuuki/localized_controllers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
`localized_controllers` is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "bundler/setup"
|
5
|
+
rescue LoadError
|
6
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rdoc/task"
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = "rdoc"
|
13
|
+
rdoc.title = "LocalizedControllers"
|
14
|
+
rdoc.options << "--line-numbers"
|
15
|
+
rdoc.rdoc_files.include("README.md")
|
16
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
require "bundler/gem_tasks"
|
20
|
+
|
21
|
+
require "rake/testtask"
|
22
|
+
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << "test"
|
25
|
+
t.pattern = "test/**/*_test.rb"
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Description:
|
2
|
+
Stubs out a new localized controller and its views. Pass the controller name,
|
3
|
+
either CamelCased or under_scored, the locale name and a list of views as
|
4
|
+
arguments.
|
5
|
+
|
6
|
+
To create a localized controller within a module, specify the controller name
|
7
|
+
as a path like 'parent_module/controller_name'.
|
8
|
+
|
9
|
+
This generates a localized controller class in app/controllers and template
|
10
|
+
engine.
|
11
|
+
|
12
|
+
Example:
|
13
|
+
`rails generate localized_controllers LocalizableResources en index show`
|
14
|
+
|
15
|
+
LocalizableResourcesEn controller with URLs like /localizable_resources.
|
16
|
+
Controller: app/controllers/localizable_resources_en_controller.rb
|
17
|
+
Views: app/views/localizable_resources/index.en.html.erb [...]
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class LocalizedControllersGenerator < Rails::Generators::NamedBase
|
4
|
+
argument :locale, type: :string
|
5
|
+
argument :actions, type: :array, default: [], banner: "action action"
|
6
|
+
|
7
|
+
source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
def create_controller_file
|
10
|
+
template "controller.rb",
|
11
|
+
File.join(
|
12
|
+
"app/controllers",
|
13
|
+
class_path,
|
14
|
+
"#{file_name}_#{locale}_controller.rb"
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_view_files
|
19
|
+
actions.each do |action|
|
20
|
+
@action = action
|
21
|
+
template "view.html.erb",
|
22
|
+
File.join(
|
23
|
+
"app/views",
|
24
|
+
class_path,
|
25
|
+
file_name,
|
26
|
+
"#{action}.#{locale}.html.erb"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This view is the <%= class_name %>#<%= @action %> for the <%= locale %> locale.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/lazy_load_hooks"
|
4
|
+
|
5
|
+
ActiveSupport.on_load(:action_controller) do
|
6
|
+
ActionDispatch::Request.prepend(
|
7
|
+
Module.new do
|
8
|
+
def controller_class
|
9
|
+
original_controller_class = super
|
10
|
+
|
11
|
+
if path_parameters[:locale]
|
12
|
+
controller_class_for(
|
13
|
+
"#{params[:controller]}_#{path_parameters[:locale]}"
|
14
|
+
)
|
15
|
+
else
|
16
|
+
original_controller_class
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
)
|
21
|
+
ActionController::Base.prepend(
|
22
|
+
Module.new do
|
23
|
+
def default_render(*args)
|
24
|
+
if params["locale"]
|
25
|
+
render(template: "#{params[:controller]}/#{params[:action]}")
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
)
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: localized_controllers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hamuyuuki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: appraisal
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Bring automagic localization to controllers.
|
98
|
+
email:
|
99
|
+
- 13702378+hamuyuuki@users.noreply.github.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/generators/localized_controllers/USAGE
|
108
|
+
- lib/generators/localized_controllers/localized_controllers_generator.rb
|
109
|
+
- lib/generators/localized_controllers/templates/controller.rb.tt
|
110
|
+
- lib/generators/localized_controllers/templates/view.html.erb.tt
|
111
|
+
- lib/localized_controllers.rb
|
112
|
+
- lib/localized_controllers/version.rb
|
113
|
+
- lib/tasks/localized_controllers_tasks.rake
|
114
|
+
homepage: https://github.com/hamuyuuki/localized_controllers
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '2.5'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.7.6.3
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Bring automagic localization to controllers
|
138
|
+
test_files: []
|