Skip to content

Instantly share code, notes, and snippets.

@mrship
Created April 17, 2012 09:13
Show Gist options
  • Save mrship/2404790 to your computer and use it in GitHub Desktop.
Save mrship/2404790 to your computer and use it in GitHub Desktop.
SimpleForm hacks for Phrase
# original SimpleForm code (lib/simple_form/inputs/base.rb:142)
def translate(namespace, default='')
model_names = lookup_model_names.dup
lookups = []
while !model_names.empty?
joined_model_names = model_names.join(".")
model_names.shift
lookups << :"#{joined_model_names}.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"#{joined_model_names}.#{reflection_or_attribute_name}"
end
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"defaults.#{attribute_name}"
lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
end
# my hack to wrap the I18n.t call with the phrase_translate call
if Rails.env.staging?
module SimpleForm
module Inputs
class Base
include Phrase::Backend::Base
def translate(namespace, default='')
model_names = lookup_model_names.dup
lookups = []
while !model_names.empty?
joined_model_names = model_names.join(".")
model_names.shift
# lookups << :"#{joined_model_names}.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"#{joined_model_names}.#{reflection_or_attribute_name}"
end
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"defaults.#{attribute_name}"
lookups << default
phrase_translate(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups).presence
end
def phrase_translate(*args)
key = lookup_normalized_key(*args)
case key[:translation]
when String, nil, "" then decorate_translation(key[:key])
else key[:translation]
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment