Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hamuyuuki/localized_controllers
Bring automagic localization to controllers
https://github.com/hamuyuuki/localized_controllers
gem i18n l10n rails ruby
Last synced: about 1 month ago
JSON representation
Bring automagic localization to controllers
- Host: GitHub
- URL: https://github.com/hamuyuuki/localized_controllers
- Owner: hamuyuuki
- License: mit
- Created: 2019-12-02T07:20:48.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-20T01:47:47.000Z (over 3 years ago)
- Last Synced: 2024-10-01T15:53:04.497Z (about 2 months ago)
- Topics: gem, i18n, l10n, rails, ruby
- Language: Ruby
- Homepage: https://rubygems.org/gems/localized_controllers
- Size: 97.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
[![Gem Version](https://badge.fury.io/rb/localized_controllers.svg)](https://badge.fury.io/rb/localized_controllers)
[![Build Status](https://travis-ci.com/hamuyuuki/localized_controllers.svg?branch=main)](https://travis-ci.com/hamuyuuki/localized_controllers)
[![CI](https://github.com/hamuyuuki/localized_controllers/actions/workflows/ci.yml/badge.svg)](https://github.com/hamuyuuki/localized_controllers/actions/workflows/ci.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/22ce36bcfc386745e3b1/maintainability)](https://codeclimate.com/github/hamuyuuki/localized_controllers/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/22ce36bcfc386745e3b1/test_coverage)](https://codeclimate.com/github/hamuyuuki/localized_controllers/test_coverage)# localized_controllers
`localized_controllers` brings _automagic_ localization to controllers. That's inspired by [Localized Views](https://guides.rubyonrails.org/i18n.html#localized-views) in Rails.
## Advance preparation
`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.
## Getting Started
Install `localized_controllers` at the command prompt:
```sh
gem install localized_controllers
```Or add `localized_controllers` to your Gemfile:
```ruby
gem "localized_controllers"
```## How to use
For example, You have `/(en|ja)/localizable_resources` endpoint and route it to `LocalizableResources#index`.
`config/routes.rb`:
```rb
Rails
.application
.routes
.draw do
scope "(:locale)", locale: /en|ja/ do
get "localizable_resources", to: "localizable_resources#index"
end
end
````app/controllers/localizable_resources_controller.rb`:
```rb
class LocalizableResourcesController < ApplicationController
def index; end
end
```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.
```sh
rails generate localized_controllers LocalizableResources en index
````app/controllers/localizable_resources_en_controller.rb`:
```rb
class LocalizableResourcesEnController < ApplicationController
def index; end
end
````app/views/localizable_resources/index.en.html.erb`:
```html
This view is the LocalizableResources#index for the en locale.
```For more information of `rails generate localized_controllers` command, please see the following:
```sh
$ rails generate localized_controllers
Usage:
rails generate localized_controllers NAME LOCALE [action action] [options]Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend], [--no-pretend] # Run but do not make any changes
-q, [--quiet], [--no-quiet] # Suppress status output
-s, [--skip], [--no-skip] # Skip files that already existDescription:
Stubs out a new localized controller and its views. Pass the controller name,
either CamelCased or under_scored, the locale name and a list of views as
arguments.To create a localized controller within a module, specify the controller name
as a path like 'parent_module/controller_name'.This generates a localized controller class in app/controllers and template
engine.Example:
`rails generate localized_controllers LocalizableResources en index show`LocalizableResourcesEn controller with URLs like /localizable_resources.
Controller: app/controllers/localizable_resources_en_controller.rb
Views: app/views/localizable_resources/index.en.html.erb [...]
```## Contributing
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.
## License
`localized_controllers` is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).