strings_xml_localizer 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1cd9018ea9ebf33a57e98dd4a7b6319f08e92537
4
+ data.tar.gz: cdf804d306db2c4de66282939702b8dcd1c3c673
5
+ SHA512:
6
+ metadata.gz: 226d8842395169dca7b08f9808d5725150837182930abc20fa9f8302f54c7289d478ecd280cdf84b92f9ff803f96578915ade4205ab9d89ca1915c2df44ca8c8
7
+ data.tar.gz: 257b8f500486dfc74f50cee93588d8199a80887af8a85cbaffe94b6894fb58c93f7f01d34df477762833d8c0c8fd785a737c7c7efcb7c258c66b5c03c2e41734
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 strings_xml_localizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Hoang Phan
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,72 @@
1
+ # StringsXmlLocalizer
2
+
3
+ A very simple gem to translate contents of a strings.xml file from one locale to another. It could be used for localize your app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'strings_xml_localizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install strings_xml_localizer
18
+
19
+ ## Usage
20
+
21
+ First, require it in your file:
22
+
23
+ ```ruby
24
+ require 'strings_xml_localizer'
25
+ ```
26
+
27
+ ### Localize from a string and receive output as a string:
28
+
29
+ ```ruby
30
+ input = %Q(
31
+ <?xml version="1.0" encoding="utf-8"?>
32
+ <resources>
33
+ <string name="hello">Hello!</string>
34
+ <string name="hi">Hello!</string>
35
+ </resources>
36
+ )
37
+ StringsXmlLocalizer.string_to_string(input)
38
+
39
+ ### OUTPUT
40
+ # "\n<resources>\n <string name=\"hello\">こんにちは!</string>\n <string name=\"hi\">こんにちは!</string>\n</resources>\n"
41
+ ###
42
+ ```
43
+
44
+ ### Localize from a string and write output to file
45
+
46
+ ```ruby
47
+ StringsXmlLocalizer.string_to_file(input, to: 'my_output.xml')
48
+ ```
49
+
50
+ ### Localize from a file and receive output as a string
51
+
52
+ ```ruby
53
+ StringsXmlLocalizer.file_to_string(input, from: 'input.xml')
54
+ ```
55
+
56
+ ### Localize from a file and write output to file
57
+
58
+ ```ruby
59
+ StringsXmlLocalizer.file_to_file(from: 'input.xml', to: 'output.xml')
60
+ ```
61
+
62
+ ### Options:
63
+ - `:from`: File to read from, default: `strings.xml`
64
+ - `:to`: File to write to, default: `output.xml`
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( http://github.com/hoangphanea/strings_xml_localizer/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module StringsXmlLocalizer
2
+ XML_HEADER = '<?xml version="1.0" encoding="utf-8"?>'
3
+ DEFAULT_FROM = 'strings.xml'
4
+ DEFAULT_TO = 'output.xml'
5
+ end
@@ -0,0 +1,3 @@
1
+ module StringsXmlLocalizer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'strings_xml_localizer/version'
2
+ require 'strings_xml_localizer/constants'
3
+ require 'go_translator'
4
+ require 'ox'
5
+
6
+ module StringsXmlLocalizer
7
+ def self.file_to_file(options = {})
8
+ options = default_options.merge(options)
9
+ string_to_file(File.open(options[:from],"r:UTF-8",&:read), options)
10
+ end
11
+
12
+ def self.file_to_string(options = {})
13
+ options = default_options.merge(options)
14
+ string_to_string(File.open(options[:from],"r:UTF-8",&:read))
15
+ end
16
+
17
+ def self.string_to_file(src, options = {})
18
+ options = default_options.merge(options)
19
+ File.open(options[:to], 'w') do |f|
20
+ f.write(XML_HEADER)
21
+ f.write(string_to_string(src))
22
+ end
23
+ end
24
+
25
+ def self.string_to_string(src)
26
+ in_xml = Ox.parse(src)
27
+ output = Ox::Document.new(version: '1.0', encoding: 'utf-8')
28
+
29
+ in_resources = in_xml.resources
30
+ out_resources = generate_out_resources(in_resources)
31
+ output << out_resources
32
+
33
+ Ox.dump(output)
34
+ end
35
+
36
+ def self.generate_out_resources(in_resources)
37
+ out_resources = Ox::Element.new('resources')
38
+ i = 0
39
+ while 1
40
+ begin
41
+ out = Ox::Element.new('string')
42
+ out[:name] = in_resources.string(i).attributes[:name]
43
+ out << translate_string_at(in_resources, i, :ja)
44
+ out_resources << out
45
+ i += 1
46
+ rescue
47
+ break
48
+ end
49
+ end
50
+ out_resources
51
+ end
52
+
53
+ def self.translate_string_at(resources, index, to)
54
+ GoTranslator.translate(resources.string(index).text, to: to)
55
+ end
56
+
57
+ def self.default_options
58
+ {
59
+ from: DEFAULT_FROM,
60
+ to: DEFAULT_TO
61
+ }
62
+ end
63
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'strings_xml_localizer'
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe StringsXmlLocalizer do
4
+ let(:src) do
5
+ %Q(
6
+ <?xml version="1.0" encoding="utf-8"?>
7
+ <resources>
8
+ <string name="hello">Hello!</string>
9
+ <string name="hi">Hello!</string>
10
+ </resources>
11
+ )
12
+ end
13
+
14
+ let(:expected_result) { "\n<resources>\n <string name=\"hello\">こんにちは!</string>\n <string name=\"hi\">こんにちは!</string>\n</resources>\n" }
15
+ let(:from) { 'from.xml' }
16
+ let(:to) { 'to.xml' }
17
+
18
+ it 'has a version number' do
19
+ expect(StringsXmlLocalizer::VERSION).not_to be nil
20
+ end
21
+
22
+ describe '.file_to_file' do
23
+ let(:writer) { double }
24
+
25
+ before do
26
+ expect(File).to receive(:open).with(to, 'w').and_yield(writer)
27
+ expect(File).to receive(:open).with(from,'r:UTF-8').and_return(src)
28
+ end
29
+
30
+ after do
31
+ StringsXmlLocalizer.file_to_file(from: from, to: to)
32
+ end
33
+
34
+ it 'writes correct string' do
35
+ expect(writer).to receive(:write).with(StringsXmlLocalizer::XML_HEADER)
36
+ expect(writer).to receive(:write).with(expected_result)
37
+ end
38
+ end
39
+
40
+ describe '.string_to_file' do
41
+ let(:writer) { double }
42
+
43
+ before do
44
+ expect(File).to receive(:open).with(to, 'w').and_yield(writer)
45
+ end
46
+
47
+ after do
48
+ StringsXmlLocalizer.string_to_file(src, to: to)
49
+ end
50
+
51
+ it 'writes correct string' do
52
+ expect(writer).to receive(:write).with(StringsXmlLocalizer::XML_HEADER)
53
+ expect(writer).to receive(:write).with(expected_result)
54
+ end
55
+ end
56
+
57
+ describe '.file_to_string' do
58
+ before do
59
+ expect(File).to receive(:open).with(from,'r:UTF-8').and_return(src)
60
+ end
61
+
62
+ it 'returns correct string' do
63
+ expect(StringsXmlLocalizer.file_to_string(from: from)).to eq expected_result
64
+ end
65
+ end
66
+
67
+ describe '.string_to_string' do
68
+ it 'returns correct string' do
69
+ expect(StringsXmlLocalizer.string_to_string(src)).to eq expected_result
70
+ end
71
+ end
72
+
73
+ describe '.generate_out_resources' do
74
+ let(:resources) { double }
75
+ let(:attributes) { { name: 'name' } }
76
+
77
+ before do
78
+ expect(resources).to receive(:string).with(0).and_return double(attributes: attributes)
79
+ expect(StringsXmlLocalizer).to receive(:translate_string_at)
80
+ end
81
+
82
+ it 'returns correct result' do
83
+ expect(StringsXmlLocalizer.generate_out_resources(resources).value).to eq 'resources'
84
+ end
85
+ end
86
+
87
+ describe '.translate_string_at' do
88
+ let(:resources) { double }
89
+ let(:index) { 1 }
90
+ let(:text) { 'text' }
91
+ let(:target) { :ja }
92
+ let(:result) { 'result' }
93
+
94
+ before do
95
+ expect(resources).to receive(:string).with(index).and_return(double(text: text))
96
+ expect(GoTranslator).to receive(:translate).with(text, to: target).and_return(result)
97
+ end
98
+
99
+ it 'returns correct result' do
100
+ expect(StringsXmlLocalizer.translate_string_at(resources, index, target)).to eq result
101
+ end
102
+ end
103
+
104
+ describe '.default_options' do
105
+ let(:expected_result) do
106
+ {
107
+ from: StringsXmlLocalizer::DEFAULT_FROM,
108
+ to: StringsXmlLocalizer::DEFAULT_TO
109
+ }
110
+ end
111
+
112
+ it 'returns correct default_options' do
113
+ expect(StringsXmlLocalizer.default_options).to eq expected_result
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'strings_xml_localizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strings_xml_localizer"
8
+ spec.version = StringsXmlLocalizer::VERSION
9
+ spec.authors = ["Hoang Phan"]
10
+ spec.email = ["hoang.phan@eastagile.com"]
11
+ spec.summary = "Localize strings.xml"
12
+ spec.description = "A simple gem to localize strings.xml files in android"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
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.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency "ox"
26
+ spec.add_dependency "go_translator"
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strings_xml_localizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hoang Phan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-19 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ox
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: go_translator
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A simple gem to localize strings.xml files in android
84
+ email:
85
+ - hoang.phan@eastagile.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/strings_xml_localizer.rb
96
+ - lib/strings_xml_localizer/constants.rb
97
+ - lib/strings_xml_localizer/version.rb
98
+ - spec/spec_helper.rb
99
+ - spec/strings_xml_localizer_spec.rb
100
+ - strings_xml_localizer.gemspec
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.9
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Localize strings.xml
125
+ test_files:
126
+ - spec/spec_helper.rb
127
+ - spec/strings_xml_localizer_spec.rb