i18n_googledocs 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,37 @@
1
+ ##
2
+
3
+ ### 0.1.3
4
+
5
+ 2012-05-02 @kristianmandrup
6
+
7
+ Various fixes. Renamed gem.
8
+
9
+ ### 0.1.1
10
+
11
+ 2012-04-23 @kristianmandrup
12
+
13
+ - Fixed compatibility with Ruby 1.8.7
14
+ - Added import, export and missing_keys generator for i18n-docs generator namespace
15
+ - Allow export generator to use any locale as base (I18n.default_locale is now the default!)
16
+ - Fixed export bug when locale is 'no' for norway ('no' is treated as false value)
17
+ - Added 'locales' generator to generate base locale files
18
+ - Use I18n.default_locale as the default master locale for all generators
19
+ - Added 'normalize' generator to normalize tabs to spaces
20
+ - Added 'copy_master' generator to use one locale translation as base for other locales
21
+ - Added :normalize parameter on export to allow pre-normalization before export :)
22
+ - Added :output-dir to export and allow ~ as HOME alias
23
+ - Added :locales option to export to control which locales are exported
24
+ - Added :auto-translate option to export and copy_master, using Google Translate API. Note: Requires key registration at Google (not free).
25
+
26
+ ### 0.0.4
27
+
28
+ Update docs, license. Push to Rubygems.org.
29
+
30
+ ### 0.0.3
31
+
32
+ * Open sourced: changed name and description.
33
+
34
+ ### 0.0.2
35
+
36
+ * removed loading of `awesome_print` from the rake task. It was breaking
37
+ download
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source :rubygems
2
+
3
+ gem 'deep_merge'
4
+
5
+ group :test do
6
+ gem 'to_lang'
7
+
8
+ # other Translator APIs or adapters
9
+ # gem 'google-translate'
10
+ # gem 'bing_translator'
11
+ end
12
+
13
+ group :development do
14
+ gem "rspec", ">= 2.8.0"
15
+ gem "rdoc", ">= 3.12"
16
+ gem "bundler", ">= 1.0.0"
17
+ gem "jeweler", ">= 1.8.3"
18
+ gem "simplecov",">= 0.5"
19
+ gem 'rake'
20
+ gem 'mocha'
21
+ end
22
+
23
+
24
+ group :test, :development do
25
+ gem 'awesome_print'
26
+ end
27
+
data/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2012 Local.ch AG
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
data/README.md ADDED
@@ -0,0 +1,173 @@
1
+
2
+ # i18n-docs
3
+
4
+ **i18n-docs** is a ruby gem that helps you to keep translations stored in Google Docs. This makes it easier for translators and project members to coordinate changes and updates. In addition, the standard features of Google Docs are super handy for this process: revision tracking, authorization, publishing, etc.
5
+
6
+ Although we at use it with Google Docs, it could be used with any CSV file.
7
+
8
+ *this gem is currently in use and tested with Rails 3.1. It probably works with other 3.x versions, but probably not 2.x at the moment.*
9
+
10
+ ## Features:
11
+
12
+ * download translations from multiple Google spreadsheets and store to YAML files
13
+ * import/export YAML files with a Google Spreadsheet
14
+ * check YAML files for missing translations (`rake i18n:find_missing_keys`)
15
+
16
+ ## Usage
17
+
18
+ ### Configuration
19
+
20
+ Add the GEM to your Rails project:
21
+
22
+ gem 'i18n-docs'
23
+
24
+ Create a configuration file in `config/translations.yml`:
25
+
26
+ files:
27
+ pre_signup.yml: "https://docs.google.com/spreadsheet/pub?key=ab43...34f3&single=true&gid=0&output=csv"
28
+ navigation.yml: "https://docs.google.com/spreadsheet/pub?key=0Ap...XveWc&single=true&gid=0&output=csv"
29
+ ... etc ...
30
+
31
+ Make the directories and files to hold the translations:
32
+
33
+ ```
34
+ $ rails g i18n_docs:locales da se no
35
+ create config/locales/da/your_combination.yml.yml
36
+ create config/locales/da/pre_signup.yml.yml
37
+ create config/locales/da/navigation.yml.yml
38
+ create config/locales/da/extras.yml.yml
39
+ create config/locales/da/your_team.yml.yml
40
+ create config/locales/se/your_combination.yml.yml
41
+ create config/locales/se/pre_signup.yml.yml
42
+ create config/locales/se/navigation.yml.yml
43
+ create config/locales/se/extras.yml.yml
44
+ create config/locales/se/your_team.yml.yml
45
+ create config/locales/no/your_combination.yml.yml
46
+ create config/locales/no/pre_signup.yml.yml
47
+ create config/locales/no/navigation.yml.yml
48
+ create config/locales/no/extras.yml.yml
49
+ create config/locales/no/your_team.yml.yml
50
+ ```
51
+
52
+ Finally, let Rails know what locales you will be using. Add this to `config/application.rb`:
53
+
54
+ # locales to support:
55
+ I18n.available_locales = [:en,:de,:it,:fr]
56
+
57
+ This defines which languages and translation files to import from a Google Spreadsheet. The content of the Spreadsheet URL is stored to a file called e.g. `example1.yml` within folders `config/locales/en` and all other detected locales.
58
+
59
+ ## Generators
60
+
61
+ ## Translation APIs
62
+
63
+ The built in (optional) auto-translation currently only supports Google Translation API.
64
+
65
+ Please help us add Bing or some other translation API support :)
66
+
67
+ ### Normalizer
68
+
69
+ Normalizes yml files, converting tabs to 2 spaces pr. default
70
+ This is useful before export in order to better ensure that yaml files have valid syntax.
71
+
72
+ `$ rails g i18n_docs:normalize da`
73
+
74
+ By default, the normalized files are prefixed with '_'. In order to overwrite existing files, use the overwrite option.
75
+
76
+ `$ rails g i18n_docs:normalize da --overwrite true`
77
+
78
+ In addition you can use the space option to customize spaces pr tab.
79
+
80
+ `$ rails g i18n_docs:normalize da se --spaces 4`
81
+
82
+ If you want to clean up and remove the normalized files, use the delete option
83
+
84
+ `$ rails g i18n_docs:normalize da se --delete`
85
+
86
+ If you want to accept all normalized files prefixed with '_'
87
+
88
+ `$ rails g i18n_docs:normalize da se --accept`
89
+
90
+ ## Copy master
91
+
92
+ Use one locale as master and copy into other locales while changing the root key :)
93
+
94
+ `$ rails g i18n_docs:copy_master da --into se no`
95
+
96
+ Auto translate from the master locale file to each locale generated.
97
+
98
+ `$ rails g i18n_docs:copy_master da --into sv --auto-translate`
99
+
100
+ ### Export
101
+
102
+ Export locale files as CSV files to upload to Google Docs
103
+
104
+ `$ rails g i18n_docs:export`
105
+
106
+ Use specific (here danish) locale as the master locale
107
+
108
+ `$ rails g i18n_docs:export da`
109
+
110
+ Auto translate from the master locale file to each csv generated.
111
+
112
+ `$ rails g i18n_docs:export da --auto-translate`
113
+
114
+ It also supports a locales option to control for which locales to export.
115
+ The normalize option can be used to pre-normalize before export using the normalize generator.
116
+
117
+ `$ rails g i18n_docs:export da --locales sv --auto-translate --normalize`
118
+
119
+ Finally the outpur-dir option can be used to control where to export the cvs files. If you use the ~ (HOME alias) it will substitute with ENV['HOME'] and work as expected ;)
120
+
121
+ `$ rails g i18n_docs:export da --locales sv --output-dir "~/Documents/csv"`
122
+
123
+ ### Find missing keys
124
+
125
+ Find and list translation keys that do not exist in all locales
126
+
127
+ `$ rails g i18n_docs:missing_keys`
128
+
129
+ ### Import
130
+
131
+ Import csv files from Google Docs as locale files
132
+
133
+ `$ rails g i18n_docs:import`
134
+
135
+ ### Rake Tasks
136
+
137
+ Following Rake tasks are added by the GEM to your Rails project:
138
+
139
+ * `rake i18n:export_translations`
140
+ Export all language files to CSV files (only files contained in 'en' folder are considered)
141
+ * `rake i18n:export_translations[da]` (override default: only files in 'da' folder)
142
+
143
+ * `rake i18n:import_translations`
144
+ Download translations from Google Spreadsheet and save them to YAML files.
145
+ * `rake i18n:missing_keys`
146
+ Find and list translation keys that do not exist in all locales
147
+
148
+
149
+ ## Setup at Google Docs
150
+
151
+ * At Google Docs, create a new spreadsheet for your translations. The first column is the translation key, the other columns are the languages:
152
+ ![screenshot](http://dl.dropbox.com/u/385855/Screenshots/bamr.png)
153
+ * use option `File -> Publish To the Web`
154
+ ![screenshot](http://dl.dropbox.com/u/385855/Screenshots/2-lp.png)
155
+ * use **get a link** to generate a unique url that points to the CSV spreadsheet
156
+ ![screenshot](http://dl.dropbox.com/u/385855/Screenshots/oom_.png)
157
+ * From now on you should only update translations in Google Docs and run `rake i18n:import_translations` in the application to get changes. You can also export your
158
+
159
+ ## Todo
160
+
161
+ *Pull requests welcome!*
162
+
163
+ * Create mocked tests that do not have to download our sample CSV files
164
+ * Support more Translation APIs besides Google, fx Bing etc.
165
+
166
+ ## CHANGELOG
167
+
168
+ See CHANGELOG file
169
+
170
+ ### Credits/License
171
+
172
+ This gem is sponsored by [local.ch](http://www.local.ch/). It is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License). If you're a ruby developer and want to work with us in Switzerland, please check out our [jobs page](http://local-ch.github.com/).
173
+
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docgem.rubygemgem.org/read/chapter/20 for more options
17
+ gem.name = "i18n_googledocs"
18
+ gem.homepage = "http://github.com/kristianmandrup/i18n-docs"
19
+ gem.summary = "Maintain translations in Google Docs and export them to your Rails project."
20
+ gem.description = "GEM providing helper scripts to manage i18n translations in Google Docgem. Features: check YAML files for missing translations; export YAML files to CSV; download translations from multiple Google spreadsheets and store to YAML files"
21
+ gem.authors = ["Georg Kunz", "Ivan Jovanovic", "Jeremy Seitz", "Kristian Mandrup"]
22
+ gem.email = 'jeremy.seitz@local.ch'
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.4
@@ -0,0 +1,95 @@
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 = "i18n_googledocs"
8
+ s.version = "0.1.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Georg Kunz", "Ivan Jovanovic", "Jeremy Seitz", "Kristian Mandrup"]
12
+ s.date = "2012-05-03"
13
+ s.description = "GEM providing helper scripts to manage i18n translations in Google Docgem. Features: check YAML files for missing translations; export YAML files to CSV; download translations from multiple Google spreadsheets and store to YAML files"
14
+ s.email = "jeremy.seitz@local.ch"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.md",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "CHANGELOG",
21
+ "Gemfile",
22
+ "LICENSE.md",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "i18n_googledocs.gemspec",
27
+ "lib/generators/i18n_docs/copy_master_generator.rb",
28
+ "lib/generators/i18n_docs/export_generator.rb",
29
+ "lib/generators/i18n_docs/import_generator.rb",
30
+ "lib/generators/i18n_docs/locales_generator.rb",
31
+ "lib/generators/i18n_docs/missing_keys_generator.rb",
32
+ "lib/generators/i18n_docs/normalize_generator.rb",
33
+ "lib/generators/i18n_docs/templates/locale.erb",
34
+ "lib/i18n_googledocs.rb",
35
+ "lib/localch_i18n/csv_to_yaml.rb",
36
+ "lib/localch_i18n/missing_keys_finder.rb",
37
+ "lib/localch_i18n/translation_file_export.rb",
38
+ "lib/localch_i18n/translations.rb",
39
+ "lib/localch_i18n/translator.rb",
40
+ "lib/localch_i18n/translator/service.rb",
41
+ "lib/localch_i18n/util.rb",
42
+ "lib/tasks/store_translations.rake",
43
+ "tasks/test.rake",
44
+ "test/fixtures/config.yml",
45
+ "test/fixtures/de/header.yml",
46
+ "test/fixtures/en/header.yml",
47
+ "test/fixtures/minimal.csv",
48
+ "test/fixtures/test.csv",
49
+ "test/test_helper.rb",
50
+ "test/unit/csv_to_yaml_test.rb",
51
+ "test/unit/translation_file_export_test.rb",
52
+ "test/unit/translations_test.rb"
53
+ ]
54
+ s.homepage = "http://github.com/kristianmandrup/i18n-docs"
55
+ s.require_paths = ["lib"]
56
+ s.rubygems_version = "1.8.24"
57
+ s.summary = "Maintain translations in Google Docs and export them to your Rails project."
58
+
59
+ if s.respond_to? :specification_version then
60
+ s.specification_version = 3
61
+
62
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
63
+ s.add_runtime_dependency(%q<deep_merge>, [">= 0"])
64
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
65
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
66
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
67
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
68
+ s.add_development_dependency(%q<simplecov>, [">= 0.5"])
69
+ s.add_development_dependency(%q<rake>, [">= 0"])
70
+ s.add_development_dependency(%q<mocha>, [">= 0"])
71
+ s.add_development_dependency(%q<awesome_print>, [">= 0"])
72
+ else
73
+ s.add_dependency(%q<deep_merge>, [">= 0"])
74
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
75
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
76
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
77
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
78
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
79
+ s.add_dependency(%q<rake>, [">= 0"])
80
+ s.add_dependency(%q<mocha>, [">= 0"])
81
+ s.add_dependency(%q<awesome_print>, [">= 0"])
82
+ end
83
+ else
84
+ s.add_dependency(%q<deep_merge>, [">= 0"])
85
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
86
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
87
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
88
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
89
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
90
+ s.add_dependency(%q<rake>, [">= 0"])
91
+ s.add_dependency(%q<mocha>, [">= 0"])
92
+ s.add_dependency(%q<awesome_print>, [">= 0"])
93
+ end
94
+ end
95
+
@@ -0,0 +1,118 @@
1
+ require 'localch_i18n/util'
2
+
3
+ module I18nDocs
4
+ module Generators
5
+ class CopyMasterGenerator < Rails::Generators::Base
6
+ include LocalchI18n::Util
7
+
8
+ desc "Copy master locale files to other locales to be used as base"
9
+
10
+ class_option :into, :type => :array, :default => [],
11
+ :desc => "Which locales to copy master files into"
12
+
13
+ class_option :auto_translate, :type => :boolean, :default => false,
14
+ :desc => "Use Google Translate API to auto-translate?"
15
+
16
+ argument :master, :type => :string, :default => nil,
17
+ :desc => "Master locale, default = I18n.default_locale"
18
+
19
+ def main_flow
20
+ check_translate_key if options[:auto_translate]
21
+ copy_master master_locale
22
+ end
23
+
24
+ protected
25
+
26
+ # TODO: refactor this when/if we support other translation services
27
+ def check_translate_key
28
+ unless I18n.google_translation_key
29
+ say %q{You must set I18n.google_translation_key to a valid API key.
30
+ See https://code.google.com/apis/console/b/0/?api=translate}, :red
31
+ exit(0)
32
+ end
33
+
34
+ say "Using google translation key: #{I18n.google_translation_key}"
35
+ end
36
+
37
+ def master_locale
38
+ master || I18n.default_locale || :en
39
+ end
40
+
41
+ def locales
42
+ @locales ||= options[:into].empty? ? I18n.available_locales : options[:into]
43
+ end
44
+
45
+ def unnormalized_files path
46
+ Dir[File.join(path,'[^_]*.yml')]
47
+ end
48
+
49
+ def locales_root
50
+ Rails.root.join 'config', 'locales'
51
+ end
52
+
53
+ def locale_names
54
+ locales.join(' ')
55
+ end
56
+
57
+ def master_locale
58
+ master || I18n.default_locale || :en
59
+ end
60
+
61
+ def make_local_copy file, locale
62
+ local_content = content.sub /#{master_locale}:/, "#{locale}:"
63
+ file = file.gsub /\\/, '/'
64
+ local_file = file.gsub /\/#{master_locale}\//, "\/#{locale}\/"
65
+
66
+ File.open(local_file, 'w') do |f|
67
+ f.puts local_content
68
+ end
69
+
70
+ # auto translate local file just written
71
+ auto_translate_for(local_file, locale) if auto_translate?
72
+ end
73
+
74
+ attr_accessor :content
75
+ attr_reader :current_locale
76
+
77
+ # override method in Util module to use options
78
+ def auto_translate?
79
+ options[:auto_translate]
80
+ end
81
+
82
+ # flatten_translations_hash has already done the translation
83
+ # if auto_translate? returned true
84
+ def auto_translate_for(local_file, locale)
85
+ @current_locale = locale
86
+ flat_hash = flatten_translations_hash load_translations_for(local_file, locale)
87
+
88
+ translated_hash = {}
89
+ flat_hash.each do |key, text|
90
+ translation = text
91
+ trans_row_hash = row_to_hash key, translation
92
+ translated_hash.deep_merge!(trans_row_hash)
93
+ end
94
+ translated_hash
95
+
96
+ # write the translated file
97
+ File.open(local_file, 'w') do |file|
98
+ final_translation_hash = {locale => translated_hash}
99
+ file.puts YAML::dump(final_translation_hash)
100
+ end
101
+ end
102
+
103
+ def locale_path locale
104
+ (locale != :all) ? File.join(locales_root, locale) : locales_root
105
+ end
106
+
107
+ def copy_master locale = :en
108
+ path = locale_path(locale)
109
+ say "Using #{locale} as master for locales: #{locale_names}"
110
+ unnormalized_files(path).each do |file|
111
+ say "Making local copies for: #{file}"
112
+ self.content = File.open(file).read
113
+ locales.each{|locale| make_local_copy file, locale }
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,92 @@
1
+ module I18nDocs
2
+ module Generators
3
+ class ExportGenerator < Rails::Generators::Base
4
+ desc "Export locale files as CSV files"
5
+
6
+ argument :master, :type => :string, :default => nil, :desc => "Master locale, default = I18n.default_locale"
7
+
8
+ class_option :normalize, :type => :boolean, :default => false,
9
+ :desc => "Normalize locale files (with overwrite) before export?"
10
+
11
+ class_option :locales, :type => :array, :default => [],
12
+ :desc => "locales to export for, default = all"
13
+
14
+ class_option :output_dir, :type => :string, :default => nil,
15
+ :desc => "Output directory"
16
+
17
+ def main_flow
18
+ generate "i18n_docs:normalize #{locale_names} --overwrite" if normalize?
19
+ show_files
20
+ export_files
21
+ end
22
+
23
+ protected
24
+
25
+ def normalize?
26
+ options[:normalize]
27
+ end
28
+
29
+ def locale_names
30
+ locales.join(' ')
31
+ end
32
+
33
+ def show_files
34
+ say ""
35
+ say " Valid locales to export: #{export_locales}", :green
36
+ say " Detected files:"
37
+ input_files.each {|f| say " * #{File.basename(f)}", :green }
38
+ end
39
+
40
+ def export_files
41
+ say ""
42
+ say " Start exporting files:", :green
43
+ say ""
44
+ say " source dir: #{source_dir}"
45
+ say " output dir: #{output_dir}"
46
+
47
+ input_files.each do |file|
48
+ file = File.basename(file)
49
+ exporter = LocalchI18n::TranslationFileExport.new(source_dir, file, output_dir, locales_to_export)
50
+ exporter.export
51
+ end
52
+
53
+ say ""
54
+ say " CSV files can be removed safely after uploading them manually to Google Spreadsheet."
55
+ say ""
56
+ end
57
+
58
+
59
+ def source_dir
60
+ Rails.root.join('config', 'locales')
61
+ end
62
+
63
+ def output_dir
64
+ @output_dir ||= begin
65
+ odir = options[:output_dir] || Rails.root.join('tmp')
66
+ odir = odir.gsub(/~/, ENV['HOME']) if odir =~ /~/
67
+ odir
68
+ end
69
+ end
70
+
71
+ def locales_to_export
72
+ export_locales.empty? ? I18n.available_locales : export_locales
73
+ end
74
+
75
+ def export_locales
76
+ locales.map(&:to_sym) & I18n.available_locales.map(&:to_sym)
77
+ end
78
+
79
+ def locales
80
+ options[:locales]
81
+ end
82
+
83
+ def master_locale
84
+ master || I18n.default_locale || :en
85
+ end
86
+
87
+ def input_files
88
+ Dir[File.join(source_dir, master_locale.to_s, '*.yml')]
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,30 @@
1
+ module I18nDocs
2
+ module Generators
3
+ class ImportGenerator < Rails::Generators::Base
4
+ desc "Import locale files from CSV files"
5
+
6
+ def main_flow
7
+ find_translations_file
8
+ import
9
+ end
10
+
11
+ protected
12
+
13
+ def find_translations_file
14
+ config_file = Rails.root.join('config', 'translations.yml')
15
+ raise "No config file 'config/translations.yml' found." if !File.exists?(config_file)
16
+ end
17
+
18
+ def import
19
+ translations = LocalchI18n::Translations.new(config_file, tmp_dir)
20
+ translations.download_files
21
+ translations.store_translations
22
+ translations.clean_up
23
+ end
24
+
25
+ def tmp_dir
26
+ Rails.root.join('tmp')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,59 @@
1
+ # for lang in de en it fr; do \
2
+ # mkdir -p config/locales/$lang; \
3
+ # touch config/locales/$lang/navigation.yml; \
4
+ # touch config/locales/$lang/forms.yml; \
5
+ # done
6
+ module I18nDocs
7
+ module Generators
8
+ class LocalesGenerator < Rails::Generators::Base
9
+ desc "Creates Locale files for I18n-docs"
10
+
11
+ argument :locales, :type => :array, :desc => "Locale codes"
12
+
13
+ source_root File.dirname(__FILE__) + '/templates'
14
+
15
+ def main_flow
16
+ locales.each do |locale|
17
+ self.locale = locale
18
+ exec_template
19
+ end
20
+ end
21
+
22
+ def instructions
23
+ say "----------------------------------"
24
+ say "Add this to config/application.rb:"
25
+ say "config.i18n.available_locales = #{available_locales}", :green
26
+ end
27
+
28
+ protected
29
+
30
+ def available_locales
31
+ locales.map(&:to_sym).inspect
32
+ end
33
+
34
+ def files_src_path
35
+ File.join Rails.root, 'config', 'translations.yml'
36
+ end
37
+
38
+ def yaml
39
+ say("Missing #{files_src_path}", :red) and exit(0) unless File.exist?(files_src_path)
40
+ YAML::load File.open(files_src_path)
41
+ end
42
+
43
+ def files
44
+ yaml['files'].keys
45
+ end
46
+
47
+ attr_accessor :locale, :file, :key, :text
48
+
49
+ def exec_template
50
+ files.each do |file|
51
+ self.file = file
52
+ self.key = file.gsub /\..+$/, ''
53
+ self.text = options[:text] || "Hello from #{locale} locale"
54
+ template "locale.erb", "config/locales/#{locale}/#{file}"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ module I18nDocs
2
+ module Generators
3
+ class MissingKeysGenerator < Rails::Generators::Base
4
+ desc "Show missing keys"
5
+
6
+ def main
7
+ finder = LocalchI18n::MissingKeysFinder.new(I18n.backend)
8
+ finder.find_missing_keys
9
+ end
10
+ end
11
+ end
12
+ end