Skip to content

Instantly share code, notes, and snippets.

@dylanjha
Last active May 11, 2020 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dylanjha/11233766 to your computer and use it in GitHub Desktop.
Save dylanjha/11233766 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
Copy link

ghost commented Oct 6, 2015

Which version of sprockets are you using? This seems to blow up on sprockets 3.4.

undefined local variable or method context' for #Sprockets::DirectiveProcessor:0x007fe27be37280`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment