Skip to content

Commit

Permalink
allow default translations in pl.yml, de.yml, etc. that are read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertlepicki committed Mar 10, 2011
1 parent 60089d5 commit 3080fbc
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'rake/gempackagetask'
require 'rspec/core'
require 'rspec/core/rake_task'

Rspec::Core::RakeTask.new(:spec)
RSpec::Core::RakeTask.new(:spec)

task :default => :spec

Expand Down
7 changes: 5 additions & 2 deletions app/helpers/go_translate_yourself/site_translations_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module GoTranslateYourself
module SiteTranslationsHelper
def translation_text_field(locale, key, default_translation)
options = { :size => (default_translation.size + 20),
:id => "translations_#{locale}_#{key}".gsub(/\./, '_') }

options[:disabled] = "disabled" if GoTranslateYourself.current_store.default_translation?("#{locale}.#{key}")
text_field_tag("translations[#{locale}.#{key}]",
ActiveSupport::JSON.decode(GoTranslateYourself.current_store["#{locale}.#{key}"] || ""),
:size => (default_translation.size + 20),
:id => "translations_#{locale}_#{key}".gsub(/\./, '_'))
options)
end
end
end
31 changes: 19 additions & 12 deletions app/modles/go_translate_yourself/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@ module GoTranslateYourself
class BaseStore

def keys
if @dev_translations.nil? || Rails.env.development?
load_dev_translations
if @default_translations.nil? || Rails.env.development?
load_default_translations

@keys = GoTranslateYourself.locales.collect {|lang| keys_without_prefix.collect {|key| "#{lang}.#{key}"} }.flatten
end

@keys
@keys
end

def default_translation(key)
load_dev_translations unless @dev_translations
@dev_translations[key.to_s.gsub(/^[a-z]*\./, "")]
load_default_translations unless @default_translations
@default_translations[key] || @default_translations[key.to_s.gsub(/^[a-z]*\./, "dev.")]
end

def keys_without_prefix
load_dev_translations unless @dev_translations
@dev_translations.keys
def default_translation?(key)
!@default_translations[key].nil?
end

def keys_without_prefix
load_default_translations unless @default_translations
@default_translations.keys.map {|k| k.sub(/^[a-z]*\./, "") }.uniq
end

protected

def load_dev_translations
dev_translations = YAML.load_file(File.join(Rails.root, "config", "locales", "dev.yml"))
@dev_translations = {}
flatten_keys(nil, dev_translations["dev"], @dev_translations)
def load_default_translations
@default_translations = {}
Dir.glob(File.join(Rails.root, "config", "locales", "*.yml")).each do |locale_file|
translations = YAML.load_file(locale_file)
code = File.basename(locale_file).sub(".yml", "")
flatten_keys(code, translations[code], @default_translations)
end
end

def flatten_keys(current_key, hash, dest_hash)
Expand Down
2 changes: 1 addition & 1 deletion app/modles/go_translate_yourself/mongo_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def []=(key, value)
end

def [](key)
load_dev_translations if Rails.env.development?
load_default_translations if Rails.env.development?
if document = collection.find_one(:_id => key)
document["value"]
else
Expand Down
5 changes: 5 additions & 0 deletions spec/acceptance/translations_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
visit homepage("cy")
page.should have_content("Hello world!")
end

scenario "Seeing blocked translations" do
visit translate
page.should have_css("input[disabled='disabled']")
end
end

3 changes: 3 additions & 0 deletions spec/dummy/config/locales/de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
de:
errors:
required: "erforderlich ist"
5 changes: 4 additions & 1 deletion spec/dummy/config/locales/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ dev:
hello: "Hello world!"
site:
name: "Some random web site"

errors:
required: "go ahead, enter something"
blank: "come on, enter something"

4 changes: 4 additions & 0 deletions spec/dummy/config/locales/pl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pl:
errors:
blank: "nie może być puste"
required: "jest wymagany"
7 changes: 7 additions & 0 deletions spec/unit/mongo_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
@store["pl.hello"] = "Witaj, świec{{elo}}ie!"
@store["pl.hello"].should eql("\"Witaj, \\u015bwiec{{elo}}ie!\"")
end

it "should load read-only translations from all *.yml files in config directory, other than dev.yml" do
@store["pl.errors.blank"].should == "\"nie mo\\u017ce by\\u0107 puste\""
@store["pl.errors.required"].should == "\"jest wymagany\""
@store["de.errors.required"].should == "\"erforderlich ist\""
@store["de.errors.blank"].should == "\"come on, enter something\""
end
end

0 comments on commit 3080fbc

Please sign in to comment.