urbane 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -37,12 +37,7 @@ directory as JSON files. It uses the googles JSON api as opposed to the XML api.
37
37
 
38
38
  ## Output format
39
39
 
40
- For now it only supports JSON
41
-
42
- {
43
- "sun_intro_step2"=>"Build another one…",
44
- "sun_intro_step3"=>"... and another one."
45
- }
40
+ Support for YAML, JSON and XML
46
41
 
47
42
  ## Output structure
48
43
 
@@ -65,4 +60,4 @@ For now it needs to have a certain format. Check the [demo document](https://doc
65
60
  2. Click on File -> Publish To The Web
66
61
  3. Check 'Automatically republish when changes are made'
67
62
  4. Find the spreadsheet id in the generated link. It is marked with 'key='. In the following url, the key would be '0Auo5c2PWMqR4dHlOSjlXcjY0X01udzNPdHlKZ09QTVE': https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Auo5c2PWMqR4dHlOSjlXcjY0X01udzNPdHlKZ09QTVE&output=html
68
- 5. Click on the close button
63
+ 5. Click on the close button
@@ -1,3 +1,3 @@
1
1
  module Urbane
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/urbane.rb CHANGED
@@ -1,11 +1,29 @@
1
1
  require "urbane/version"
2
2
  require "urbane/vendor/ordered_hash"
3
3
  require "json"
4
+ require "yaml"
5
+ require "nokogiri"
4
6
  require "open-uri"
5
7
 
6
8
  module Urbane
7
9
  class Generator
8
10
 
11
+ GENERATORS = {
12
+ :json => lambda{|content| JSON.pretty_generate(content)},
13
+ :yaml => lambda{|content| content.to_yaml},
14
+ :xml => lambda do |content|
15
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
16
+ xml.send('resource-bundle', 'xmlns' => '',
17
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance') do
18
+ content.each do |key, translation|
19
+ xml.resource(translation, 'key' => key)
20
+ end
21
+ end
22
+ end
23
+ builder.to_xml(:indent => 2, :encoding => 'UTF-8')
24
+ end
25
+ }
26
+
9
27
  def initialize(options)
10
28
  @target_dir = options[:target_dir]
11
29
  @spreadsheet_id = options[:spreadsheet_id]
@@ -13,6 +31,7 @@ module Urbane
13
31
  @language_locale_map = options[:languages]
14
32
  @languages = @language_locale_map.keys
15
33
  @fallback_language = options[:fallback_language]
34
+ @format = options[:format] || :json
16
35
  end
17
36
 
18
37
  def run
@@ -35,7 +54,7 @@ module Urbane
35
54
  @language_locale_map.each do |language, locale|
36
55
  `mkdir -p #{@target_dir}/#{locale}`
37
56
  File.open("#{@target_dir}/#{locale}/#{@file_name_for_translation_file}", 'w') do |f|
38
- f.write JSON.pretty_generate(sorted_hash_for_language(language))
57
+ f.write GENERATORS[@format].call(sorted_hash_for_language(language))
39
58
  end
40
59
  end
41
60
  end
data/test/urbane_test.rb CHANGED
@@ -1,9 +1,18 @@
1
1
  # encoding: UTF-8
2
2
  require 'test_helper'
3
3
 
4
+ FIXTURE_FILE_PATH = File.join('test', 'fixtures')
5
+ TARGET_DIR = File.join('/tmp', 'translation_generator_test')
6
+
7
+ def read_file(path)
8
+ File.open(File.join(TARGET_DIR, path), "r"){ |f| f.read }
9
+ end
10
+
11
+ def read_json_file(path)
12
+ JSON.parse(read_file(path))
13
+ end
14
+
4
15
  class Urbane::GeneratorTest < Test::Unit::TestCase
5
- FIXTURE_FILE_PATH = File.join('test', 'fixtures')
6
- TARGET_DIR = File.join('/tmp', 'translation_generator_test')
7
16
 
8
17
  context 'generator' do
9
18
  setup do
@@ -33,17 +42,17 @@ class Urbane::GeneratorTest < Test::Unit::TestCase
33
42
  :spanish => 'es',
34
43
  :portuguese => 'pt'
35
44
  },
36
- :fallback_language => :english
45
+ :fallback_language => :english,
46
+ :format => :json
37
47
  }
38
- @generator = Urbane::Generator.new(@options)
39
- @generator.run
40
- end
48
+ end
41
49
 
42
50
  teardown do
43
51
  FileUtils.rm_rf(TARGET_DIR)
44
52
  end
45
53
 
46
54
  should 'create a folder and a document for each locale' do
55
+ Urbane::Generator.new(@options).run
47
56
  @options[:languages].values.each do |locale|
48
57
  expected_file = File.join(TARGET_DIR, locale,'text_ids.json')
49
58
  assert File.exists?(expected_file), "file #{expected_file} should exist"
@@ -51,14 +60,32 @@ class Urbane::GeneratorTest < Test::Unit::TestCase
51
60
  end
52
61
 
53
62
  should 'fall back if a key is empty' do
54
- info_hash_fr = JSON.parse(File.open(File.join(TARGET_DIR,'fr', 'text_ids.json'), "r"){ |f| f.read })
55
- info_hash_us = JSON.parse(File.open(File.join(TARGET_DIR,'en', 'text_ids.json'), "r"){ |f| f.read })
63
+ Urbane::Generator.new(@options).run
64
+ info_hash_fr = read_json_file(File.join('fr', 'text_ids.json'))
65
+ info_hash_us = read_json_file(File.join('en', 'text_ids.json'))
56
66
  assert_equal info_hash_us['sun_intro_step2'], info_hash_fr['sun_intro_step2']
57
67
  end
58
68
 
59
69
  should 'handle special chars' do
60
- info_hash_de = JSON.parse(File.open(File.join(TARGET_DIR,'de', 'text_ids.json'), "r"){ |f| f.read })
70
+ Urbane::Generator.new(@options).run
71
+ info_hash_de = read_json_file(File.join('de', 'text_ids.json'))
61
72
  assert info_hash_de['sun_intro_step2'].include?('äÄö')
62
73
  end
74
+
75
+ # How do I test this? JSON is a subset of YAML, so most JSON will actually
76
+ # be valid YAML
77
+ should 'support yaml output' do
78
+ @options[:format] = :yaml
79
+ @options[:file_name] = 'text_ids.yml'
80
+ Urbane::Generator.new(@options).run
81
+ assert YAML.load(read_file(File.join('en', 'text_ids.yml')));
82
+ end
83
+
84
+ should 'support xml' do
85
+ @options[:format] = :xml
86
+ @options[:file_name] = 'text_ids.xml'
87
+ Urbane::Generator.new(@options).run
88
+ assert Nokogiri::XML(read_file(File.join('en', 'text_ids.xml')));
89
+ end
63
90
  end
64
91
  end
data/urbane.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency "rake"
26
26
 
27
27
  s.add_runtime_dependency "json", "1.6.1"
28
+ s.add_runtime_dependency "nokogiri", "1.5.0"
28
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urbane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mocha
16
- requirement: &70255489866860 !ruby/object:Gem::Requirement
16
+ requirement: &70150065933880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70255489866860
24
+ version_requirements: *70150065933880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda-context
27
- requirement: &70255489866240 !ruby/object:Gem::Requirement
27
+ requirement: &70150065933200 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70255489866240
35
+ version_requirements: *70150065933200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fakeweb
38
- requirement: &70255489865520 !ruby/object:Gem::Requirement
38
+ requirement: &70150065932440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70255489865520
46
+ version_requirements: *70150065932440
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70255489864700 !ruby/object:Gem::Requirement
49
+ requirement: &70150065931700 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70255489864700
57
+ version_requirements: *70150065931700
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: json
60
- requirement: &70255489863920 !ruby/object:Gem::Requirement
60
+ requirement: &70150065930840 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - =
@@ -65,7 +65,18 @@ dependencies:
65
65
  version: 1.6.1
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70255489863920
68
+ version_requirements: *70150065930840
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: &70150065930040 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - =
75
+ - !ruby/object:Gem::Version
76
+ version: 1.5.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70150065930040
69
80
  description: Read google spreadsheet and generate translation files out of it
70
81
  email:
71
82
  - patrick.huesler@gmail.com