Giter Club home page Giter Club logo

rails-asset-localization's Introduction

RailsAssetLocalization

Gem Version Build Status

A Rails Engine that allows you to use i18next with the asset pipeline. Locales are served dynamically to allow easy integration with services like CopyCopter.

Basic Setup

Add this to your Gemfile:

gem 'rails-asset-localization', git: '[email protected]:nicolai86/rails-asset-localization.git', branch: :master

and this to your routes:

mount RailsAssetLocalization::Engine => "/locales"

Your locales are now available under /locales/:locale

Now you need to load i18next:

# inside your application.coffee
#= require i18next.min # or i18next for development version

and properly configure it to work with your rails locales:

# inside your app startup code
locale = "de"
i18n.init({
  # change default interpolation from __VARIABLE__ to rails-style %{VARIABLE}
  interpolationPrefix: '%{'
  interpolationSuffix: '}'

  # current locale to load
  lng: locale

  # rails-asset-localization path
  resGetPath: '/locales/%{lng}.json'

  # store locales for 1 day in localStorage
  useLocalStorage: true
  localStorageExpirationTime: 60 * 60 * 24 * 1000
})

Now you can use it everywhere in your asset pipeline - see i18next dokumentation for details.

Advanced Setup: precompiled assets

You might want to serve a static version of your assets to enable users to access your localization without incurring additional network requests.

First, load your latest locales into the asset pipeline:

# inside your application.coffee
#= require i18next.min
#= require i18n/translations

Next, instruct i18next to store the bundled locales locally

for bundledLocale of bundledLocales
  storedLocale = window.localStorage.getItem("res_#{bundledLocale}")?
  unless storedLocale?
    object = {}
    object[bundledLocale] = bundledLocales[bundledLocale]
    i18n.sync._storeLocal object

Lastly, initialize i18next like described above

This way your locales are instantly accessible. The next time the i18next updates all locales (your cached locales need to be older than localStorageExpirationTime ms) the locales are updated and voilà. Your user sees new content.

Advanced Setup: HandlebarsAsset integration

If you are using @leshill awesome handlebars_assets you might want to use a handlebars helper function to use it inside your views. This will get you started:

Handlebars.registerHelper 't', ->
  args = [].slice.call(arguments)
  result = i18n.t.apply(i18n,args)
  return new Handlebars.SafeString(result)

# inside your views

{{t "app.hello", name: "Max Mustermann"}} # => Hello %{name} -> Hello Max Mustermann

rails-asset-localization's People

Contributors

mozzan avatar nicolai86 avatar

Watchers

 avatar

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.