From 551229fd96b38a2b8978128992fe978d5150e7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Kru=CC=88ger?= Date: Wed, 25 Nov 2020 15:00:44 +0100 Subject: [PATCH 1/4] added default value for phrases --- app/helpers/inline_helper.rb | 2 +- app/models/phrasing_phrase.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) 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..c3c2b97 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='empty' ) 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.to_s phrasing_phrase.save phrasing_phrase end From 6f86e8fdcf0ac1be4080d6e0f9713b1d89005f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Kru=CC=88ger?= Date: Wed, 25 Nov 2020 15:03:59 +0100 Subject: [PATCH 2/4] now chooses key, if default empty --- app/models/phrasing_phrase.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/phrasing_phrase.rb b/app/models/phrasing_phrase.rb index c3c2b97..87c5704 100644 --- a/app/models/phrasing_phrase.rb +++ b/app/models/phrasing_phrase.rb @@ -41,7 +41,7 @@ def self.create_phrase(key,value=nil, default='empty' ) phrasing_phrase = PhrasingPhrase.new phrasing_phrase.locale = I18n.locale.to_s phrasing_phrase.key = key.to_s - phrasing_phrase.value = value || default.to_s + phrasing_phrase.value = value || default.to_s || key.to_s phrasing_phrase.save phrasing_phrase end From e7202887f2174b07dc624d3d9786459e1fad4af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Kru=CC=88ger?= Date: Wed, 25 Nov 2020 15:07:15 +0100 Subject: [PATCH 3/4] trying a fix --- app/models/phrasing_phrase.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/phrasing_phrase.rb b/app/models/phrasing_phrase.rb index 87c5704..1b6feb5 100644 --- a/app/models/phrasing_phrase.rb +++ b/app/models/phrasing_phrase.rb @@ -37,11 +37,11 @@ def self.search_i18n_and_create_phrase(key,default=nil) end end - def self.create_phrase(key,value=nil, default='empty' ) + 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 || default.to_s || key.to_s + phrasing_phrase.value = value || default || key.to_s phrasing_phrase.save phrasing_phrase end From 73ad43e05ed6c320b8a51f711b4eabf9bb57bf99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Kru=CC=88ger?= Date: Wed, 25 Nov 2020 15:15:13 +0100 Subject: [PATCH 4/4] edited readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) 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) %>