grosser-gettext_test_log 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,56 @@
1
+ Problem
2
+ =======
3
+ - Finding all translations by parsing is impossible for dynamic calls
4
+ - often even static calls are not found
5
+ - some file formats do not have a gettext parser
6
+
7
+ Solution
8
+ ========
9
+ - log all translations during test execution
10
+ - write them to an parseable temp file
11
+ - use gettext:find / gettext:pack as usual
12
+ - missing translation = missing test, write tests!
13
+
14
+ Usage
15
+ =====
16
+ Create a rake task:
17
+ #lib/tasks/gettext_test_log.rake
18
+ desc "write all msgids used into testlog_phrases.rb"
19
+ task :gettext_test_log => :environment do
20
+ #place the file should somewhere your updatepo is searching
21
+ GettextTestLog::write_test_log("locale/testlog_phrases.rb",
22
+ :exclude_msgids_in_po_files=>[
23
+ 'some_po_file_that_contains_translations_you_do_not_need.po'
24
+ ]
25
+ )
26
+ end
27
+
28
+ Add to your spec_helper:
29
+ #spec/spec_helper.rb
30
+ GettextTestLog::activate_test_logging
31
+
32
+ Run:
33
+ rake gettext_test_log
34
+ rake gettext:find #find new translations
35
+ #fill newly found translations
36
+ rake gettext:pack #write translations
37
+
38
+ Install
39
+ =======
40
+ - As Rails plugin: `script/plugin install git://github.com/grosser/gettext_test_log.git `
41
+ - As gem: `sudo gem install grosser-gettext_test_log -s http://gems.github.com/`
42
+
43
+ Examples output
44
+ ===============
45
+ #locale/testlog_phrases.rb
46
+ _("Add to favorites")
47
+ _("Added a subtitle to %{name}")
48
+ _("Additional Information")
49
+ _("Address")
50
+ ...
51
+
52
+ Author
53
+ ======
54
+ Michael Grosser
55
+ grosser.michael@gmail.com
56
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,69 @@
1
+ module GettextTestLog
2
+ extend self
3
+ SEPERATOR = "\nNEXT-WORD\n"
4
+
5
+ # write to a given file, which msgids where found
6
+ # in the `rake updatepo` recogniseable format _("msgid")
7
+ def write_test_log(storage_file,options={})
8
+ messages = messages_from_test_run
9
+ if excluded = options[:exclude_msgids_in_po_files]
10
+ messages -= msgids_in_po_files(excluded)
11
+ end
12
+ store_messages(messages,storage_file)
13
+ end
14
+
15
+ # call this inside your spec_helper.rb
16
+ # to activate test message storage
17
+ def activate_test_logging
18
+ if ENV['LOG_GETTEXT']
19
+ require 'gettext_test_log/fast_gettext_stub'
20
+ Spec::Runner.configure do |config|
21
+ #TODO only call after the 'last' test was executed
22
+ config.before(:all) do
23
+ File.open(ENV['LOG_GETTEXT'],'w') do |f|
24
+ f.puts FastGettext::Translation::TRANSLATIONS_USED * SEPERATOR
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def messages_from_test_run
34
+ logfile = 'tmp_gettext_test_log.txt'
35
+ run_tests(logfile)
36
+ ids = used_msgids(logfile)
37
+ `rm #{logfile}`
38
+ ids
39
+ end
40
+
41
+ def used_msgids(logfile)
42
+ File.read(logfile).split(/#{SEPERATOR}/m).map(&:strip).reject(&:blank?).uniq
43
+ end
44
+
45
+ def run_tests(logfile)
46
+ ENV['LOG_GETTEXT']=logfile
47
+ Rake::Task["spec:models"].invoke rescue nil
48
+ end
49
+
50
+ def msgids_in_po_files(files)
51
+ [*files].map{|file| messages_from_po_file(file).keys}.flatten
52
+ end
53
+
54
+ #returns a hash of all msgid => msgstr
55
+ def messages_from_po_file(file)
56
+ require 'gettext'
57
+ require 'gettext/poparser'
58
+ require 'gettext/mofile'
59
+ data = MOFile.new
60
+ GetText::PoParser.new.parse(File.read(file),data)
61
+ data
62
+ end
63
+
64
+ def store_messages(messages,file)
65
+ File.open(file,'w') do |f|
66
+ f.puts messages.uniq.sort.map{|message| %Q[_("#{message}")].gsub("\n","\\n")} * "\n"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,15 @@
1
+ module FastGettext::Translation
2
+ TRANSLATIONS_USED = []
3
+
4
+ def __with_log(x)
5
+ TRANSLATIONS_USED << x
6
+ __without_log(x)
7
+ end
8
+ alias_method_chain :_, :log
9
+
10
+ def s__with_log(x)
11
+ TRANSLATIONS_USED << x
12
+ s__without_log(x)
13
+ end
14
+ alias_method_chain :s_, :log
15
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
2
+
3
+ describe GettextTestLog do
4
+ end
@@ -0,0 +1,32 @@
1
+ # ---- requirements
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
7
+ require 'gettext_test_log'
8
+
9
+ # ---- rspec
10
+ Spec::Runner.configure do |config|
11
+ config.mock_with :mocha
12
+ end
13
+
14
+
15
+ # ---- bugfix
16
+ #`exit?': undefined method `run?' for Test::Unit:Module (NoMethodError)
17
+ #can be solved with require test/unit but this will result in extra test-output
18
+ module Test
19
+ module Unit
20
+ def self.run?
21
+ true
22
+ end
23
+ end
24
+ end
25
+
26
+
27
+ # ---- Helpers
28
+ def pending_it(text,&block)
29
+ it text do
30
+ pending(&block)
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grosser-gettext_test_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: grosser.michael@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - VERSION.yml
27
+ - lib/gettext_test_log
28
+ - lib/gettext_test_log.rb
29
+ - lib/gettext_test_log/fast_gettext_stub.rb
30
+ - spec/gettext_test_log_spec.rb
31
+ - spec/spec_helper.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/grosser/gettext_test_log
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Logs all gettext translations during test execution, to a GetText readable format
59
+ test_files: []
60
+