Skip to content

Commit

Permalink
Merge 74cfd22 into 4d8f6c1
Browse files Browse the repository at this point in the history
  • Loading branch information
netbe committed Sep 24, 2014
2 parents 4d8f6c1 + 74cfd22 commit fc3e8c1
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 13 deletions.
1 change: 1 addition & 0 deletions .babelish.sample
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ langs: # Languages to convert. i.e. English:en
# - info
# ignore_lang_path: true # does not care about lang component path. i.e: en.lproj/
# sheet: 0 # Index of worksheet to download. First index is 0.
# macros_filename: Babelish.h # File to output the defines of localized strings
3 changes: 3 additions & 0 deletions lib/babelish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ def to_utf8
# General
require "babelish/language"
require "babelish/google_doc"

# iOS specific
require "babelish/xcode_macros"
11 changes: 10 additions & 1 deletion lib/babelish/commandline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class Commandline < Thor
method_option :output_basenames, :type => :array, :aliases => "-o", :desc => "Basename of output files"
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 :sheet, :type => :numeric, :desc => "Index of worksheet to download. First index is 0"
if klass[:name] == "CSV2Strings"
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
Expand Down Expand Up @@ -118,6 +121,7 @@ def csv2base(classname)
args.delete(:langs)
args.delete(:filename)

xcode_macros = Babelish::XcodeMacros.new if options[:macros_filename]
files.each_with_index do |filename, index|
if options[:output_basenames]
args[:output_basename] = options[:output_basenames][index]
Expand All @@ -127,6 +131,11 @@ def csv2base(classname)
args = Thor::CoreExt::HashWithIndifferentAccess.new(args)
converter = class_object.new(filename, options[:langs], args)
say converter.convert
xcode_macros.process(converter.table, converter.keys) if options[:macros_filename]
end
if options[:macros_filename]
say "generating macros"
xcode_macros.write_content(options[:macros_filename])
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/babelish/csv2base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ def create_file_from_path(file_path)
return File.new(file_path, "w")
end

def keys
@languages.each do |lang|
next unless lang
return lang.content.keys unless lang.content.keys.empty?
end
return []
end

def table
output_basename
end

def language_filepaths(language)
#implement in subclass
[]
Expand Down
1 change: 1 addition & 0 deletions lib/babelish/csv2strings.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Babelish
class CSV2Strings < Csv2Base
attr_accessor :languages

def language_filepaths(language)
require 'pathname'
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.3.2'
VERSION = "0.3.2"
end
46 changes: 46 additions & 0 deletions lib/babelish/xcode_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Babelish
class XcodeMacros
attr_accessor :content, :table, :keys
def initialize(table = "Localizable", keys = {})
@content = ""
@table = table
@keys = keys
end

def self.write_macros(file_path, table, keys)
instance = XcodeMacros.new
instance.process(table, keys)
instance.write_content(file_path)
end

def process(table, keys)
keys.each do |key|
macro_name = "LS_#{key.upcase}"
macro_name += "_#{table.upcase}" if table != "Localizable"
macro_name.gsub!(' ', '')
macro_name.gsub!('.', '_')
macro_name.gsub!('-', '_')
@content << String.new(<<-EOS)
#define #{macro_name} NSLocalizedStringFromTable(@"#{key}",@"#{table}",@"")
EOS
end
@content
end

def write_content(file_path)
header = String.new(<<-EOS)
//
// file_path
//
// This file was generated by Babelish
//
// https://github.com/netbe/babelish
//
EOS
header.gsub! "file_path", File.basename(file_path)
file = File.new(file_path, "w")
file.write header + @content
file.close
end
end
end
30 changes: 23 additions & 7 deletions test/babelish/commands/test_command_csv2strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_csv2strings_with_multiple_2_languages
assert File.exist?("./en.lproj/Localizable.strings")
assert File.exist?("./fr.lproj/Localizable.strings")

#clean up
# clean up
system("rm -rf ./en.lproj/")
system("rm -rf ./fr.lproj/")
end
Expand All @@ -28,7 +28,7 @@ def test_csv2strings_with_output_dir
assert File.exist?("./mynewlocation/en.lproj/Localizable.strings"), "can't find output file for English in mynewlocation folder"
assert File.exist?("./mynewlocation/fr.lproj/Localizable.strings"), "can't find output file for French in mynewlocation folder"

#clean up
# clean up
system("rm -rf ./mynewlocation")
end

Expand All @@ -43,7 +43,7 @@ def test_csv2strings_with_fetch_google_doc
Commandline.new([], options).csv2strings
end

#clean up
# clean up
system("rm -rf ./en.lproj/")
system("rm -rf ./fr.lproj/")
end
Expand All @@ -55,7 +55,7 @@ def test_csv2strings_with_config_file
Commandline.new.csv2strings
end

#clean up
# clean up
system("rm -rf ./en.lproj/")
system("rm -rf ./fr.lproj/")
end
Expand All @@ -76,7 +76,7 @@ def test_csv2strings_with_output_basenames_option
assert File.exist?("./en.lproj/sheet2.strings"), "can't find output file for sheet2 English"
assert File.exist?("./fr.lproj/sheet2.strings"), "can't find output file for sheet2 French"

#clean up
# clean up
system("rm -rf ./en.lproj/")
system("rm -rf ./fr.lproj/")
end
Expand All @@ -96,7 +96,7 @@ def test_csv2strings_with_ignore_lang_path_option
assert File.exist?("./sheet1.strings"), "can't find output file for sheet1 English with_ignore_lang_path_option"
assert File.exist?("./sheet2.strings"), "can't find output file for sheet2 English with_ignore_lang_path_option"

#clean up
# clean up
system("rm -f sheet1.strings")
system("rm -f sheet2.strings")
end
Expand All @@ -112,10 +112,26 @@ def test_csv2strings_with_ignore_lang_path_option_local
# testing
assert File.exist?("./Localizable.strings"), "can't find output file for English with_ignore_lang_path_option"

#clean up
# clean up
system("rm -f ./Localizable.strings")
end

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

Commandline.new([], options).csv2strings
# testing
assert File.exist?("./Babelish.h"), "can't find macros file"

# clean up
system("rm -f ./Localizable.strings")
system("rm -f ./Babelish.h")
end

def teardown

system("rm -f .babelish")
Expand Down
15 changes: 15 additions & 0 deletions test/babelish/test_csv2strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ def test_converting_csv_to_dotstrings_default_lang_to_key
system("rm -rf *.lproj")
end

def test_converting_csv_to_dotstrings_keys
expected_output = String.new(<<-EOS)
"GREETINGS" = "Buenos dias";
"ANOTHER_STRING" = "ANOTHER_STRING";
EOS
csv_file = "test/data/test_data_multiple_langs.csv"
spanish_file = "es.lproj/Localizable.strings"
converter = Babelish::CSV2Strings.new(csv_file,
{'English' => [:en], "French" => "fr", "German" => "de", "Spanish" => "es"},
:default_lang => "variables")
converter.convert
assert !converter.keys.empty?
system("rm -rf *.lproj")
end

end
8 changes: 4 additions & 4 deletions test/babelish/test_strings2csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ def test_initialize
assert_equal filenames, converter.filenames
end

def test_initialize_with_default_values
converter = Babelish::Strings2CSV.new
assert_not_nil converter.csv_filename
end
def test_initialize_with_default_values
converter = Babelish::Strings2CSV.new
assert_not_nil converter.csv_filename
end

end
61 changes: 61 additions & 0 deletions test/babelish/test_xcode_macros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "test_helper"
class TestXcodeMacros < Test::Unit::TestCase
def test_process
keys = ["login.title", "login.button"]
table = "Localizable"
xcode_macros = Babelish::XcodeMacros.new
content = xcode_macros.process(table, keys)
expected_output = String.new(<<-EOS)
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
EOS
assert_equal expected_output, content
end

def test_write_macros_class_method
keys = ["login.title", "login.button"]
table = "Localizable"
Babelish::XcodeMacros.write_macros("Babelish.h", table, keys)
expected_output = String.new(<<-EOS)
//
// Babelish.h
//
// This file was generated by Babelish
//
// https://github.com/netbe/babelish
//
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
EOS
assert File.exists?("Babelish.h")
result = File.read("Babelish.h")
assert_equal expected_output, result
#clean up
system("rm -f ./Babelish.h")
end


def test_write_macros_process
keys = ["login.title", "login.button"]
table = "Localizable"
macros = Babelish::XcodeMacros.new
macros.process(table, keys)
macros.write_content("Babelish.h")
expected_output = String.new(<<-EOS)
//
// Babelish.h
//
// This file was generated by Babelish
//
// https://github.com/netbe/babelish
//
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
EOS
assert File.exists?("Babelish.h")
result = File.read("Babelish.h")
assert_equal expected_output, result
#clean up
system("rm -f ./Babelish.h")
end
end

0 comments on commit fc3e8c1

Please sign in to comment.