Created
February 28, 2020 04:13
-
-
Save excid3/4c349d13f13737c1bdef8ef4548e3ab4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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