Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .babelish.sample
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ langs: # Languages to convert. i.e. English:en

###################################### General ######################################

# csv_separator: ',' # CSV column separator character, uses ',' by default

# csv_separator: ',' # CSV column separator character, uses ',' by default
###################################### JSON ######################################

# pretty_json: false # Prettify your json output files
2 changes: 1 addition & 1 deletion lib/babelish/android2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Babelish
require "nokogiri"
class Android2CSV < Base2Csv

def initialize(args = {:filenames => []})
def initialize(args = {filenames: []})
super(args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/babelish/base2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Babelish
class Base2Csv
attr_accessor :csv_filename, :headers, :filenames, :default_lang

def initialize(args = {:filenames => []})
def initialize(args = {filenames: []})
raise ArgumentError.new("No filenames given") unless args[:filenames]
if args[:headers]
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)
Expand Down
67 changes: 34 additions & 33 deletions lib/babelish/commandline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,59 @@
require 'yaml'
class Commandline < Thor
include Thor::Actions
class_option :verbose, :type => :boolean
class_option :config, :type => :string, :aliases => "-c", :desc => "Read configuration from given file", :default => ".babelish"
class_option :verbose, type: :boolean
class_option :config, type: :string, aliases: "-c", desc: "Read configuration from given file", default: ".babelish"
map "-v" => :version

CSVCLASSES = [
{:name => "CSV2Strings", :ext => ".strings"},
{:name => "CSV2Android", :ext => ".xml"},
{:name => "CSV2JSON", :ext => ".json"},
{:name => "CSV2Php", :ext => ".php"},
{name: "CSV2Strings", ext: ".strings"},
{name: "CSV2Android", ext: ".xml"},
{name: "CSV2JSON", ext: ".json"},
{name: "CSV2Php", ext: ".php"},
]

CSVCLASSES.each do |klass|
desc "#{klass[:name].downcase}", "Convert CSV file to #{klass[:ext]}"
method_option :filename, :type => :string, :aliases => "-i", :desc => "CSV file to convert from or name of file in Google Drive"
method_option :langs, :type => :hash, :aliases => "-L", :desc => "Languages to convert. i.e. English:en"
method_option :filename, type: :string, aliases: "-i", desc: "CSV file to convert from or name of file in Google Drive"
method_option :langs, type: :hash, aliases: "-L", desc: "Languages to convert. i.e. English:en"

# optional options
method_option :excluded_states, :type => :array, :aliases => "-x", :desc => "Exclude rows with given state"
method_option :state_column, :type => :numeric, :aliases => "-s", :desc => "Position of column for state if any"
method_option :keys_column, :type => :numeric, :aliases => "-k", :desc => "Position of column for keys"
method_option :comments_column, :type => :numeric, :aliases => "-C", :desc => "Position of column for comments if any"
method_option :default_lang, :type => :string, :aliases => "-l", :desc => "Default language to use for empty values if any"
method_option :csv_separator, :type => :string, :aliases => "-S", :desc => "CSV column separator character, uses ',' by default"
method_option :output_dir, :type => :string, :aliases => "-d", :desc => "Path of output files"
method_option :output_basenames, :type => :array, :aliases => "-o", :desc => "Basename of output files"
method_option :stripping, :type => :boolean, :aliases => "-N", :default => false, :desc => "Strips values of spreadsheet"
method_option :ignore_lang_path, :type => :boolean, :aliases => "-I", :lazy_default => false, :desc => "Ignore the path component of langs"
method_option :fetch, :type => :boolean, :desc => "Download file from Google Drive"
method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0"
method_option :excluded_states, type: :array, aliases: "-x", desc: "Exclude rows with given state"
method_option :state_column, type: :numeric, aliases: "-s", desc: "Position of column for state if any"
method_option :keys_column, type: :numeric, aliases: "-k", desc: "Position of column for keys"
method_option :comments_column, type: :numeric, aliases: "-C", desc: "Position of column for comments if any"
method_option :default_lang, type: :string, aliases: "-l", desc: "Default language to use for empty values if any"
method_option :csv_separator, type: :string, aliases: "-S", desc: "CSV column separator character, uses ',' by default"
method_option :output_dir, type: :string, aliases: "-d", desc: "Path of output files"
method_option :output_basenames, type: :array, aliases: "-o", desc: "Basename of output files"
method_option :stripping, type: :boolean, aliases: "-N", default: false, desc: "Strips values of spreadsheet"
method_option :ignore_lang_path, type: :boolean, aliases: "-I", lazy_default: false, desc: "Ignore the path component of langs"
method_option :fetch, type: :boolean, desc: "Download file from Google Drive"
method_option :sheet, type: :numeric, desc: "Index of worksheet to download. First index is 0"
method_option :pretty_json, type: :boolean, aliases: "-p", desc: "Prettify your json output files"
if klass[:name] == "CSV2Strings"
method_option :macros_filename, :type => :boolean, :aliases => "-m", :lazy_default => false, :desc => "Filename containing defines of localized keys"
method_option :macros_filename, type: :boolean, aliases: "-m", lazy_default: false, desc: "Filename containing defines of localized keys"
end
define_method("#{klass[:name].downcase}") do
csv2base(klass[:name])
end
end

BASECLASSES = [
{:name => "Strings2CSV", :ext => ".strings"},
{:name => "Android2CSV", :ext => ".xml"},
{:name => "JSON2CSV", :ext => ".json"},
{:name => "Php2CSV", :ext => ".php"},
{name: "Strings2CSV", ext: ".strings"},
{name: "Android2CSV", ext: ".xml"},
{name: "JSON2CSV", ext: ".json"},
{name: "Php2CSV", ext: ".php"},
]

BASECLASSES.each do |klass|
desc "#{klass[:name].downcase}", "Convert #{klass[:ext]} files to CSV file"
method_option :filenames, :type => :array, :aliases => "-i", :desc => "location of strings files (FILENAMES)"
method_option :filenames, type: :array, aliases: "-i", desc: "location of strings files (FILENAMES)"

# optional options
method_option :csv_filename, :type => :string, :aliases => "-o", :desc => "location of output file"
method_option :headers, :type => :array, :aliases => "-h", :desc => "override headers of columns, default is name of input files and 'Variables' for reference"
method_option :dryrun, :type => :boolean, :aliases => "-n", :desc => "prints out content of hash without writing file"
method_option :csv_filename, type: :string, aliases: "-o", desc: "location of output file"
method_option :headers, type: :array, aliases: "-h", desc: "override headers of columns, default is name of input files and 'Variables' for reference"
method_option :dryrun, type: :boolean, aliases: "-n", desc: "prints out content of hash without writing file"
define_method("#{klass[:name].downcase}") do
begin
base2csv(klass[:name])
Expand All @@ -64,10 +65,10 @@ class Commandline < Thor
end

desc "csv_download", "Download Google Spreadsheet containing translations"
method_option :gd_filename, :type => :string, :desc => "File to download from Google Drive."
method_option :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0."
method_option :all, :type => :boolean, :lazy_default => true, :desc => "Download all worksheets to individual csv files."
method_option :output_filename, :type => :string, :desc => "Filepath of downloaded file."
method_option :gd_filename, type: :string, desc: "File to download from Google Drive."
method_option :sheet, type: :numeric, desc: "Index of worksheet to download. First index is 0."
method_option :all, type: :boolean, lazy_default: true, desc: "Download all worksheets to individual csv files."
method_option :output_filename, type: :string, desc: "Filepath of downloaded file."
def csv_download
all = options[:sheet] ? false : options[:all]
filename = options['gd_filename']
Expand Down
10 changes: 5 additions & 5 deletions lib/babelish/csv2base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Csv2Base

def initialize(filename, langs, args = {})
default_args = {
:excluded_states => [],
:state_column => nil,
:keys_column => 0,
:csv_separator => ","
excluded_states: [],
state_column: nil,
keys_column: 0,
csv_separator: ","
}

args = default_args.merge!(args)
Expand Down Expand Up @@ -99,7 +99,7 @@ def convert(name = @csv_filename)
excludedCols = []
defaultCol = 0

CSV.foreach(name, :quote_char => '"', :col_sep => @csv_separator, :row_sep => :auto) do |row|
CSV.foreach(name, quote_char: '"', col_sep: @csv_separator, row_sep: :auto) do |row|

if rowIndex == 0
#check there's at least two columns
Expand Down
7 changes: 6 additions & 1 deletion lib/babelish/csv2json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module Babelish
require 'json'
class CSV2JSON < Csv2Base

def initialize(filename, langs, args = {})
super
@pretty_json = args[:pretty_json]
end

def language_filepaths(language)
require 'pathname'
filename = @output_basename || language.code
Expand All @@ -11,7 +16,7 @@ def language_filepaths(language)
end

def hash_to_output(content = {})
return content.to_json
@pretty_json ? JSON.pretty_generate(content) : content.to_json
end

def extension
Expand Down
2 changes: 1 addition & 1 deletion lib/babelish/json2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Babelish
require 'json'
class JSON2CSV < Base2Csv

def initialize(args = {:filenames => []})
def initialize(args = {filenames: []})
super(args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/babelish/php2csv.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Babelish
class Php2CSV < Base2Csv

def initialize(args = {:filenames => []})
def initialize(args = {filenames: []})
super(args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/babelish/strings2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Strings2CSV < Base2Csv
# actually default_lang = default_filename
attr_accessor :csv_filename, :headers, :filenames, :default_lang

def initialize(args = {:filenames => []})
def initialize(args = {filenames: []})
super(args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/babelish/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Babelish
VERSION = "0.6.5"
VERSION = "0.6.4"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

end
14 changes: 7 additions & 7 deletions test/babelish/commands/test_command_android2csv.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
class TestAndroid2CSVCommand < Test::Unit::TestCase
def test_android2csv
options = {:filenames => ["test/data/android.xml"]}
options = {filenames: ["test/data/android.xml"]}
Commandline.new([], options).android2csv

assert File.exist?("translations.csv")
Expand All @@ -11,7 +11,7 @@ def test_android2csv
end

def test_android2csv_with_dryrun_option
options = {:filenames => ["test/data/android.xml"], :dryrun => true}
options = {filenames: ["test/data/android.xml"], dryrun: true}
Commandline.new([], options).android2csv

assert !File.exist?("translations.csv")
Expand All @@ -21,7 +21,7 @@ def test_android2csv_with_dryrun_option
end

def test_android2csv_with_output_file
options = {:filenames => ["test/data/android.xml"], :csv_filename => "myfile.csv"}
options = {filenames: ["test/data/android.xml"], csv_filename: "myfile.csv"}
# -i, -o
Commandline.new([], options).android2csv

Expand All @@ -32,7 +32,7 @@ def test_android2csv_with_output_file
end

def test_android2csv_with_headers
options = {:filenames => ["test/data/android.xml"], :headers => ["constants", "english"]}
options = {filenames: ["test/data/android.xml"], headers: ["constants", "english"]}
# -i, -h
Commandline.new([], options).android2csv

Expand All @@ -44,9 +44,9 @@ def test_android2csv_with_headers

def test_android2csv_with_two_files
options = {
:filenames => ["test/data/android-en.xml", "test/data/android-fr.xml"],
:headers => %w{Constants English French},
:csv_filename => "enfr.csv"
filenames: ["test/data/android-en.xml", "test/data/android-fr.xml"],
headers: %w{Constants English French},
csv_filename: "enfr.csv"
}
# --filenames, --headers, -o
Commandline.new([], options).android2csv
Expand Down
14 changes: 7 additions & 7 deletions test/babelish/commands/test_command_csv2android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class TestCSV2AndroidCommand < Test::Unit::TestCase

def test_csv2android_with_multiple_2_languages
options = {
:filename => "test/data/test_data_multiple_langs.csv",
:langs => {"English" => "en", "French" => "fr"}
filename: "test/data/test_data_multiple_langs.csv",
langs: {"English" => "en", "French" => "fr"}
}
Commandline.new([], options).csv2android

Expand All @@ -18,8 +18,8 @@ def test_csv2android_with_multiple_2_languages

def test_csv2android_with_multiple_2_languages_with_region
options = {
:filename => "test/data/test_data_multiple_langs.csv",
:langs => {"English" => "en-US", "French" => "fr"}
filename: "test/data/test_data_multiple_langs.csv",
langs: {"English" => "en-US", "French" => "fr"}
}
Commandline.new([], options).csv2android

Expand All @@ -33,9 +33,9 @@ def test_csv2android_with_multiple_2_languages_with_region

def test_csv2android_with_output_dir
options = {
:filename => "test/data/test_data_multiple_langs.csv",
:langs => {"English" => "en", "French" => "fr"},
:output_dir => "mynewlocation"
filename: "test/data/test_data_multiple_langs.csv",
langs: {"English" => "en", "French" => "fr"},
output_dir: "mynewlocation"
}

Commandline.new([], options).csv2android
Expand Down
46 changes: 23 additions & 23 deletions test/babelish/commands/test_command_csv2strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class TestCSV2StringsCommand < Test::Unit::TestCase

def test_csv2strings_with_multiple_2_languages
options = {
:filename => "test/data/test_data_multiple_langs.csv",
:langs => {"English" => "en", "French" => "fr"}
filename: "test/data/test_data_multiple_langs.csv",
langs: {"English" => "en", "French" => "fr"}
}
Commandline.new([], options).csv2strings

Expand All @@ -18,9 +18,9 @@ def test_csv2strings_with_multiple_2_languages

def test_csv2strings_with_output_dir
options = {
:filename => "test/data/test_data_multiple_langs.csv",
:langs => {"English" => "en", "French" => "fr"},
:output_dir => "mynewlocation"
filename: "test/data/test_data_multiple_langs.csv",
langs: {"English" => "en", "French" => "fr"},
output_dir: "mynewlocation"
}
Commandline.new([], options).csv2strings

Expand All @@ -35,9 +35,9 @@ def test_csv2strings_with_output_dir
def test_csv2strings_with_fetch_google_doc
omit if ENV['TRAVIS']
options = {
:filename => "my_strings",
:langs => {"English" => "en", "French" => "fr"},
:fetch => true
filename: "my_strings",
langs: {"English" => "en", "French" => "fr"},
fetch: true
}
assert_nothing_raised do
Commandline.new([], options).csv2strings
Expand All @@ -63,10 +63,10 @@ def test_csv2strings_with_config_file
def test_csv2strings_with_output_basenames_option
omit if ENV['TRAVIS']
options = {
:filename => "my_strings",
:langs => {"English" => "en", "French" => "fr"},
:fetch => true,
:output_basenames => %w(sheet1 sheet2),
filename: "my_strings",
langs: {"English" => "en", "French" => "fr"},
fetch: true,
output_basenames: %w(sheet1 sheet2),
}

Commandline.new([], options).csv2strings
Expand All @@ -84,11 +84,11 @@ def test_csv2strings_with_output_basenames_option
def test_csv2strings_with_ignore_lang_path_option
omit if ENV['TRAVIS']
options = {
:filename => "my_strings",
:langs => {"English" => "en"},
:fetch => true,
:ignore_lang_path => true,
:output_basenames => %w(sheet1 sheet2),
filename: "my_strings",
langs: {"English" => "en"},
fetch: true,
ignore_lang_path: true,
output_basenames: %w(sheet1 sheet2),
}

Commandline.new([], options).csv2strings
Expand All @@ -103,9 +103,9 @@ def test_csv2strings_with_ignore_lang_path_option

def test_csv2strings_with_ignore_lang_path_option_local
options = {
:filename => "test/data/test_data.csv",
:langs => {"English" => "en"},
:ignore_lang_path => true,
filename: "test/data/test_data.csv",
langs: {"English" => "en"},
ignore_lang_path: true,
}

Commandline.new([], options).csv2strings
Expand All @@ -118,9 +118,9 @@ def test_csv2strings_with_ignore_lang_path_option_local

def test_csv2string_with_macros_filename
options = {
:filename => "test/data/test_data.csv",
:macros_filename => "Babelish.h",
:langs => { "English" => "en" }
filename: "test/data/test_data.csv",
macros_filename: "Babelish.h",
langs: { "English" => "en" }
}

Commandline.new([], options).csv2strings
Expand Down
Loading