aigu 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 862c7adb7ffd92e40de8e7f9e53b817c1ffbf1e6
4
+ data.tar.gz: 057bb56afa2fb168f16f3f33f580f9ae7eff88a2
5
+ SHA512:
6
+ metadata.gz: f5a9a45689e04f9af76887a8aef2c8fa9747da4e9c2a7aed80db06391aaf3346ca2fa492aed559cede3817b6a4b3deb59310e6f19c2cadc711237d65cbc2dab9
7
+ data.tar.gz: e9c729ece1be9074b2c4d0f78cfccd86084cd4e35044f74c4cfb2a83fefdbe4f7efaf484208ef523ad1c1b07d3c5e37f7b5627e087d6bbb692c74fceebc32098
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2014, Mirego
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ - Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ - Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ - Neither the name of the Mirego nor the names of its contributors may
13
+ be used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,62 @@
1
+ ## Introduction
2
+
3
+ Aigu processes Rails YAML localization files and generates a single JSON file
4
+ that can be imported into the Accent service. It can also process a JSON file
5
+ generated by Accent and convert it back to multiple YAML files.
6
+
7
+ ## Installation
8
+
9
+ Add these lines to your application’s Gemfile as development dependancies:
10
+
11
+ ```ruby
12
+ group :development do
13
+ gem 'aigu'
14
+ end
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Aigu provides an executable named `aigu`.
20
+
21
+ ### Exporting the JSON file for Accent
22
+
23
+ The `export` command looks for YAML localization files and generates a JSON
24
+ file. This file will then be compatible with Accent.
25
+
26
+ ```bash
27
+ $ aigu export --locale=fr --input-directory=config/locales --output-file=my-project.json
28
+ ```
29
+
30
+ ### Options
31
+
32
+ | Option | Description |
33
+ |-------------------|---------------------------------------------------------------------------------------------------------------------------|
34
+ | `locale` | The locale used to find localization files. Files that match `*.<locale>.yml` into the input directory will be processed. |
35
+ | `input-directory` | The directory used to find localization files |
36
+ | `output-file` | The path to the JSON file that will be written by `aigu` |
37
+
38
+ ### Importing the JSON file from Accent
39
+
40
+ The `import` command takes a generated JSON file from Accent and generates
41
+ the original YAML file structure.
42
+
43
+ ```bash
44
+ $ aigu import --input-file=file-from-accent.json --output-directory=config/locales
45
+ ```
46
+
47
+ ### Options
48
+
49
+ | Option | Description |
50
+ |--------------------|-------------------------------------------------------------------|
51
+ | `input-file` | The path to the Accent-generated JSON file |
52
+ | `output-directory` | The directory where the localization YAML files will be generated |
53
+
54
+ ## License
55
+
56
+ `Aigu` is © 2014 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/aigu/blob/master/LICENSE.md) file.
57
+
58
+ ## About Mirego
59
+
60
+ [Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).
61
+
62
+ We also [love open-source software](http://open.mirego.com) and we try to give back to the community as much as we can.
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Start an IRB session with the gem'
4
+ task :console do
5
+ $:.unshift File.expand_path('..', __FILE__)
6
+ require 'aigu'
7
+ require 'irb'
8
+
9
+ ARGV.clear
10
+ IRB.start
11
+ end
data/SPEC.md ADDED
@@ -0,0 +1,65 @@
1
+ # Specification
2
+
3
+ ## Keys
4
+
5
+ Keys are generated based on the localization file path and the YAML key.
6
+
7
+ ```json
8
+ {
9
+ "<path>|<key>": "<value>"
10
+ }
11
+ ```
12
+
13
+ Where:
14
+
15
+ | Item | Description |
16
+ |-----------|-----------------------------------------------------------------------|
17
+ | `<path>` | The path of the YAML localization file (with `/` used as a separator) |
18
+ | `<key> ` | The full YAML key with `:` used as a separator between levels |
19
+ | `<value>` | The localized string |
20
+
21
+ ## Examples
22
+
23
+ ### Input
24
+
25
+ ```yaml
26
+ # config/locales/app/users.fr.yml
27
+ fr:
28
+ app:
29
+ users:
30
+ index:
31
+ new: 'Nouvel utilisateur'
32
+ edit: 'Modifier l’utilisateur %{user}'
33
+
34
+ # config/locales/app/shared/comments.fr.yml
35
+ fr:
36
+ app:
37
+ shared:
38
+ comments:
39
+ title: 'Tous les commentaires'
40
+
41
+ # config/locales/admin/shared.fr.yml
42
+ fr:
43
+ admin:
44
+ shared:
45
+ footer:
46
+ copyright: 'Copyright © %{year}'
47
+
48
+ # config/locales/layouts.fr.yml
49
+ fr:
50
+ layouts:
51
+ application:
52
+ title: 'Mon Application'
53
+ ```
54
+
55
+ ### Output
56
+
57
+ ```json
58
+ {
59
+ "app/users.fr|fr.app.users.index.new": "Nouvel utilisateur",
60
+ "app/users.fr|fr.app.users.index.edit": "Modifier l’utilisateur %{user}",
61
+ "app/shared/comments.fr|fr.app.shared.comments.title": "Tous les commentaires",
62
+ "admin/shared.fr|fr.admin.shared.footer.copyright": "Copyright © %{year}",
63
+ "layouts.fr|fr.layouts.application.title": "Mon Application"
64
+ }
65
+ ```
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aigu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'aigu'
8
+ spec.version = Aigu::VERSION
9
+ spec.authors = ['Rémi Prévost']
10
+ spec.email = ['rprevost@mirego.com']
11
+ spec.description = 'Aigu converts a directory of Rails localization files into a single JSON file to import in Accent.'
12
+ spec.summary = spec.description
13
+ spec.homepage = 'https://github.com/mirego/aigu'
14
+ spec.license = 'BSD 3-Clause'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'aigu'
6
+
7
+ Aigu::CLI.new(ENV.dup, ARGV.dup).run
@@ -0,0 +1,85 @@
1
+ require 'aigu/version'
2
+
3
+ require 'optparse'
4
+ require 'fileutils'
5
+ require 'json'
6
+ require 'yaml'
7
+
8
+ require 'aigu/extensions/hash'
9
+ require 'aigu/cli'
10
+
11
+ module Aigu
12
+ PUBLIC_COMMANDS = %w(import export)
13
+
14
+ def self.import(input_file: nil, output_directory: nil)
15
+ puts "Generating YAML files in `#{output_directory}` based on Accent-generated `#{input_file}` file"
16
+ puts '---'
17
+ json = File.read(input_file)
18
+ object = JSON.parse(json)
19
+ blob = Hash.recursive
20
+
21
+ object.each_pair do |key, value|
22
+ filename, flat_key = key.split('|')
23
+
24
+ parts = flat_key.split('.')
25
+ hash = blob[filename]
26
+
27
+ parts.each_with_index do |part, index|
28
+ if index + 1 < parts.length
29
+ hash = hash[part]
30
+ else
31
+ hash[part] = value
32
+ end
33
+ end
34
+ end
35
+
36
+ blob.each_pair do |file_name, hash|
37
+ file_path = File.join(output_directory, "#{file_name}.yml")
38
+ puts "Generating #{file_path}"
39
+ FileUtils.mkdir_p(File.dirname(file_path))
40
+
41
+ File.open(file_path, 'w+') do |file|
42
+ file << hash.to_yaml(line_width: 100_000_000)
43
+ end
44
+ end
45
+
46
+ puts '---'
47
+ puts 'Done'
48
+ end
49
+
50
+ def self.export(output_file: nil, input_directory: nil)
51
+ puts "Generating Accent JSON file `#{output_file}` based on YAML localization files in `#{input_directory}` directory"
52
+ puts '---'
53
+
54
+ output = {}
55
+
56
+ Dir[File.join(input_directory, '**', '*.yml')].each do |file|
57
+ content = YAML.load_file(file)
58
+ base_key = file.gsub(input_directory, '').gsub(/^\/*/, '').gsub(/\.yml$/, '|')
59
+
60
+ output.merge! flattenize_hash(content, base_key)
61
+ end
62
+
63
+ file_path = output_file
64
+ puts "Generating #{file_path}"
65
+ FileUtils.mkdir_p(File.dirname(file_path))
66
+
67
+ File.open(file_path, 'w+') do |file|
68
+ file << output.to_json
69
+ end
70
+
71
+ puts '---'
72
+ puts 'Done'
73
+ end
74
+
75
+ def self.flattenize_hash(hash, base_key = '')
76
+ if hash.is_a?(Hash)
77
+ hash.reduce({}) do |memo, (key, value)|
78
+ new_base_key = [base_key, key].join('.').sub('|.', '|')
79
+ memo.merge! flattenize_hash(value, new_base_key)
80
+ end
81
+ else
82
+ { base_key => hash }
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,44 @@
1
+ module Aigu
2
+ class CLI
3
+ def initialize(env, argv)
4
+ @env = env
5
+ @command = Aigu::PUBLIC_COMMANDS.include?(argv.first) ? argv.shift : nil
6
+ @argv = argv
7
+ parse_options
8
+ end
9
+
10
+ def run
11
+ Aigu.send(@command, @options) if @command
12
+ end
13
+
14
+ def parse_options
15
+ @options = {}
16
+
17
+ OptionParser.new do |opts|
18
+ opts.banner = 'Usage: aigu [options]'
19
+
20
+ opts.on('--input-directory=', 'The directory in which the Rails YAML localization files are stored.') do |directory|
21
+ @options[:input_directory] = directory
22
+ end
23
+
24
+ opts.on('--output-directory=', 'The directory in which the Rails YAML localization files will be generated.') do |directory|
25
+ @options[:output_directory] = directory
26
+ end
27
+
28
+ opts.on('--input-file=', 'The JSON file generated by Accent.') do |file|
29
+ @options[:input_file] = file
30
+ end
31
+
32
+ opts.on('--output-file=', 'The JSON file that will be generated for Accent.') do |file|
33
+ @options[:output_file] = file
34
+ end
35
+
36
+ opts.on_tail('-h', '--help', 'Show this message') do
37
+ puts opts
38
+ exit
39
+ end
40
+
41
+ end.parse! @argv
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ class Hash
2
+ def self.recursive
3
+ new do |hash, key|
4
+ hash[key] = recursive
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Aigu
2
+ VERSION = '0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aigu
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Rémi Prévost
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Aigu converts a directory of Rails localization files into a single JSON
42
+ file to import in Accent.
43
+ email:
44
+ - rprevost@mirego.com
45
+ executables:
46
+ - aigu
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.md
53
+ - README.md
54
+ - Rakefile
55
+ - SPEC.md
56
+ - aigu.gemspec
57
+ - bin/aigu
58
+ - lib/aigu.rb
59
+ - lib/aigu/cli.rb
60
+ - lib/aigu/extensions/hash.rb
61
+ - lib/aigu/version.rb
62
+ homepage: https://github.com/mirego/aigu
63
+ licenses:
64
+ - BSD 3-Clause
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.1.0
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Aigu converts a directory of Rails localization files into a single JSON
86
+ file to import in Accent.
87
+ test_files: []