diff --git a/app/assets/stylesheets/copycat_engine.css b/app/assets/stylesheets/copycat_engine.css index 55a1849..e5772b2 100644 --- a/app/assets/stylesheets/copycat_engine.css +++ b/app/assets/stylesheets/copycat_engine.css @@ -400,6 +400,19 @@ div.edit h2 { text-overflow: ellipsis; } +div.edit .variables table { +width: auto; +} + + +div.edit .variables table th { + font-weight: bold; +} +div.edit .variables table td, +div.edit .variables table th { +padding: 1em; +border: 1px dotted; +} div.edit textarea { width: 99%; margin-top: 1em; @@ -461,3 +474,6 @@ body { margin-bottom: .5em; } +.variables { + clear: both; +} diff --git a/app/models/copycat_translation.rb b/app/models/copycat_translation.rb index 3c34b1e..78738f9 100644 --- a/app/models/copycat_translation.rb +++ b/app/models/copycat_translation.rb @@ -6,17 +6,24 @@ class CopycatTranslation < ActiveRecord::Base validates :key, :presence => true validates :locale, :presence => true - - attr_accessible :locale, :key, :value + + attr_accessible :locale, :key, :value, :options + serialize :options, Hash + RESERVED_KEYS = [:scope, :default, :separator, :resolve, :rescue_format] + + def user_parameters + # RESERVED_KEYS from i18n gem + self.options.dup.delete_if {|k| RESERVED_KEYS.include? k} + end module Serialize - + def import_yaml(yaml) hash = YAML.load(yaml) hash.each do |locale, data| hash_flatten(data).each do |key, value| c = where(key: key, locale: locale).first - c ||= new(key: key, locale: locale) + c ||= new(key: key, locale: locale) c.value = value c.save end @@ -31,14 +38,14 @@ def export_yaml end hash.to_yaml end - + # {"foo"=>{"a"=>"1", "b"=>"2"}} ----> {"foo.a"=>1, "foo.b"=>2} def hash_flatten(hash) - result = {} + result = {} hash.each do |key, value| - if value.is_a? Hash + if value.is_a? Hash hash_flatten(value).each { |k,v| result["#{key}.#{k}"] = v } - else + else result[key] = value end end diff --git a/app/views/copycat_translations/edit.html.erb b/app/views/copycat_translations/edit.html.erb index fc4ddd4..1aa583f 100644 --- a/app/views/copycat_translations/edit.html.erb +++ b/app/views/copycat_translations/edit.html.erb @@ -1,6 +1,30 @@
-

<%= @copycat_translation.key%>

+

<%= @copycat_translation.key %>

<%= button_to "Delete this item", copycat_translation_path(@copycat_translation), :method => :delete, :onclick => "return confirm('Are you sure?');" %> + <%- if @copycat_translation.user_parameters.present? %> +
+

Variables

+

You could use the following variables in your inserted text, just using %{variable} and you will obtain its value

+ + + + + + + + + + <%- @copycat_translation.user_parameters.each do |key, value| %> + + + + + + <% end %> + +
VariableValueUsage
<%= key %><%= value %><%= "%{#{key}}" %>
+
+ <% end %> <%= form_for @copycat_translation, :url => { :action => "update" } do |f| %> <%= f.text_area :value, :rows => 12 %> <%= f.submit "Update" %> diff --git a/db/migrate/20120313191745_create_copycat_translations.rb b/db/migrate/20120313191745_create_copycat_translations.rb index f5b00d3..9004e1d 100644 --- a/db/migrate/20120313191745_create_copycat_translations.rb +++ b/db/migrate/20120313191745_create_copycat_translations.rb @@ -4,6 +4,7 @@ def up t.string :locale t.string :key t.text :value + t.text :options t.timestamps end change_table :copycat_translations do |t| diff --git a/lib/copycat/implementation.rb b/lib/copycat/implementation.rb index 48a43da..f32dc1a 100644 --- a/lib/copycat/implementation.rb +++ b/lib/copycat/implementation.rb @@ -7,7 +7,7 @@ def lookup(locale, key, scope = [], options = {}) return cct.value if cct value = super(locale, key, scope, options) if value.is_a?(String) || value.nil? - CopycatTranslation.create(locale: locale.to_s, key: key.to_s, value: value) + CopycatTranslation.create(locale: locale.to_s, key: key.to_s, value: value, options: options) end value end