Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesyang124/75a69abd8aadc92e9ab8b069d3bab0de to your computer and use it in GitHub Desktop.
Save jamesyang124/75a69abd8aadc92e9ab8b069d3bab0de to your computer and use it in GitHub Desktop.
Adds Sprockets Directive `depend_on_config`. I'm using this to load .yml locale translations on the server into the client in json
=begin
Useage:
#assets/locales/en.js.erb
```js
//= depend_on_config 'locales/es.yml'
```
=end
class Sprockets::DirectiveProcessor
def process_depend_on_config_directive(file)
path = File.expand_path(file, "#{Rails.root}/config")
context.depend_on(path)
end
end
module JsLocaleHelper
def self.output_locale(locale)
locale_str = locale.to_s
file = Rails.root.join("config/locales/", "#{locale_str}.yml")
YAML::load(File.open file)[locale_str].to_json.html_safe
end
end
//= depend_on_config 'locales/es.yml'
I18n.translations = I18n.translations || {};
I18n.translations.en = "<%= JsLocaleHelper.output_locale(:en) %>"
en:
hello: Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment