babelish_rnc 1.0.0.pre
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 +7 -0
- data/.babelish.sample +35 -0
- data/.gitignore +34 -0
- data/.hound.yml +4 -0
- data/.travis.yml +15 -0
- data/CONTRIBUTING.md +12 -0
- data/Dockerfile +6 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +20 -0
- data/README.md +73 -0
- data/Rakefile +17 -0
- data/babelish.gemspec +41 -0
- data/babelish_rnc.gemspec +42 -0
- data/bin/babelish_rnc +6 -0
- data/lib/babelish_rnc.rb +31 -0
- data/lib/babelish_rnc/android2csv.rb +28 -0
- data/lib/babelish_rnc/base2csv.rb +103 -0
- data/lib/babelish_rnc/commandline.rb +188 -0
- data/lib/babelish_rnc/csv2android.rb +53 -0
- data/lib/babelish_rnc/csv2base.rb +189 -0
- data/lib/babelish_rnc/csv2json.rb +21 -0
- data/lib/babelish_rnc/csv2php.rb +25 -0
- data/lib/babelish_rnc/csv2strings.rb +34 -0
- data/lib/babelish_rnc/google_doc.rb +55 -0
- data/lib/babelish_rnc/json2csv.rb +20 -0
- data/lib/babelish_rnc/language.rb +30 -0
- data/lib/babelish_rnc/php2csv.rb +30 -0
- data/lib/babelish_rnc/strings2csv.rb +61 -0
- data/lib/babelish_rnc/version.rb +3 -0
- data/lib/babelish_rnc/xcode_macros.rb +49 -0
- data/test/babelish_rnc/commands/test_command_android2csv.rb +60 -0
- data/test/babelish_rnc/commands/test_command_csv2android.rb +35 -0
- data/test/babelish_rnc/commands/test_command_csv2strings.rb +139 -0
- data/test/babelish_rnc/commands/test_command_strings2csv.rb +110 -0
- data/test/babelish_rnc/test_android2csv.rb +53 -0
- data/test/babelish_rnc/test_base2csv.rb +43 -0
- data/test/babelish_rnc/test_bins.rb +32 -0
- data/test/babelish_rnc/test_commandline.rb +127 -0
- data/test/babelish_rnc/test_csv2android.rb +72 -0
- data/test/babelish_rnc/test_csv2base.rb +44 -0
- data/test/babelish_rnc/test_csv2json.rb +27 -0
- data/test/babelish_rnc/test_csv2php.rb +27 -0
- data/test/babelish_rnc/test_csv2strings.rb +154 -0
- data/test/babelish_rnc/test_json2csv.rb +34 -0
- data/test/babelish_rnc/test_php2csv.rb +73 -0
- data/test/babelish_rnc/test_strings2csv.rb +147 -0
- data/test/babelish_rnc/test_xcode_macros.rb +112 -0
- data/test/data/android-en.xml +9 -0
- data/test/data/android-fr.xml +9 -0
- data/test/data/android.xml +9 -0
- data/test/data/android_special_chars.csv +8 -0
- data/test/data/android_special_chars.xml +12 -0
- data/test/data/android_special_chars_test_result.xml +10 -0
- data/test/data/genstrings.strings +0 -0
- data/test/data/json.json +6 -0
- data/test/data/php_lang.php +8 -0
- data/test/data/test_comments.strings +2 -0
- data/test/data/test_data.csv +3 -0
- data/test/data/test_data.strings +2 -0
- data/test/data/test_data_fr_with_comments.strings +6 -0
- data/test/data/test_data_fr_with_comments.xml +9 -0
- data/test/data/test_data_multiple_langs.csv +3 -0
- data/test/data/test_data_with_comments.csv +3 -0
- data/test/data/test_data_with_percent.csv +3 -0
- data/test/data/test_data_with_percent_space.csv +4 -0
- data/test/data/test_data_with_semicolon.csv +3 -0
- data/test/data/test_data_with_spaces.csv +3 -0
- data/test/data/test_en.strings +2 -0
- data/test/data/test_fr.strings +2 -0
- data/test/data/test_space.strings +10 -0
- data/test/data/test_utf16.strings +0 -0
- data/test/data/test_with_nil.csv +3 -0
- data/test/data/test_with_nil.strings +4 -0
- data/test/data/xcode_empty.strings +7 -0
- data/test/test_helper.rb +17 -0
- metadata +311 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 721202378c4739d8503cf53580037e795d8afc60
|
4
|
+
data.tar.gz: dc2fdbd8b5a4d7b298f453c1e7689ac6f8a90041
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c316be815f90a02faa480c06f33b13766b219afff696068c83781528b9743eddeb30d78028bf38e3d4f171fa3c153798063d99cf5e2af42591661c8b2342f420
|
7
|
+
data.tar.gz: e6c57ab212a69d39c985a3862eb6e4b942b203feea095c94f63508eb051d29597b5744546b99d98e2e0c6a801ea0b7183f6d664b91a9da1ad8fcd52708145314
|
data/.babelish.sample
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# This is a sample configuration file
|
2
|
+
|
3
|
+
###################################### Base2CSV ######################################
|
4
|
+
|
5
|
+
## required, location of strings files (FILENAMES)
|
6
|
+
filenames: ["test/data/test_en.strings", "test/data/test_fr.strings"]
|
7
|
+
|
8
|
+
## optional
|
9
|
+
# headers: ["Constants", "English", "French"] # override headers of columns, default is name of input files and 'Variables' for reference
|
10
|
+
# csv_filename: "testconfig.csv" # location of output file
|
11
|
+
# dryrun: true # prints out content of hash without writing file
|
12
|
+
|
13
|
+
###################################### CSV2Base ######################################
|
14
|
+
|
15
|
+
## required
|
16
|
+
filename: test/data/test_data.csv # CSV file (CSV_FILENAME) to convert from or name of file in Google Drive
|
17
|
+
langs: # Languages to convert. i.e. English:en
|
18
|
+
English: "en"
|
19
|
+
|
20
|
+
## optional
|
21
|
+
# fetch: true # set to true to get file(filename) from google drive
|
22
|
+
# excluded_states: ["Images", "Xib"] # Exclude rows with given state
|
23
|
+
# comments_column: 4 # Position of column for comments if any
|
24
|
+
# state_column: 3 # Position of column for state if any
|
25
|
+
# keys_column: 0 # Position of column for keys
|
26
|
+
# default_lang: "English" # Header name of column containing fallback value if value is empty
|
27
|
+
# output_dir: "resources/" # Path of output files
|
28
|
+
# output_basenames: # Basename of output files
|
29
|
+
# - Localizable
|
30
|
+
# - info
|
31
|
+
# ignore_lang_path: true # does not care about lang component path. i.e: en.lproj/
|
32
|
+
# sheet: 0 # Index of worksheet to download. First index is 0.
|
33
|
+
# macros_filename: Babelish.h # File to output the defines of localized strings
|
34
|
+
# stripping: false # Strips values inside the spreadsheet
|
35
|
+
# csv_separator: ',' # CSV column separator character, uses ',' by default
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Generated files
|
2
|
+
/*.csv
|
3
|
+
/*.strings
|
4
|
+
*.lproj
|
5
|
+
*.gem
|
6
|
+
*~
|
7
|
+
*#
|
8
|
+
#*#
|
9
|
+
|
10
|
+
# Config file
|
11
|
+
.babelish
|
12
|
+
.babelish.token
|
13
|
+
|
14
|
+
# SimpleCov
|
15
|
+
coverage
|
16
|
+
|
17
|
+
# ignore Gemfile.lock as http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
|
18
|
+
# this should solve the fastercsv issue
|
19
|
+
Gemfile.lock
|
20
|
+
|
21
|
+
# Ruby package manager files
|
22
|
+
.ruby-version
|
23
|
+
.rbenv-version
|
24
|
+
.rvmrc
|
25
|
+
|
26
|
+
# Sublime Text
|
27
|
+
*.sublime-workspace
|
28
|
+
*.sublime-project
|
29
|
+
|
30
|
+
doc
|
31
|
+
.yardoc
|
32
|
+
|
33
|
+
|
34
|
+
.DS_Store
|
data/.hound.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
language: ruby
|
2
|
+
sudo: false
|
3
|
+
rvm:
|
4
|
+
- 2.1.0
|
5
|
+
- 2.2
|
6
|
+
- 2.3.1
|
7
|
+
deploy:
|
8
|
+
provider: rubygems
|
9
|
+
api_key:
|
10
|
+
secure: Xjq+v+jEU6wK4BtyfnV1elegNcxK6Ah/O99Sn9c2IlkCmJ1wxLBouqzEiSorSJ4IOMa5H2y3gwo5GXOr6Y7d8huyGrPuBeCSGqAmH77wNCIv7G+jnLiYb1sRZbtKcPW2QaN6JF81qDIelwyspMfo6/ug1qN1x323UaxZl7f7nUE=
|
11
|
+
gem: babelish
|
12
|
+
on:
|
13
|
+
tags: true
|
14
|
+
repo: netbe/Babelish
|
15
|
+
all_branches: true
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Want to add another support for a new format or/and usage? Add a new feature? Fix a bug?
|
4
|
+
|
5
|
+
1. Create a pull request with a branch like `feature/<nameofbranch>` or `hotfix/<nameofbranch>`.
|
6
|
+
2. Don't forget to explain the issue or the benefits of the feature you want to be merged.
|
7
|
+
3. Write unit tests covering the feature and/or fix the ones the feature/issue breaks.
|
8
|
+
|
9
|
+
And in any case feel free to ask for help in any of those steps.
|
10
|
+
|
11
|
+
|
12
|
+
|
data/Dockerfile
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 François Benaiteau
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
[](http://travis-ci.org/netbe/Babelish)
|
2
|
+
[](https://codeclimate.com/github/netbe/Babelish)[](https://coveralls.io/r/netbe/Babelish)
|
3
|
+
[](http://badge.fury.io/rb/babelish)
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
**Babelish : Chaotically confused, like Babel**
|
8
|
+
|
9
|
+
Originally created to deal with localizedStrings files (aka *CSV-to-iOS-Localizable.strings-converter*), this command tool now converts a csv file of translations into the below file formats and vice-versa:
|
10
|
+
* .strings (iOS)
|
11
|
+
* .xml (Android)
|
12
|
+
* .json
|
13
|
+
* .php
|
14
|
+
|
15
|
+
It can also fetch the csv file from GoogleDrive.
|
16
|
+
|
17
|
+
# Installation
|
18
|
+
|
19
|
+
`gem install babelish`
|
20
|
+
|
21
|
+
Requires Ruby 2.0.0 or above.
|
22
|
+
|
23
|
+
Or download [Latest Release](https://github.com/netbe/Babelish/releases/latest).
|
24
|
+
|
25
|
+
Or via docker: `docker run netbe/Babelish babelish help`
|
26
|
+
|
27
|
+
# Usage
|
28
|
+
|
29
|
+
```
|
30
|
+
› babelish help 18:21:30
|
31
|
+
Commands:
|
32
|
+
babelish android2csv # Convert .xml files to CSV file
|
33
|
+
babelish csv2android # Convert CSV file to .xml
|
34
|
+
babelish csv2json # Convert CSV file to .json
|
35
|
+
babelish csv2php # Convert CSV file to .php
|
36
|
+
babelish csv2strings # Convert CSV file to .strings
|
37
|
+
babelish csv_download # Download Google Spreadsheet containing translations
|
38
|
+
babelish help [COMMAND] # Describe available commands or one specific command
|
39
|
+
babelish init # Create a configuration file from template
|
40
|
+
babelish json2csv # Convert .json files to CSV file
|
41
|
+
babelish open FILE # Open local csv file in default editor or Google Spreadsheet containing translations in default browser
|
42
|
+
babelish php2csv # Convert .php files to CSV file
|
43
|
+
babelish strings2csv # Convert .strings files to CSV file
|
44
|
+
babelish version # Display current version
|
45
|
+
|
46
|
+
Options:
|
47
|
+
[--verbose], [--no-verbose]
|
48
|
+
-c, [--config=CONFIG] # Read configuration from given file
|
49
|
+
# Default: .babelish
|
50
|
+
```
|
51
|
+
|
52
|
+
You can use a **configuration file** to hold all your commandline arguments into a file.
|
53
|
+
Place a `.babelish` file (YAML) in your repo where you will run the command. In case you need to reset the Google Drive token you have to delete the .babelish.token file.
|
54
|
+
See [.babelish.sample](.babelish.sample) file in the doc folder. as the possible values.
|
55
|
+
|
56
|
+
*For previous CSV-to-iOS-Localizable.strings-converter, rename your `.csvconverter` into `.babelish`.*
|
57
|
+
|
58
|
+
**Note for docker:** In order to export the output of a conversion, you will need to pipe the result out of the container. An example would be
|
59
|
+
```
|
60
|
+
› docker run netbe/Babelish babelish csv2json >> some_file.json
|
61
|
+
```
|
62
|
+
|
63
|
+
**For more details, check the documentation:**
|
64
|
+
https://github.com/netbe/Babelish/wiki/How-to-Use
|
65
|
+
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
Run `bundle install` to install all the dependencies. Tests are done with `Test::Unit` so run `rake test` to run all the test suite.
|
70
|
+
|
71
|
+
# Todo & Known issues
|
72
|
+
|
73
|
+
See GitHub issues
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'yard'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/babelish_rnc/**/test_*.rb']
|
8
|
+
# t.warning = true
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
YARD::Rake::YardocTask.new do |t|
|
13
|
+
t.files = ['lib/**/*.rb']
|
14
|
+
# t.options = ['--any', '--extra', '--opts']
|
15
|
+
end
|
16
|
+
|
17
|
+
task :default => :test
|
data/babelish.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'babelishX'
|
7
|
+
s.version = '1.0.1'
|
8
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
9
|
+
s.summary = "CSV converter for localization files"
|
10
|
+
s.description = "This set of commands converts a CSV file to the following formats:
|
11
|
+
- .strings (iOS)
|
12
|
+
- .xml (Android)
|
13
|
+
- .json
|
14
|
+
- .php"
|
15
|
+
s.authors = ["François Benaiteau", "Markus Paeschke"]
|
16
|
+
s.email = ['francois.benaiteau@gmail.com', 'markus.paeschke@gmail.com']
|
17
|
+
s.homepage = 'http://netbe.github.io/Babelish/'
|
18
|
+
s.license = 'MIT'
|
19
|
+
|
20
|
+
s.add_dependency "thor"
|
21
|
+
|
22
|
+
s.add_dependency "google_drive", "~> 2.1.7"
|
23
|
+
s.add_dependency "nokogiri"
|
24
|
+
# google_drive dependency to ask for mail and password
|
25
|
+
s.add_dependency "highline"
|
26
|
+
|
27
|
+
# specify version of rack so works on ruby <2.2.2
|
28
|
+
s.add_dependency "rack", "1.6.4"
|
29
|
+
# json support
|
30
|
+
s.add_dependency "json"
|
31
|
+
|
32
|
+
s.add_development_dependency "rake"
|
33
|
+
s.add_development_dependency "test-unit"
|
34
|
+
s.add_development_dependency "simplecov"
|
35
|
+
s.add_development_dependency "yard"
|
36
|
+
|
37
|
+
s.files = `git ls-files`.split("\n")
|
38
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
39
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
40
|
+
s.require_path = 'lib'
|
41
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'babelish_rnc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'babelish_rnc'
|
8
|
+
s.version = BabelishRnc::VERSION
|
9
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
10
|
+
s.summary = "CSV converter for localization files"
|
11
|
+
s.description = "This set of commands converts a CSV file to the following formats:
|
12
|
+
- .strings (iOS)
|
13
|
+
- .xml (Android)
|
14
|
+
- .json
|
15
|
+
- .php"
|
16
|
+
s.authors = ["François Benaiteau", "Markus Paeschke"]
|
17
|
+
s.email = ['francois.benaiteau@gmail.com', 'markus.paeschke@gmail.com']
|
18
|
+
s.homepage = 'https://github.com/rbrovko/babelish_rnc'
|
19
|
+
s.license = 'MIT'
|
20
|
+
|
21
|
+
s.add_dependency "thor"
|
22
|
+
|
23
|
+
s.add_dependency "google_drive", "~> 2.1.7"
|
24
|
+
s.add_dependency "nokogiri"
|
25
|
+
# google_drive dependency to ask for mail and password
|
26
|
+
s.add_dependency "highline"
|
27
|
+
|
28
|
+
# specify version of rack so works on ruby <2.2.2
|
29
|
+
s.add_dependency "rack", "1.6.4"
|
30
|
+
# json support
|
31
|
+
s.add_dependency "json"
|
32
|
+
|
33
|
+
s.add_development_dependency "rake"
|
34
|
+
s.add_development_dependency "test-unit"
|
35
|
+
s.add_development_dependency "simplecov"
|
36
|
+
s.add_development_dependency "yard"
|
37
|
+
|
38
|
+
s.files = `git ls-files`.split("\n")
|
39
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
40
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
41
|
+
s.require_path = 'lib'
|
42
|
+
end
|
data/bin/babelish_rnc
ADDED
data/lib/babelish_rnc.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
# Fixes UTF8 issues
|
4
|
+
# see http://stackoverflow.com/questions/4583924/string-force-encoding-in-ruby-1-8-7-or-rails-2-x
|
5
|
+
class String
|
6
|
+
def to_utf8
|
7
|
+
force_encoding("UTF-8")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# From CSV
|
12
|
+
require "babelish_rnc/csv2base"
|
13
|
+
require "babelish_rnc/csv2strings"
|
14
|
+
require "babelish_rnc/csv2android"
|
15
|
+
require "babelish_rnc/csv2php"
|
16
|
+
require "babelish_rnc/csv2json"
|
17
|
+
|
18
|
+
# To CSV
|
19
|
+
require "babelish_rnc/base2csv"
|
20
|
+
require "babelish_rnc/strings2csv"
|
21
|
+
require "babelish_rnc/android2csv"
|
22
|
+
require "babelish_rnc/php2csv"
|
23
|
+
require "babelish_rnc/json2csv"
|
24
|
+
|
25
|
+
# General
|
26
|
+
require "babelish_rnc/language"
|
27
|
+
require "babelish_rnc/google_doc"
|
28
|
+
|
29
|
+
# iOS specific
|
30
|
+
require "babelish_rnc/xcode_macros"
|
31
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BabelishRnc
|
2
|
+
require "nokogiri"
|
3
|
+
class Android2CSV < Base2Csv
|
4
|
+
|
5
|
+
def initialize(args = {:filenames => []})
|
6
|
+
super(args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def load_strings(strings_filename)
|
10
|
+
strings = {}
|
11
|
+
xml_file = File.open(strings_filename)
|
12
|
+
|
13
|
+
parser = Nokogiri::XML(xml_file) do |config|
|
14
|
+
config.strict.noent
|
15
|
+
end
|
16
|
+
parser.xpath("//string").each do |node|
|
17
|
+
if !node.nil? && !node["name"].nil?
|
18
|
+
strings.merge!(node["name"] => node.inner_html)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
xml_file.close
|
23
|
+
|
24
|
+
[strings, {}]
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module BabelishRnc
|
2
|
+
class Base2Csv
|
3
|
+
attr_accessor :csv_filename, :headers, :filenames, :default_lang
|
4
|
+
|
5
|
+
def initialize(args = {:filenames => []})
|
6
|
+
raise ArgumentError.new("No filenames given") unless args[:filenames]
|
7
|
+
if args[:headers]
|
8
|
+
raise ArgumentError.new("number of headers and files don't match, don't forget the constant column") unless args[:headers].size == (args[:filenames].size + 1)
|
9
|
+
end
|
10
|
+
|
11
|
+
@filenames = args[:filenames]
|
12
|
+
|
13
|
+
@csv_filename = args[:csv_filename] || "translations.csv"
|
14
|
+
@default_lang = args[:default_lang]
|
15
|
+
@headers = args[:headers] || default_headers
|
16
|
+
end
|
17
|
+
|
18
|
+
public
|
19
|
+
|
20
|
+
# Process files and create csv
|
21
|
+
#
|
22
|
+
# @param [Boolean] write_to_file create or not the csv file
|
23
|
+
# @return [Hash] the translations formatted if write_to_file
|
24
|
+
def convert(write_to_file = true)
|
25
|
+
strings = {}
|
26
|
+
keys = nil
|
27
|
+
comments = {}
|
28
|
+
|
29
|
+
@filenames.each do |fname|
|
30
|
+
header = fname
|
31
|
+
strings[header], file_comments = load_strings(fname)
|
32
|
+
keys ||= strings[header].keys
|
33
|
+
comments.merge!(file_comments)
|
34
|
+
end
|
35
|
+
|
36
|
+
if write_to_file
|
37
|
+
# Create csv file
|
38
|
+
puts "Creating #{@csv_filename}"
|
39
|
+
create_csv_file(keys, strings, comments)
|
40
|
+
else
|
41
|
+
return keys, strings
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
# Load all strings of a given file
|
48
|
+
#
|
49
|
+
# @param [String, #read] strings_filename filename of file containing translations
|
50
|
+
# for a given language
|
51
|
+
# @return [Hash] the translations for a given language
|
52
|
+
def load_strings(strings_filename)
|
53
|
+
return [{}, {}]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Give the default headers of csv file
|
57
|
+
#
|
58
|
+
# @return [Array] headers of csv
|
59
|
+
def default_headers
|
60
|
+
headers = ["Variables"]
|
61
|
+
@filenames.each do |fname|
|
62
|
+
headers << fname
|
63
|
+
end
|
64
|
+
headers
|
65
|
+
end
|
66
|
+
|
67
|
+
# Basename of given file
|
68
|
+
#
|
69
|
+
# @param [String, #read] file_path
|
70
|
+
# @return [String] basename
|
71
|
+
def basename(file_path)
|
72
|
+
filename = File.basename(file_path)
|
73
|
+
return filename.split('.')[0].to_sym if file_path
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Create the resulting file
|
79
|
+
#
|
80
|
+
# @param [Array] keys references of all translations
|
81
|
+
# @param [Array] strings translations of all languages
|
82
|
+
# @param [Hash] comments hash containing keys, comments related to each keys, describe the translation
|
83
|
+
def create_csv_file(keys, strings, comments = nil)
|
84
|
+
raise "csv_filename must not be nil" unless @csv_filename
|
85
|
+
CSV.open(@csv_filename, "wb") do |csv|
|
86
|
+
@headers << "Comments" if !comments.nil? && !comments.empty?
|
87
|
+
csv << @headers
|
88
|
+
keys.each do |key|
|
89
|
+
line = [key]
|
90
|
+
default_val = strings[@default_lang][key] if strings[@default_lang]
|
91
|
+
@filenames.each do |fname|
|
92
|
+
lang = fname
|
93
|
+
current_val = (lang != default_lang && strings[lang][key] == default_val) ? '' : strings[lang][key]
|
94
|
+
line << current_val
|
95
|
+
end
|
96
|
+
line << comments[key] if comments && comments[key]
|
97
|
+
csv << line
|
98
|
+
end
|
99
|
+
puts "Done"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|