-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* localization scope recipe * . * mv info section to routing file
- Loading branch information
Showing
5 changed files
with
142 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
Using a locale scope is an effective way to set the locale for your users. | ||
|
||
### Only Avo engine | ||
|
||
On the community plan, the only requirement is to wrap the Avo engine mount within the scope. | ||
|
||
:::warning | ||
This will work for Avo's engine routes but won't work for any of the nested engines. | ||
|
||
If you're using `avo-pro`, `avo-advanced` or any other Avo engine check the next section. | ||
::: | ||
|
||
```ruby{4-6} | ||
# config/routes.rb | ||
Rails.application.routes.draw do | ||
scope ":locale" do | ||
mount Avo::Engine, at: Avo.configuration.root_path | ||
end | ||
end | ||
``` | ||
|
||
### Avo engine and nested engines | ||
|
||
Because of how Rails is mounting engines, that locale scope is not being applied to nested engines like `avo-advanced`, `avo-pro`, etc. | ||
|
||
The fix here is to tell Avo not to mount the engines and have them mounted yourself. | ||
|
||
```ruby {5} | ||
# config/avo.rb | ||
|
||
Avo.configure do |config| | ||
# Disable automatic engine mounting | ||
config.mount_avo_engines = false | ||
end | ||
``` | ||
|
||
Once automatic engine mounting is disabled, the next step is to mount the Avo engine and its nested engines within the locale scope. | ||
|
||
```ruby {4-6,9-15} | ||
# config/routes.rb | ||
|
||
Rails.application.routes.draw do | ||
scope ":locale" do | ||
mount Avo::Engine, at: Avo.configuration.root_path | ||
end | ||
end | ||
|
||
if defined? ::Avo | ||
Avo::Engine.routes.draw do | ||
scope "(:locale)" do # Take note of the parentheses | ||
instance_exec(&Avo.mount_engines) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
:::warning | ||
Take note of the parentheses around the `(:locale)` scope when mounting the engines. These parentheses are essential to ensure proper functionality. | ||
::: | ||
|
||
This will instruct Rails to add the locale scope to all Avo nested engines too. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
outline: [2,3] | ||
--- | ||
|
||
# Localization scope 🌍✨ | ||
|
||
Implementing a localization scope in Avo allows you to seamlessly adapt your application to support multiple languages, enhancing the user experience. This recipe will guide you through the steps to configure a locale scope, ensuring your application dynamically sets and respects the user's preferred language. Let's dive in! | ||
|
||
## 1. Mount Avo within a `:locale` scope | ||
|
||
<!-- @include: ./../common/mount_avo_under_locale_scope_common.md--> | ||
|
||
## 2. Setup `default_url_options` | ||
|
||
To ensure that the `:locale` scope is consistently included in generated URLs, it must be explicitly added to Avo's `default_url_options` configuration. This step is vital for preserving the user's language preferences throughout the application. Without this configuration, the locale parameter might be omitted, leading to unexpected language switches or inconsistent localization behavior. | ||
|
||
Here's how you can set it up: | ||
|
||
```ruby{4} | ||
# config/avo.rb | ||
Avo.configure do |config| | ||
# Add :locale to default URL options | ||
config.default_url_options = [:locale] | ||
end | ||
``` | ||
|
||
This configuration guarantees that every URL generated by Avo includes the `:locale` parameter, aligning navigation with the user's language preferences. | ||
|
||
## 3. Apply the `locale` Scope | ||
|
||
To properly handle localization within Avo, you'll need to ensure the `locale` parameter is respected throughout the request. | ||
|
||
:::info | ||
If you've already ejected the `Avo::ApplicationController`, you can skip the ejection step below. | ||
::: | ||
|
||
### Eject the `Avo::ApplicationController` | ||
|
||
Run the following command to eject the `Avo::ApplicationController`: | ||
|
||
```bash | ||
rails generate avo:eject --controller application_controller | ||
``` | ||
|
||
This command generates a customizable version of the `Avo::ApplicationController`, allowing you to override default behaviors. | ||
|
||
### Override the `set_avo_locale` method | ||
|
||
Next, override the `set_avo_locale` method to ensure that the `locale` parameter is applied throughout the request lifecycle. Update your controller as follows: | ||
|
||
```ruby{5-7} | ||
# app/controllers/avo/application_controller.rb | ||
module Avo | ||
class ApplicationController < BaseApplicationController | ||
def set_avo_locale(&action) | ||
I18n.with_locale(params[:locale], &action) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
This implementation uses `I18n.with_locale` to set the desired locale for the duration of the request, ensuring consistent localization behavior across Avo's interface. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters