Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/inline_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions app/models/phrasing_phrase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down