Giter Club home page Giter Club logo

gettext_i18n_rails_js's Introduction

GettextI18nRailsJs

Gem Version Build Status Code Climate Test Coverage Dependency Status

Extends gettext_i18n_rails, making your .PO files available to client side javascript as JSON. It will find translations inside your .js, .coffee, .handlebars and .mustache files, then it will create JSON versions of your .PO files so you can serve them with the rest of your assets, thus letting you access all your translations offline from client side javascript.

Versions

This gem is tested on the following versions, it's also possible that it works with older versions, but because of version bumps at gettext_i18n_rails and fast_gettext we have dropped the older versions from the testing matrix:

  • Ruby
    • 2.1.0
    • 2.2.0
  • Rails
    • 3.2.21
    • 4.0.13
    • 4.1.16
    • 4.2.7

Installation

gem "gettext_i18n_rails_js", "~> 1.2"

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency 'gettext_i18n_rails_js', '~> 1.2'

Usage

set up you rails application with gettext support as usual, afterwards just execute the following rake task to export your translations to JSON:

rake gettext:po_to_json

Per default this will reconstruct the locale/<lang>/app.po structure as javascript files inside app/assets/javascripts/locale/<lang>/app.js

The gem provides the Jed library to use the generated javascript files. It also provides a global __ function that maps to Jed#gettext. The Jed instance used by the client side __ function is pre-configured with the lang attribute specified in your main HTML tag. Before anything, make sure your page's HTML tag includes a valid lang attribute, for example:

%html{ manifest: "", lang: I18n.locale }

Once you're sure your page is configured with a locale, then you should add both your javascript locale files and the provided javascripts to your application.js

//= require_tree ./locale
//= require gettext/all

The default function name is window.__, to avoid conflicts with underscore.js. If you want to alias the function to something else in your javascript you should also instruct the javascript and coffeescript parser to look for a different function when finding your translations within the config file config/gettext_i18n_rails_js.yml, these are valid available options:

output_path: "app/assets/javascripts/locale"
handlebars_function: "__"
javascript_function: "__"
jed_options:
  pretty: false

If you prefer an initializer file within your rails application you can use that in favor of the YML configuration as well:

GettextI18nRailsJs.config do |config|
  config.output_path = "app/assets/javascripts/locale"

  config.handlebars_function = "__"
  config.javascript_function = "__"

  config.jed_options = {
    pretty: false
  }
end

Todo

  • More deep testing against multiple Rails versions
  • Extend the current test suite, especially handlebars

Contributing

Fork -> Patch -> Spec -> Push -> Pull Request

Authors

License

MIT

Copyright

Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
Copyright (c) 2015 Webhippie <http://www.webhippie.de>

gettext_i18n_rails_js's People

Contributors

nubis avatar tboerger avatar lidaobing avatar mikezaby avatar serco-chen avatar artemv avatar akirill0v avatar juanboca avatar pravi avatar vilius avatar bradbarrow avatar

Watchers

delxen 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.