i18n-export 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ require File.expand_path("i18n_export/config", File.dirname(__FILE__))
2
+ require "active_support"
3
+
4
+ module I18nExport
5
+
6
+ def self.config_file=(file)
7
+ @@config_file = file
8
+ end
9
+
10
+ def self.config_file
11
+ @@config_file ||= "config/i18n-export.yml"
12
+ end
13
+
14
+ def self.configuration
15
+ I18nExport::Config.new(config_file)
16
+ end
17
+
18
+ def self.export!
19
+ configuration.file_definitions.each {|definition| export_file(definition) }
20
+ end
21
+
22
+ private
23
+ def self.export_file(definition)
24
+ open(definition.filename, "w+") do |f|
25
+ f.print "var I18n = I18n || {};\n"
26
+ f.print "I18n.translations = "
27
+ f.print ActiveSupport::JSON.encode(generate_output(definition.filters))
28
+ f.print ";"
29
+ end
30
+ end
31
+
32
+ def self.generate_output(filters)
33
+ result = {}
34
+
35
+ I18n.available_locales.each do |locale|
36
+ if filters.nil? or filters.empty?
37
+ result[locale] = I18n.t('.', :locale => locale)
38
+ else
39
+ result[locale] ||= {}
40
+
41
+ filters.each do |filter|
42
+ insert_data(I18n.t(filter, :locale => locale), filter.split("."), result[locale])
43
+ end
44
+ end
45
+ end
46
+
47
+ result
48
+ end
49
+
50
+ def self.insert_data(data, path, result)
51
+ component = path.shift
52
+
53
+ if path.empty?
54
+ result[component] = data
55
+ else
56
+ result[component] ||= {}
57
+ insert_data(data, path, result[component])
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+
64
+ require File.expand_path("i18n_export/railtie", File.dirname(__FILE__)) if defined?(Rails) and Rails.version >= "3.0.0"
@@ -0,0 +1,27 @@
1
+ require 'yaml'
2
+
3
+ module I18nExport
4
+ class Config
5
+ class FileDefinition < Struct.new(:filename, :filters)
6
+ end
7
+
8
+ def initialize(filename=I18nExport.config_file)
9
+ # lets make sure the file exists
10
+ raise Errno::ENOENT unless File.file?(filename)
11
+
12
+ @filename = filename
13
+ end
14
+
15
+ def file_definitions
16
+ (config["translations"] || []).map do |defn|
17
+ FileDefinition.new(defn["file"], defn["only"])
18
+ end
19
+ end
20
+
21
+ private
22
+ def config
23
+ @config ||= YAML.load_file(@filename)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails'
2
+
3
+ module I18nExport
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/i18n_export.rake"
7
+ end
8
+
9
+ # Used for development
10
+ config.to_prepare do
11
+ I18nExport.export!
12
+ end
13
+
14
+ # Used for production
15
+ config.after_initialize do
16
+ I18nExport.export!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+
2
+ module I18nExport
3
+ module Version
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ PATCH = 0
7
+ BUILD = nil
8
+
9
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ puts "rake task loading!!!!"
2
+
3
+ namespace :i18n do
4
+ desc "exports all i18n translations according to configuration"
5
+ task :export do
6
+ I18nExport.export!
7
+ end
8
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+
4
+ require File.expand_path("../lib/i18n_export.rb", File.dirname(__FILE__))
5
+
6
+ class Test::Unit::TestCase
7
+
8
+ def setup
9
+ # assign the configuration file
10
+ I18nExport.config_file = fixture_filename("config.yml")
11
+ end
12
+
13
+ def teardown
14
+ # remove temp files from the test/tmp directory
15
+ Dir.glob(tempfile("*")).each do |filename|
16
+ File.delete(filename)
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def fixture(file)
23
+ File.read(fixture_filename(file))
24
+ end
25
+
26
+ def fixture_filename(file)
27
+ File.expand_path("fixture/#{file}", File.dirname(__FILE__))
28
+ end
29
+
30
+ def tempfile(filename)
31
+ File.expand_path("tmp/#{filename}", File.dirname(__FILE__))
32
+ end
33
+
34
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('helper', File.dirname(__FILE__))
2
+
3
+ class TestConfiguration < Test::Unit::TestCase
4
+
5
+ def test_configured_filename
6
+ I18nExport.config_file = nil # defaults to config/i18n-export.yml
7
+
8
+ assert_raise(Errno::ENOENT) { I18nExport::Config.new }
9
+
10
+ I18nExport.config_file = fixture_filename("config.yml")
11
+
12
+ assert_nothing_raised { I18nExport::Config.new }
13
+ end
14
+
15
+ def test_manually_configured_filename
16
+ I18nExport.config_file = nil
17
+
18
+ assert_raise(Errno::ENOENT) { I18nExport::Config.new }
19
+ assert_nothing_raised { I18nExport::Config.new(fixture_filename "config.yml") }
20
+ end
21
+
22
+ def test_values
23
+ config = I18nExport::Config.new
24
+
25
+ assert_equal 3, config.file_definitions.length
26
+
27
+ assert_equal "test/tmp/app.js", config.file_definitions.first.filename
28
+ assert_equal ["app"], config.file_definitions.first.filters
29
+ end
30
+
31
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path('helper', File.dirname(__FILE__))
2
+ require 'yaml'
3
+
4
+ class TestExport < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+
9
+ I18n.available_locales = nil
10
+
11
+ translations = YAML::load_file(fixture_filename("translations.yml"))
12
+
13
+ translations.each do |locale, translations|
14
+ I18n.backend.store_translations(locale, translations)
15
+ end
16
+ end
17
+
18
+ def teardown
19
+ super
20
+ I18n.available_locales = nil
21
+ end
22
+
23
+ def test_configuration
24
+ assert I18nExport.configuration.is_a?(I18nExport::Config)
25
+ end
26
+
27
+ def test_file_created
28
+ assert_equal false, File.file?(tempfile("app.js"))
29
+
30
+ I18nExport.export!
31
+
32
+ assert_equal true, File.file?(tempfile("app.js"))
33
+ end
34
+
35
+ def test_full_export
36
+ I18nExport.export!
37
+
38
+ assert_equal fixture("all.js"), File.read(tempfile "all.js")
39
+ end
40
+
41
+ def test_partial_export
42
+ I18nExport.export!
43
+
44
+ assert_equal fixture("app.js"), File.read(tempfile "app.js")
45
+ assert_equal fixture("client.js"), File.read(tempfile "client.js")
46
+ end
47
+
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-export
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - William Howard
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-04 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: i18n
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 1
44
+ segments:
45
+ - 3
46
+ - 0
47
+ - 3
48
+ version: 3.0.3
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rake
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: test-unit
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ description: Provides exporting functionality for subtrees of I18n translations. Includes Railtie for Rails 3 integration.
80
+ email:
81
+ - whoward.tke@gmail.com
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files: []
87
+
88
+ files:
89
+ - lib/i18n_export.rb
90
+ - lib/i18n_export/railtie.rb
91
+ - lib/i18n_export/config.rb
92
+ - lib/i18n_export/version.rb
93
+ - lib/tasks/i18n_export.rake
94
+ - test/test_configuration.rb
95
+ - test/test_export.rb
96
+ - test/helper.rb
97
+ has_rdoc: true
98
+ homepage: http://github.com/whoward/i18n-export
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.6.1
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Export I18n translations in multiple formats
131
+ test_files:
132
+ - test/test_configuration.rb
133
+ - test/test_export.rb
134
+ - test/helper.rb