Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the localization concern, moving to around_action #133

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions rails_docker.rb
Expand Up @@ -65,7 +65,7 @@ def source_paths
<<~EOT

import 'translations/translations';

import 'initializers/';
import 'screens/';
EOT
Expand Down Expand Up @@ -175,14 +175,14 @@ def source_paths
insert_into_file 'config/webpack/environment.js', after: "const { environment } = require('@rails/webpacker')\n" do
<<~EOT
const webpack = require('webpack');

const plugins = [
new webpack.ProvidePlugin({
// Translations
I18n: 'i18n-js',
})
]

environment.config.set('plugins', plugins);
EOT
end
Expand All @@ -195,4 +195,18 @@ def source_paths

EOT
end

# I18n configuration
# Remove the incorrect fallback configuration (was generated by Rails) - https://github.com/ruby-i18n/i18n/releases/tag/v1.1.0
gsub_file('config/environments/production.rb', 'config.i18n.fallbacks = true', '')

# Adding the correct fallback configuration along with default locale and available locales
environment do
<<~EOT
config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(', ')
config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE')
config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(', ')

EOT
end
end
3 changes: 3 additions & 0 deletions rails_docker/Dockerfile.tt
Expand Up @@ -4,6 +4,9 @@ ARG RUBY_ENV=development
ARG NODE_ENV=development
ARG BUILD_ENV=development
ARG ASSET_HOST=http://localhost
ARG AVAILABLE_LOCALES
ARG DEFAULT_LOCALE
ARG FALLBACK_LOCALES

# Define all the envs here
ENV RACK_ENV=$RUBY_ENV \
Expand Down
3 changes: 3 additions & 0 deletions rails_docker/docker-compose.yml.tt
Expand Up @@ -25,6 +25,9 @@ services:
- NODE_ENV=production
- BUILD_ENV=production
- ASSET_HOST=
- AVAILABLE_LOCALES
- DEFAULT_LOCALE
- FALLBACK_LOCALES
image: ${DOCKER_IMAGE}:${BRANCH_TAG}
container_name: <%= APP_NAME %>_web
command: bin/start.sh
Expand Down
12 changes: 9 additions & 3 deletions shared/app/controllers/concerns/localization.rb
Expand Up @@ -2,12 +2,18 @@ module Localization
extend ActiveSupport::Concern

included do
before_action :set_locale
around_action :switch_locale

protected

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
def switch_locale(&action)
locale = extract_locale_from_param || I18n.default_locale

I18n.with_locale(locale, &action)
end

def extract_locale_from_param
I18n.locale_available?(params[:locale]) ? params[:locale].to_sym : nil
hoangmirs marked this conversation as resolved.
Show resolved Hide resolved
end

private
Expand Down
3 changes: 3 additions & 0 deletions shared/config/application.yml.tt
Expand Up @@ -6,6 +6,9 @@ default: &default
MAILER_DEFAULT_HOST: "localhost"
MAILER_DEFAULT_PORT: "3000"
MAILER_SENDER: "Test <noreply@nimblehq.co>"
AVAILABLE_LOCALES: "en"
DEFAULT_LOCALE: "en"
FALLBACK_LOCALES: "en"

development:
<<: *default
Expand Down