Code Monkey home page Code Monkey logo

subdomain_locale's Introduction

Subdomain Locale (for Rails)

A multi-language website requires some way to detect a user's locale. Subdomain is one option.

Setup

Add the gem to your Gemfile:

gem "subdomain_locale"

Set a default subdomain:

# config/application.rb

# I18n library now recommends you to enforce available locales.
config.i18n.enforce_available_locales = true
config.i18n.available_locales = :en, :ru

# See "Configuration" for difference between these:
config.default_locale = :ru
config.i18n.default_locale = :en

Subdomains will now determine the current locale:

Add links to the new subdomains using :locale URL parameter:

<% [:ru, :en].each do |locale| %>
  <%= link_to locale, params.merge(locale: locale) %>
<% end %>

Configuration

You can hook a special subdomain name with a locale:

config.subdomain_locale["us"] = :"en-US" # us.lvh.me
config.subdomain_locale["ca"] = :"en-CA" # ca.lvh.me
config.subdomain_locale["ua"] = :uk      # ua.lvh.me

Default locale will link to the default subdomain (main domain by default: lvh.me). If you prefer "www" use this config option:

config.default_subdomain = "www"

English developers prefer to see English in console and other places. This is why we have a separate default locale for the website:

config.default_locale = :ru
config.i18n.default_locale = :en

For example, with this config example.com will be in Russian, while validation errors in console are still in English.

You can also override our controller method to completely ignore subdomain locale. For example, if you want admin panel to always be in English:

class AdminController
  # This is already an around_action
  def set_locale(&block)
    I18n.with_locale(:en, &block)
  end
end

Testing

This gem is tested against Rails 5.0 and 5.1.

gem install isolate
rake test:all

Changelog

1.3.0

  • Now testing against Rails 5.

1.2.0

  • Support Rails 4.2 and I18n 0.7; drop Rails 3.2.

  • Note: Rails 4.2 is more strict about _path/_url helpers. Make sure to use _url helper whenever you specify locale parameter:

    root_path(locale: :ru) # bad, will raise deprecation warning
    root_url(locale: :ru)  # good

1.1.0

  • Custom subdomain provided in your default_url_options now has precedence over the default subdomain-locale.

1.0.0

  • Links outside controllers now also point to the current locale. For example, in mailers.
  • Now compatible with the new I18n.enforce_available_locales.
  • No subdomain is now deafult instead of "www". Can be reverted by setting config.default_domain.
  • Separate website's default locale (config.default_locale) from the global default locale (config.i18n.default_locale).
  • Test gem in the whole Rails stack (3.2, 4.0).
  • Add config.subdomain_locale for indirect mapping ("us" => :"en-US").

0.1.1

  • Adding changelog to the readme.
  • Don't require files until they needed. That means less boot time impact.

0.1.0. Minor internal changes & fixes, documentation.

  • Fixing url_for's argument modified.
  • Adding readme and documentation.
  • Using require instead of autoload.
  • Refactoring tests.
  • Clearly specifying I18n dependency.

0.0.1โ€”0.0.3. Initial releases.

subdomain_locale's People

Contributors

nashbridges avatar semaperepelitsa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

subdomain_locale's Issues

Translated routes fails

Hey ho!

I'am very interested on your gem because I am do it that on my app.
The problem I have is when I switch the language on a page that has a URL that is different for every available locale. Is your gem implemented thinking in that point?

Could it be nice to have tha feature, right?

Thanks guys!

Rails 5 support

I see that this gem doesn't support rails 5. What stops it from doing so? Is there any way I can help you getting past that?

Thank you for considering that!

Proper urls in mailer views

Would be great if subdomain_locale can patch links in mailer as well without explicit setting locale: I18n.locale

Patch polymorphic_url too to add `locale` parameter

It would be good for consistency to patch polymorphic_url too to support locale parameter, like url_for. Something like:

    def polymorphic_url(record_or_hash_or_array, options = {})
      options = options.dup
      if options.key?(:locale)
        locale = options.delete(:locale)
        options[:subdomain] = subdomain_locales.subdomain_for(locale)
        options[:only_path] = false
      end
      super record_or_hash_or_array, options
    end

I don't know yet where to monkeypatch this method.

i18n dependency update needed

In Gemfile:
rails (= 4.2.1) ruby depends on
activejob (= 4.2.1) ruby depends on
activesupport (= 4.2.1) ruby depends on
i18n (~> 0.7) ruby

subdomain_locale (>= 0) ruby depends on
  i18n (~> 0.6.9) ruby

Rails 6 support

Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    rails (= 6.1.2) was resolved to 6.1.2, which depends on
      railties (= 6.1.2)

    subdomain_locale (~> 1.3) was resolved to 1.3.0, which depends on
      railties (>= 5.0, < 6.0)

Orphaned file links in the gemspec

subdomain_locale at ... did not have a valid gemspec.
This prevents bundler from installing bins or native extensions, but that may not affect its functionality.
The validation message from Rubygems was:
  ["test/controller_test.rb", "test/lib/i18n.rb", "test/acceptance_test.rb"] are not files

Maybe git ls-files is less error-prone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.