diff --git a/README.md b/README.md index 1c78195..f48a488 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ You can start adding new phrases by simply adding them in your view file: <%= phrase('my-first-phrase') %> +If you want the key not to be the default phrase, you can define one like here: + + <%= phrase('my-first-phrase',default: 'default phrase') %> + Aside from editing phrases (basically, Rails translations) you can also edit model attributes inline. Use the same `phrase` method, with the first attribute being the record in question, and the second one the attribute you wish to make editable: <%= phrase(@post, :title) %> diff --git a/app/helpers/inline_helper.rb b/app/helpers/inline_helper.rb index 1b489ae..2511c5c 100644 --- a/app/helpers/inline_helper.rb +++ b/app/helpers/inline_helper.rb @@ -32,7 +32,7 @@ def inline(record, attribute, options = {}) def phrasing_extract_record(key, options = {}) key = options[:scope] ? "#{options[:scope]}.#{key}" : key - PhrasingPhrase.find_phrase(key) + PhrasingPhrase.find_phrase(key,options[:default]) end def uneditable_phrase(record, attribute) diff --git a/app/models/phrasing_phrase.rb b/app/models/phrasing_phrase.rb index c3ec5b3..1b6feb5 100644 --- a/app/models/phrasing_phrase.rb +++ b/app/models/phrasing_phrase.rb @@ -8,8 +8,8 @@ class PhrasingPhrase < ActiveRecord::Base after_update :version_it - def self.find_phrase(key) - where(key: key, locale: I18n.locale.to_s).first || search_i18n_and_create_phrase(key) + def self.find_phrase(key,default=nil) + where(key: key, locale: I18n.locale.to_s).first || search_i18n_and_create_phrase(key,default) end def self.fuzzy_search(search_term, locale) @@ -28,20 +28,20 @@ def self.fuzzy_search(search_term, locale) private - def self.search_i18n_and_create_phrase(key) + def self.search_i18n_and_create_phrase(key,default=nil) begin value = I18n.t(key, raise: true) create_phrase(key, value) rescue I18n::MissingTranslationData - create_phrase(key) + create_phrase(key,nil,default) end end - def self.create_phrase(key, value=nil) + def self.create_phrase(key,value=nil, default=nil ) phrasing_phrase = PhrasingPhrase.new phrasing_phrase.locale = I18n.locale.to_s phrasing_phrase.key = key.to_s - phrasing_phrase.value = value || key.to_s + phrasing_phrase.value = value || default || key.to_s phrasing_phrase.save phrasing_phrase end