All Projects → abitdodgy → i18n_lazy_scope

abitdodgy / i18n_lazy_scope

Licence: MIT license
Use lazy lookup with custom i18n scopes.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to i18n lazy scope

lockup
Lockup Gem
Stars: ✭ 111 (+909.09%)
Mutual labels:  gem, ruby-on-rails
rails
Rails translation made _('simple').
Stars: ✭ 65 (+490.91%)
Mutual labels:  i18n, internationalization
laminas-i18n
Provide translations for your application, and filter and validate internationalized values
Stars: ✭ 40 (+263.64%)
Mutual labels:  i18n, internationalization
A18n
Automated I18n solution for JavaScript/TypeScript/React
Stars: ✭ 244 (+2118.18%)
Mutual labels:  i18n, internationalization
exception hunter
Crash reporting engine to hunt down bugs 🐞
Stars: ✭ 78 (+609.09%)
Mutual labels:  gem, ruby-on-rails
Flutter translate
Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
Stars: ✭ 245 (+2127.27%)
Mutual labels:  i18n, internationalization
react-mobx-router
Create React App with React Router 4 and MobX + Internationalization
Stars: ✭ 90 (+718.18%)
Mutual labels:  i18n, internationalization
Localeapp
Send and retrieve your ruby i18n localizations to the Locale translation service
Stars: ✭ 225 (+1945.45%)
Mutual labels:  i18n, gem
typesafe-i18n
A fully type-safe and lightweight internationalization library for all your TypeScript and JavaScript projects.
Stars: ✭ 1,227 (+11054.55%)
Mutual labels:  i18n, internationalization
awesome-i18n
🌍 A curated list of i18n resources for all kind of languages and frameworks
Stars: ✭ 205 (+1763.64%)
Mutual labels:  i18n, internationalization
L10ns
Internationalization workflow and formatting
Stars: ✭ 234 (+2027.27%)
Mutual labels:  i18n, internationalization
active record-updated at
Touch `updated_at` by default with calls to `update_all` and `update_column(s)`
Stars: ✭ 27 (+145.45%)
Mutual labels:  gem, ruby-on-rails
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (+2009.09%)
Mutual labels:  i18n, internationalization
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+2136.36%)
Mutual labels:  i18n, internationalization
Tourism Demo
Flutter app backed by Redux, shows animations, internationalization (i18n), ClipPath, fonts and others...
Stars: ✭ 232 (+2009.09%)
Mutual labels:  i18n, internationalization
ad localize
ADLocalize is a simple way to manage your localization files. Supported wording sources : CSVs and Google Sheets. Localization file generation available for iOS, Android, JSON (i18next), YAML and Java properties
Stars: ✭ 22 (+100%)
Mutual labels:  i18n, internationalization
Serge
Continuous localization platform
Stars: ✭ 212 (+1827.27%)
Mutual labels:  i18n, internationalization
Localer
Automatic detecting missing I18n translations tool.
Stars: ✭ 219 (+1890.91%)
Mutual labels:  i18n, gem
doorkeeper-i18n
Translation files for Doorkeeper OAuth 2 provider
Stars: ✭ 30 (+172.73%)
Mutual labels:  i18n, internationalization
msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (+63.64%)
Mutual labels:  i18n, internationalization

I18nLazyScope

I18nLazyScope is a small library that lets you use lazy lookup with custom scopes in your locale files.

It inserts a customisable namespace in the scope, just below the locale. The following table illustrates the difference between I18nLazyLookup and the default i18n-rails behaviour.

I18nLazyLookup Rails/I18n Lazy Lookup
Controllers locale.controllers.controller_name.action.key locale.controller_name.action.key
Mailers locale.mailers.mailer_name.action.key locale.mailer_name.action.key
Views locale.views.template_or_partial_path.key locale.template_or_partial_path.key

What's Lazy Lookup?

Lazy lookup is a feature built into the I18n gem. It allows you to use translations without explicitly qualifying their scope. For example, consider the following code that lives in app/views/users/show.html.erb.

<%= t('.welcome_message') %>

Rails automatically converts .welcome_message to en.users.show.welcome_message. This saves you from having to type users.show, which is handy if you have lots of translations. But it forces you to structure your locale files the way Rails prefers.

en:
  users:
    show:
      welcome_message: "You are such a star!"

Sometimes the above structure is not ideal. This is because en.users refers to both views and controllers. Other times it might be better to scope users under a namespace instead of having it under the top level namespace.

en:
  controllers:
    users:
      show:
        success: "Yay!"
  views:
    users:
      show:
        welcome_message: "Yay!"

Large applications with lots of translations might find it hard to take advantage of lazy lookup. Wouldn't it be great if you could use lazy lookup with a custom namespace? Well, now you can.

<%= t_scoped(:welcome_message) %>

t_scoped converts welcome_message to en.views.show.welcome_message, or whatever namespace you specified in your initializer. It's a wrapper method around Rails' translate and its alias t.

Installation

Add this line to your application's Gemfile:

gem 'i18n_lazy_scope'

And then execute:

$ bundle

Or install it yourself as:

$ gem install i18n_lazy_scope

Usage

Use t_scoped instead of t or translate, and make sure you add the corresponding keys in your locale files.

<!-- app/views/users/show.html.erb -->
<%= t_scoped :greeting %>
# config/locales/en.yml
en:
  views:
    users:
      show:
       greeting: "Hello!"

Customising the Namespace

By default, the library uses the following namespaces:

  1. Controllers: locale.controllers.controller_name.action_name.key
  2. Mailers: locale.mailers.mailer_name.action_name.key
  3. Views: locale.views.template_or_partial_path.key

Add an initializer with a configuration block to customise the namespaces.

# config/initializers/i18n_lazy_scope.rb
I18nLazyScope.configure do |config|
  # Resolves lazy lookup to `locale.custom.scope.controller_name.action_name.key`
  config.action_controller_scope = [:custom, :scope]
  # ... `locale.my.custom.scope.mailer_name.action_name.key`
  config.action_mailer_scope     = [:custom, :scope]
  # ... `locale.my.custom.scope.template_or_partial_path.key`
  config.action_view_scope       = [:custom, :scope]
end

Interpolation

Interpolation works exactly as it would if you call t or translate.

<%= t_scoped 'greeting', name: @user.name %>
en:
  views:
    users:
      show:
       greeting: "Hello, %{name}!"

A Note on Scoping

If you find that you have to customise the scope on an individual basis, you should use t and translate that ship with Rails or the I18n gem. Scoping on individual basis defeats the point of this gem.

API

t_scoped(key, **args)

Version

I18nLazyScope uses semantic versioning.

Contributing

  1. Fork it ( https://github.com/abitdodgy/i18n_lazy_scope/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].