Skip to content

Commit

Permalink
Merge pull request #63 from raouldevil/master
Browse files Browse the repository at this point in the history
Adding support to dump a single locale at a time.
  • Loading branch information
thomasdarde committed Apr 7, 2014
2 parents 06f99b3 + 0ec39a0 commit e8cf14a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ As tolk stores all the keys and translated strings in the database, you need to
```bash
$ rake tolk:sync
```

The above will fetch all the new keys from en.yml and put them in the database. Additionally, it'll also get rid of the deleted keys from the database and reflect updated translations - if any.

If you already have data in your non primary locale files, you will need to import those to Tolk as a one time thing :
Expand All @@ -42,6 +42,14 @@ If you already have data in your non primary locale files, you will need to impo

Upon visiting http://your_app.com/tolk - you will be presented with different options like creating new locale or providing translations for the existing locales. Once done with translating all the pending strings, you are can write back the new locales to filesystem :


```bash
$ rake tolk:dump_yml["the_target_locale"]
```

This command will generate a single yml file for a specified locale. The locale ISO code should be given in string format as the only argument ("en-us" or "en-gb" for example).


```bash
$ rake tolk:dump_all
```
Expand Down
4 changes: 4 additions & 0 deletions app/models/tolk/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def dump_all(*args)
secondary_locales.each { |locale| locale.dump(*args) }
end

def dump_yaml(name, *args)
find_by_name(name).dump(*args)
end

def special_key_or_prefix?(prefix, key)
self.special_prefixes.include?(prefix) || self.special_keys.include?(key)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/tolk_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace :tolk do
Tolk::Locale.dump_all
end

desc "Generate a single yml file for a specific locale"
task :dump_yaml, :locale => :environment do
Tolk::Locale.dump_yaml(locale)
end

desc "Imports data all non default locale yml files to Tolk"
task :import => :environment do
Rake::Task['tolk:sync'].invoke
Expand Down
26 changes: 23 additions & 3 deletions test/unit/sync_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def test_sync_sets_primary_updated_for_secondary_translations_on_update
assert t1.primary_updated?
assert ! t2.primary_updated?
end

def test_sync_marks_translations_for_review_when_the_primary_translation_has_changed
Tolk::Locale.create!(:name => 'es')

phrase = Tolk::Phrase.create! :key => 'number.precision'
english_translation = phrase.translations.create!(:text => "1", :locale => Tolk::Locale.find_by_name("en"))
spanish_translation = phrase.translations.create!(:text => "1", :locale => Tolk::Locale.find_by_name("es"))
Expand All @@ -74,7 +74,7 @@ def test_sync_marks_translations_for_review_when_the_primary_translation_has_cha
Tolk::Locale.expects(:load_translations).returns({'number.precision' => "2"}).at_least_once
Tolk::Locale.sync! and spanish_translation.reload
assert spanish_translation.out_of_date?

spanish_translation.text = "2"
spanish_translation.save! and spanish_translation.reload
assert spanish_translation.up_to_date?
Expand Down Expand Up @@ -204,4 +204,24 @@ def test_dump_all_after_sync
ensure
FileUtils.rm_f(tmpdir)
end

def test_dump_locale_after_sync
spanish = Tolk::Locale.create!(:name => 'es')

Tolk::Locale.sync!

phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

tmpdir = Rails.root.join("../../tmp/sync/locales")
FileUtils.mkdir_p(tmpdir)
Tolk::Locale.dump_yaml('es', tmpdir)

spanish_file = "#{tmpdir}/es.yml"
data = YAML::safe_load(IO.read(spanish_file))['es']
assert_equal ['hello_world'], data.keys
assert_equal 'hola', data['hello_world']
ensure
FileUtils.rm_f(tmpdir)
end
end

0 comments on commit e8cf14a

Please sign in to comment.