i18n_screwdriver 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "actionpack", ">= 3.0.0"
4
+
5
+ group :development do
6
+ gem "shoulda", ">= 0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.5.1"
9
+ gem "rcov", ">= 0"
10
+ end
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.3)
6
+ activemodel (= 3.0.3)
7
+ activesupport (= 3.0.3)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.4)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.13)
13
+ rack-test (~> 0.5.6)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.3)
16
+ activesupport (= 3.0.3)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.4)
19
+ activesupport (3.0.3)
20
+ builder (2.1.2)
21
+ erubis (2.6.6)
22
+ abstract (>= 1.0.0)
23
+ git (1.2.5)
24
+ i18n (0.4.2)
25
+ jeweler (1.5.1)
26
+ bundler (~> 1.0.0)
27
+ git (>= 1.2.5)
28
+ rake
29
+ rack (1.2.1)
30
+ rack-mount (0.6.13)
31
+ rack (>= 1.0.0)
32
+ rack-test (0.5.6)
33
+ rack (>= 1.0)
34
+ rake (0.8.7)
35
+ rcov (0.9.9)
36
+ shoulda (2.11.3)
37
+ tzinfo (0.3.23)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ actionpack (>= 3.0.0)
44
+ bundler (~> 1.0.0)
45
+ jeweler (~> 1.5.1)
46
+ rcov
47
+ shoulda
@@ -0,0 +1,52 @@
1
+ = i18n_screwdriver
2
+
3
+ Translating applications is often a pain in the ass. The problem with rails i18n
4
+ is that you have to use keys for every string to translate.
5
+
6
+ That is one too many abstraction layers - I love the gettext syntax but I want to
7
+ stick to the rails standard libraries. Therefor I created this small toolbox.
8
+
9
+ == Usage
10
+
11
+ Set these constants to make i18n screwdriver aware of the languages you are using
12
+
13
+ APPLICATION_LANGUAGES = ['de', 'en, 'fr', 'it']
14
+ DEFAULT_LANGUAGE = 'de'
15
+
16
+ In your views, helpers and controllers use the convenient underscore helper method for all your translations
17
+
18
+ _("your translation")
19
+
20
+ When you are done you have 2 helper rake tasks. The first one scans all your views, controllers and
21
+ helpers for translation. It removes unused translation strings and creates an application.<lang>.yml file
22
+ for each of your APPLICATION_LANGUAGES.
23
+
24
+ rake i18n:update
25
+
26
+ The seconds one lets you translate your application.<lang>.yml file line by line. Of course
27
+ you can edit the application.<lang>.yml file itself - but dont do that for your default language file.
28
+ It gets recreated everytime you run the tasks - edit those translations in your views e.g. itself.
29
+
30
+ TRANSLATE=en rake i18n:translate
31
+
32
+
33
+ == Next steps
34
+
35
+ * DRY the code
36
+ * test more
37
+ * use ruby_parser instead of regex
38
+ * support interpolation
39
+
40
+ == Contributing to i18n_screwdriver
41
+
42
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
43
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
44
+ * Fork the project
45
+ * Start a feature/bugfix branch
46
+ * Commit and push until you are happy with your contribution
47
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
48
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
49
+
50
+ == Copyright
51
+
52
+ Copyright (c) 2010 Tobias Miesel. Released unter the MIT license.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "i18n_screwdriver"
8
+ gem.homepage = "http://github.com/agileapplications/i18n_screwdriver"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{make translating with rails i18n fun again}
11
+ gem.email = "agileapplications@gmail.com"
12
+ gem.authors = ["Tobias Miesel"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.add_dependency "actionpack", ">= 3.0.0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :test => :check_dependencies
36
+ task :default => :test
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "i18n_screwdriver #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{i18n_screwdriver}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Miesel"]
12
+ s.date = %q{2010-11-19}
13
+ s.email = %q{agileapplications@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".bundle/config",
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "i18n_screwdriver.gemspec",
25
+ "lib/i18n_screwdriver.rb",
26
+ "tasks/i18n.rake",
27
+ "test/helper.rb",
28
+ "test/test_i18n_screwdriver.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/agileapplications/i18n_screwdriver}
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.7}
34
+ s.summary = %q{make translating with rails i18n fun again}
35
+ s.test_files = [
36
+ "test/helper.rb",
37
+ "test/test_i18n_screwdriver.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
46
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
49
+ s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
52
+ else
53
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
57
+ s.add_dependency(%q<rcov>, [">= 0"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
63
+ s.add_dependency(%q<shoulda>, [">= 0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ s.add_dependency(%q<shoulda>, [">= 0"])
68
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
69
+ end
70
+ end
71
+
@@ -0,0 +1,22 @@
1
+ module I18n
2
+ module Screwdriver
3
+ def self.included(klass)
4
+ klass.send :include, InstanceMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ def _(translation)
9
+ # the . is a special character in rails i18n - we have to strip it
10
+ translation_without_dot = translation.gsub(/\./, '').strip
11
+ t("#{translation_without_dot}")
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActionController::Base.send :include, I18n::Screwdriver
18
+ ActionView::Helpers::TranslationHelper.send :include, I18n::Screwdriver
19
+ # make helper available in tests
20
+ ActionController::TestCase.send :include, I18n::Screwdriver
21
+ ActiveSupport::TestCase.send :include, I18n::Screwdriver
22
+ ActionController::IntegrationTest.send :include, I18n::Screwdriver
@@ -0,0 +1,99 @@
1
+ namespace :i18n do
2
+ task :update => :environment do
3
+
4
+ translations = {}
5
+ raise "ERROR: APPLICATION_LANGUAGES = ['de', 'en', ...] is not defined - define it in your initializers" unless defined? APPLICATION_LANGUAGES
6
+ raise "ERROR: DEFAULT_LANGUAGE = '??' is not defined - define it in your initializers" unless defined? DEFAULT_LANGUAGE
7
+
8
+ # parse all view files for translations - removed ones get removed from
9
+ # the i18n translation yml's automatically
10
+ Dir.glob("**/*.{haml,erb}").each do |file|
11
+ scan_for_translations(file, translations)
12
+ end
13
+
14
+ # also check helper files and controllers
15
+ Dir.glob("**/*{_helper,_controller}.rb").each do |file|
16
+ scan_for_translations(file, translations)
17
+ end
18
+
19
+ # rewrite default language file with updated translations
20
+ puts "found #{translations.keys.size} unique translations"
21
+ write_translation_file(DEFAULT_LANGUAGE, translations)
22
+
23
+ # write a file for each foreign language
24
+ (APPLICATION_LANGUAGES - [DEFAULT_LANGUAGE]).each do |language|
25
+ # load foreign language file in temp hash
26
+ if File.exists?("config/locales/application.#{language}.yml")
27
+ existing_translations = YAML.load_file("config/locales/application.#{language}.yml")
28
+ existing_translations = existing_translations[language]
29
+ else
30
+ existing_translations = { language => {} }
31
+ end
32
+
33
+ # delete removed translations from hash
34
+ existing_translations.each_key do |key|
35
+ existing_translations.delete(key) unless translations.has_key?(key)
36
+ end
37
+
38
+ # add new keys with empty translation to hash
39
+ translations.each_key do |key|
40
+ existing_translations[key] = "TRANSLATION_MISSING: #{key}" unless existing_translations.has_key?(key)
41
+ end
42
+ write_translation_file(language, existing_translations)
43
+ end
44
+ end
45
+
46
+ task :translate => :environment do
47
+ raise "ERROR: usage: TRANSLATE=de rake i18n:translate" unless ENV['TRANSLATE']
48
+ language = ENV['TRANSLATE']
49
+ if File.exists?("config/locales/application.#{language}.yml")
50
+ existing_translations = YAML.load_file("config/locales/application.#{language}.yml")
51
+ existing_translations = existing_translations[language]
52
+ puts "Translating #{existing_translations.keys.size} entries to <#{language}> (enter :q to save and quit):"
53
+ existing_translations.keys.sort.each do |key|
54
+ if existing_translations[key] == "TRANSLATION_MISSING"
55
+ puts "> #{key}"
56
+ input = STDIN.gets.chomp
57
+ if input == ":q"
58
+ break
59
+ else
60
+ existing_translations[key] =
61
+ end
62
+ end
63
+ end
64
+
65
+ puts "Schreiben der Datei config/locales/application.#{language}.yml"
66
+ write_translation_file(language, existing_translations)
67
+ else
68
+ puts "ERROR: No translation file in config/locales/application.#{language}.yml"
69
+ puts "Choosen language is invalid or you did not run rake i18n:update yet"
70
+ end
71
+ end
72
+
73
+ def write_translation_file(language, translations)
74
+ file = File.open("config/locales/application.#{language}.yml", "w")
75
+ file.puts "# use rake task i18n:update to generate this file"
76
+ file.puts
77
+ file.puts "\"#{language}\":"
78
+ translations.keys.sort do |a, b|
79
+ a.downcase <=> b.downcase
80
+ end.each do |key|
81
+ file.puts " \"#{key}\": \"#{translations[key]}\""
82
+ end
83
+ file.close
84
+ end
85
+
86
+ def scan_for_translations(file, translations)
87
+ File.open(file) do |io|
88
+ io.each do |line|
89
+ if line =~ /_\("([^"]+)"\)/
90
+ value = $1
91
+ translations[$1.gsub(/\./, '').strip] = value.strip
92
+ elsif line =~ /_\('([^']+)'\)/
93
+ value = $1
94
+ translations[$1.gsub(/\./, '').strip] = value.strip
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'action_controller'
13
+ require 'action_view'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'i18n_screwdriver'
18
+
19
+ class Test::Unit::TestCase
20
+ include I18n::Screwdriver
21
+ include ActionView::Helpers
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestI18nScrewdriver < Test::Unit::TestCase
4
+ should "remove dots from translation string" do
5
+ assert_equal "<span class=\"translation_missing\">en, mynewtranslation</span>", _("my.new.translation")
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_screwdriver
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tobias Miesel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-19 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: actionpack
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ prerelease: false
40
+ name: shoulda
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: bundler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: jeweler
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 1
77
+ segments:
78
+ - 1
79
+ - 5
80
+ - 1
81
+ version: 1.5.1
82
+ requirement: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ type: :development
85
+ prerelease: false
86
+ name: rcov
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirement: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ type: :development
99
+ prerelease: false
100
+ name: shoulda
101
+ version_requirements: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirement: *id006
111
+ - !ruby/object:Gem::Dependency
112
+ type: :runtime
113
+ prerelease: false
114
+ name: actionpack
115
+ version_requirements: &id007 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 7
121
+ segments:
122
+ - 3
123
+ - 0
124
+ - 0
125
+ version: 3.0.0
126
+ requirement: *id007
127
+ description:
128
+ email: agileapplications@gmail.com
129
+ executables: []
130
+
131
+ extensions: []
132
+
133
+ extra_rdoc_files:
134
+ - README.rdoc
135
+ files:
136
+ - .bundle/config
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - README.rdoc
140
+ - Rakefile
141
+ - VERSION
142
+ - i18n_screwdriver.gemspec
143
+ - lib/i18n_screwdriver.rb
144
+ - tasks/i18n.rake
145
+ - test/helper.rb
146
+ - test/test_i18n_screwdriver.rb
147
+ has_rdoc: true
148
+ homepage: http://github.com/agileapplications/i18n_screwdriver
149
+ licenses:
150
+ - MIT
151
+ post_install_message:
152
+ rdoc_options: []
153
+
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ hash: 3
162
+ segments:
163
+ - 0
164
+ version: "0"
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: 3
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ requirements: []
175
+
176
+ rubyforge_project:
177
+ rubygems_version: 1.3.7
178
+ signing_key:
179
+ specification_version: 3
180
+ summary: make translating with rails i18n fun again
181
+ test_files:
182
+ - test/helper.rb
183
+ - test/test_i18n_screwdriver.rb