Skip to content

Instantly share code, notes, and snippets.

@excid3
Created February 28, 2020 04:13
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 excid3/4c349d13f13737c1bdef8ef4548e3ab4 to your computer and use it in GitHub Desktop.
Save excid3/4c349d13f13737c1bdef8ef4548e3ab4 to your computer and use it in GitHub Desktop.
require 'yaml'
class LocaleGroup
attr_reader :output
def initialize
@output = {}
@output.default_proc = -> (h, k) { h[k] = Hash.new(&h.default_proc) }
end
def to_yaml
output.to_yaml
end
def nest(key, value)
array = key.split(".")
output.dig(*array[0..-2])[array.fetch(-1)] = value
end
end
locales = LocaleGroup.new
locales.nest("en.whatever.devise.login.welcome", "Hey")
locales.nest("en.whatever.devise.login.hello", "Hello")
locales.nest("en.yolo", "Dolo")
puts locales.to_yaml
---
en:
whatever:
devise:
login:
welcome: Hey
hello: Hello
yolo: Dolo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment