langulator 0.0.1

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/.gitignore ADDED
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in langulator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Katrina Owen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Langulator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'langulator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install langulator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/langulator ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'langulator'
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Katrina Owen"]
5
+ gem.email = ["katrina.owen@gmail.com"]
6
+ gem.description = %q{Manage, maintain, and munge your i18n files.}
7
+ gem.summary = %q{Tasks to keep your i18n files managable.}
8
+ gem.homepage = "http://github.com/kytrinyx/langulator"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "langulator"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = "0.0.1"
16
+
17
+ gem.add_development_dependency "rspec"
18
+ end
data/lib/langulator.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'langulator/loader'
2
+ require 'langulator/munger'
3
+
4
+ module Langulator
5
+ def self.compile(options = {})
6
+ loader = Loader.new(options)
7
+ munger = Munger.new(:language => loader.origin, :translations => loader.source_translations, :alternates => loader.destination_translations)
8
+
9
+ munger.munge
10
+ end
11
+
12
+ def self.write(options)
13
+ translations = compile(options)
14
+ File.open(options[:to], 'w') do |file|
15
+ file.write translations.to_yaml
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module Langulator
2
+ class Loader
3
+
4
+ attr_reader :base_path, :origin, :alternates
5
+ def initialize(options = {})
6
+ @base_path = options[:base_path]
7
+ @origin = options[:origin]
8
+ @alternates = options[:alternates]
9
+ end
10
+
11
+ def paths
12
+ @paths ||= Dir.glob("#{base_path}#{origin}.yml").map {|file| file.gsub("#{origin}.yml", '') }.sort
13
+ end
14
+
15
+ def source_translations
16
+ translations = load_translations(origin)
17
+ end
18
+
19
+ def destination_translations
20
+ translations = {}
21
+ alternates.each do |language|
22
+ translations[language] = load_translations(language)
23
+ end
24
+ translations
25
+ end
26
+
27
+ def load_translations(language)
28
+ translations = {}
29
+ paths.each do |path|
30
+ translations[path] = read_translations(path, language)
31
+ end
32
+ translations
33
+ end
34
+
35
+ def read_translations(path, language)
36
+ file = "#{path}#{language}.yml"
37
+ if File.exists?(file)
38
+ YAML.load(File.read(file))
39
+ else
40
+ {}
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ module Langulator
2
+ class Munger
3
+
4
+ attr_reader :source_language, :source_translations, :alternate_translations
5
+ def initialize(options = {})
6
+ @source_language = options[:language]
7
+ @source_translations = options[:translations]
8
+ @alternate_translations = options[:alternates]
9
+ end
10
+
11
+ def munge
12
+ dictionary = transform(source_language, source_translations)
13
+ alternate_translations.each do |language, translations|
14
+ dictionary = insert(language, translations, dictionary)
15
+ end
16
+ dictionary
17
+ end
18
+
19
+ def transform(language, translations)
20
+ dictionary = {}
21
+ translations.each do |key, value|
22
+ dictionary[key] ||= {}
23
+ if value.is_a?(Hash)
24
+ dictionary[key] = transform(language, value)
25
+ else
26
+ dictionary[key][language] = value
27
+ end
28
+ end
29
+ dictionary
30
+ end
31
+
32
+ def insert(language, translations, dictionary)
33
+ dictionary.dup.each do |key, value|
34
+ if value.is_a?(Hash)
35
+ insert(language, (translations || {})[key], value)
36
+ else
37
+ dictionary[language] = translations
38
+ end
39
+ end
40
+ dictionary
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ ---
2
+ food:
3
+ breakfast: yoghurt
4
+ lunch: sandwich
5
+ dinner:
6
+ main_course: steak
7
+ side: baked potato
8
+ desert: chocolate mousse
@@ -0,0 +1,5 @@
1
+ ---
2
+ volume:
3
+ sound: loud
4
+ liquid: sloshing
5
+ hair: "because I'm worth it"
@@ -0,0 +1,3 @@
1
+ ---
2
+ food:
3
+ lunch: smørbrød
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require 'langulator'
3
+
4
+ describe Langulator do
5
+ let(:options) { {:base_path => 'spec/fixtures/**/', :origin => 'english', :alternates => ['norsk', 'francais']} }
6
+
7
+ let(:combined) do
8
+ {
9
+ "spec/fixtures/" => {
10
+ "food" => {
11
+ "breakfast" => {"english" => "yoghurt", "norsk" => nil, "francais" => nil},
12
+ "lunch" => {"english" => "sandwich", "norsk" => "smørbrød", "francais" => nil},
13
+ "dinner" => {
14
+ "main_course" => {"english" => "steak", "norsk" => nil, "francais" => nil},
15
+ "side" => {"english" => "baked potato", "norsk" => nil, "francais" => nil},
16
+ "desert" => {"english" => "chocolate mousse", "norsk" => nil, "francais" => nil}
17
+ }
18
+ }
19
+ },
20
+ "spec/fixtures/lang/" => {
21
+ "volume" => {
22
+ "sound" => {"english" => "loud", "norsk" => nil, "francais" => nil},
23
+ "liquid" => {"english" => "sloshing", "norsk" => nil, "francais" => nil},
24
+ "hair" => {"english" => "because I'm worth it", "norsk" => nil, "francais" => nil}
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ it "loads and munges" do
31
+ Langulator.compile(options).should eq(combined)
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'langulator/loader'
3
+
4
+ describe Langulator::Loader do
5
+
6
+ let(:english) do
7
+ {
8
+ "spec/fixtures/" => {
9
+ "food" => {
10
+ "breakfast" => "yoghurt",
11
+ "lunch" => "sandwich",
12
+ "dinner" => {"main_course" => "steak", "side" => "baked potato", "desert" => "chocolate mousse"}
13
+ }
14
+ },
15
+ "spec/fixtures/lang/" => {
16
+ "volume" => {
17
+ "sound" => "loud",
18
+ "liquid" => "sloshing",
19
+ "hair" => "because I'm worth it"
20
+ }
21
+ }
22
+ }
23
+ end
24
+
25
+ let(:norwegian) do
26
+ {
27
+ "spec/fixtures/" => {
28
+ "food" => {
29
+ "lunch" => "smørbrød"
30
+ }
31
+ },
32
+ "spec/fixtures/lang/" => {}
33
+ }
34
+ end
35
+
36
+ let(:french) do
37
+ {
38
+ "spec/fixtures/" => {},
39
+ "spec/fixtures/lang/" => {}
40
+ }
41
+ end
42
+
43
+ subject { Langulator::Loader.new(:base_path => 'spec/fixtures/**/', :origin => 'english', :alternates => ['norsk', 'francais']) }
44
+
45
+ its(:paths) { should eq(['spec/fixtures/', 'spec/fixtures/lang/']) }
46
+
47
+ its(:source_translations) { should eq(english) }
48
+ its(:destination_translations) { should eq({'norsk' => norwegian, 'francais' => french}) }
49
+ end
@@ -0,0 +1,102 @@
1
+ require 'langulator/munger'
2
+
3
+ describe Langulator::Munger do
4
+
5
+ let(:english) do
6
+ {
7
+ :game => {
8
+ :rock => "rock",
9
+ :paper => "paper",
10
+ :scissors => "scissors",
11
+ :other => {:deeply => "nested"}
12
+ }
13
+ }
14
+ end
15
+
16
+ let(:english_remapped) do
17
+ {
18
+ :game => {
19
+ :rock => {:english => "rock"},
20
+ :paper => {:english => "paper"},
21
+ :scissors => {:english => "scissors"},
22
+ :other => {
23
+ :deeply => {:english => "nested"}
24
+ }
25
+ }
26
+ }
27
+ end
28
+
29
+ let(:french) { {:game => {:paper => 'papier', :other => {:not => 'included'}}} }
30
+
31
+ let(:combined) do
32
+ {
33
+ :game => {
34
+ :rock => {
35
+ :english => "rock",
36
+ :french => nil
37
+ },
38
+ :paper => {
39
+ :english => "paper",
40
+ :french => "papier"
41
+ },
42
+ :scissors => {
43
+ :english => "scissors",
44
+ :french => nil
45
+ },
46
+ :other => {
47
+ :deeply => {
48
+ :english => "nested",
49
+ :french => nil
50
+ }
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ let(:empty_target) do
57
+ {
58
+ :game => {
59
+ :rock => {
60
+ :english => "rock",
61
+ :norwegian => nil
62
+ },
63
+ :paper => {
64
+ :english => "paper",
65
+ :norwegian => nil
66
+ },
67
+ :scissors => {
68
+ :english => "scissors",
69
+ :norwegian => nil
70
+ },
71
+ :other => {
72
+ :deeply => {
73
+ :english => "nested",
74
+ :norwegian => nil
75
+ }
76
+ }
77
+ }
78
+ }
79
+ end
80
+
81
+ context "partial munges" do
82
+ subject { Langulator::Munger.new }
83
+
84
+ it "remaps a source dictionary" do
85
+ subject.transform(:english, english).should eq(english_remapped)
86
+ end
87
+
88
+ it "inserts an alternate language" do
89
+ subject.insert(:french, french, english_remapped).should eq(combined)
90
+ end
91
+
92
+ it "inserts an empty alternate language" do
93
+ subject.insert(:norwegian, {}, english_remapped).should eq(empty_target)
94
+ end
95
+ end
96
+
97
+ it "munges" do
98
+ munger = Langulator::Munger.new(:language => :english, :translations => english, :alternates => {:french => french})
99
+ munger.munge.should eq(combined)
100
+ end
101
+
102
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: langulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Katrina Owen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70137034740080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70137034740080
25
+ description: Manage, maintain, and munge your i18n files.
26
+ email:
27
+ - katrina.owen@gmail.com
28
+ executables:
29
+ - langulator
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - bin/langulator
39
+ - langulator.gemspec
40
+ - lib/langulator.rb
41
+ - lib/langulator/loader.rb
42
+ - lib/langulator/munger.rb
43
+ - spec/fixtures/english.yml
44
+ - spec/fixtures/lang/english.yml
45
+ - spec/fixtures/norsk.yml
46
+ - spec/langulator_spec.rb
47
+ - spec/loader_spec.rb
48
+ - spec/munger_spec.rb
49
+ homepage: http://github.com/kytrinyx/langulator
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.15
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Tasks to keep your i18n files managable.
73
+ test_files:
74
+ - spec/fixtures/english.yml
75
+ - spec/fixtures/lang/english.yml
76
+ - spec/fixtures/norsk.yml
77
+ - spec/langulator_spec.rb
78
+ - spec/loader_spec.rb
79
+ - spec/munger_spec.rb