Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract l10n behaviors to class #209

Merged
merged 2 commits into from
Aug 29, 2015
Merged
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
1 change: 1 addition & 0 deletions lib/run_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'run_loop/environment'
require 'run_loop/logging'
require 'run_loop/xcode'
require 'run_loop/l10n'
require 'run_loop/process_terminator'
require 'run_loop/process_waiter'
require 'run_loop/lldb'
Expand Down
107 changes: 107 additions & 0 deletions lib/run_loop/l10n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
module RunLoop
class L10N

# Find the localized name for a given key_code
#
# @example
# lookup_localization_name('delete.key', 'da') => 'Slet'
#
# @param [String] key_code the localization signifier, e.g. 'delete.key'
# @param [String] localized_lang an iso language code returned by calabash ios server
#
# @return [String] the localized name
def lookup_localization_name(key_code, localized_lang)
lookup_table_dir = lang_dir(localized_lang)
return nil unless lookup_table_dir

key_name_lookup_table(lookup_table_dir)[key_code]
end

UIKIT_AXBUNDLE_PATH_CORE_SIM = 'Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/AccessibilityBundles/UIKit.axbundle/'

LANG_CODE_TO_LANG_NAME_MAP = {
'en' => 'English',
'nl' => 'Dutch',
'fr' => 'French',
'de' => 'German',
'es' => 'Spanish',
'it' => 'Italian',
'jp' => 'Japanese'
}

# maps the ios keyboard localization to a language directory where we can
# find a key-code -> localized-label mapping
def lang_dir(localized_lang)
l10n_path = uikit_bundle_l10n_path

## 2 char + _ + sub localization
# en_GB.lproj
lang_dir_name = "#{localized_lang}.lproj".sub('-','_')
if File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end

# 2 char iso language code
# vi.lproj
two_char_country_code = localized_lang.split('-')[0]
lang_dir_name = "#{two_char_country_code}.lproj"
if File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end

# Full name
# e.g. Dutch.lproj
lang_dir_name = "#{LANG_CODE_TO_LANG_NAME_MAP[two_char_country_code]}.lproj"
if is_full_name?(two_char_country_code) &&
File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end
nil
end

def uikit_bundle_l10n_path
developer_dir = xcode.developer_dir
if !developer_dir
nil
else
if xcode.version_gte_6?
File.join(developer_dir, UIKIT_AXBUNDLE_PATH_CORE_SIM)
else
['7.1', '7.0', '6.1'].map do |sdk|
path = axbundle_path_for_sdk(developer_dir, sdk)
if File.exist?(path)
path
else
nil
end
end.compact.first
end
end
end

# Xcode 5.1.1 path.
def axbundle_path_for_sdk(developer_dir, sdk)
File.join(developer_dir,
'Platforms/iPhoneSimulator.platform/Developer/SDKs',
"iPhoneSimulator#{sdk}.sdk",
'System/Library/AccessibilityBundles/UIKit.axbundle')
end

def is_full_name?(two_letter_country_code)
LANG_CODE_TO_LANG_NAME_MAP.has_key?(two_letter_country_code)
end

def key_name_lookup_table(lang_dir_name)
path = File.join(uikit_bundle_l10n_path, lang_dir_name, 'Accessibility.strings')
JSON.parse(`plutil -convert json #{path} -o -`)
end

# @!visibility private
attr_reader :xcode

# @!visibility private
def xcode
@xcode ||= RunLoop::Xcode.new
end
end
end
95 changes: 0 additions & 95 deletions lib/run_loop/xctools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,103 +289,8 @@ def instruments_supports_hyphen_s?(version=instruments(:version))
}.call
end

# Find the localized name for a given key_code
#
# @example
# lookup_localization_name('delete.key', 'da') => 'Slet'
#
# @param [String] key_code the localization signifier, e.g. 'delete.key'
# @param [String] localized_lang an iso language code returned by calabash ios server
#
# @return [String] the localized name
def lookup_localization_name(key_code, localized_lang)
lookup_table_dir = lang_dir(localized_lang)
return nil unless lookup_table_dir

key_name_lookup_table(lookup_table_dir)[key_code]
end

private

UIKIT_AXBUNDLE_PATH_CORE_SIM = 'Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/AccessibilityBundles/UIKit.axbundle/'

LANG_CODE_TO_LANG_NAME_MAP = {
'en' => 'English',
'nl' => 'Dutch',
'fr' => 'French',
'de' => 'German',
'es' => 'Spanish',
'it' => 'Italian',
'jp' => 'Japanese'
}

# maps the ios keyboard localization to a language directory where we can
# find a key-code -> localized-label mapping
def lang_dir(localized_lang)
l10n_path = uikit_bundle_l10n_path

## 2 char + _ + sub localization
# en_GB.lproj
lang_dir_name = "#{localized_lang}.lproj".sub('-','_')
if File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end

# 2 char iso language code
# vi.lproj
two_char_country_code = localized_lang.split('-')[0]
lang_dir_name = "#{two_char_country_code}.lproj"
if File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end

# Full name
# e.g. Dutch.lproj
lang_dir_name = "#{LANG_CODE_TO_LANG_NAME_MAP[two_char_country_code]}.lproj"
if is_full_name?(two_char_country_code) &&
File.exists?(File.join(l10n_path, lang_dir_name))
return lang_dir_name
end
nil
end

def uikit_bundle_l10n_path
developer_dir = xcode.developer_dir
if !developer_dir
nil
else
if xcode.version_gte_6?
File.join(developer_dir, UIKIT_AXBUNDLE_PATH_CORE_SIM)
else
['7.1', '7.0', '6.1'].map do |sdk|
path = axbundle_path_for_sdk(developer_dir, sdk)
if File.exist?(path)
path
else
nil
end
end.compact.first
end
end
end

# Xcode 5.1.1 path.
def axbundle_path_for_sdk(developer_dir, sdk)
File.join(developer_dir,
'Platforms/iPhoneSimulator.platform/Developer/SDKs',
"iPhoneSimulator#{sdk}.sdk",
'System/Library/AccessibilityBundles/UIKit.axbundle')
end

def is_full_name?(two_letter_country_code)
LANG_CODE_TO_LANG_NAME_MAP.has_key?(two_letter_country_code)
end

def key_name_lookup_table(lang_dir_name)
path = File.join(uikit_bundle_l10n_path, lang_dir_name, 'Accessibility.strings')
JSON.parse(`plutil -convert json #{path} -o -`)
end

# @!visibility private
attr_reader :xcode

Expand Down
69 changes: 69 additions & 0 deletions spec/lib/l10n_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
describe RunLoop::L10N do

subject(:l10n) { RunLoop::L10N.new }

describe '#uikit_bundle_l10n_path' do
it 'return value' do
if Resources.shared.core_simulator_env?
stub_env('DEVELOPER_DIR', '/some/xcode/path')
axbundle_path = RunLoop::L10N.const_get('UIKIT_AXBUNDLE_PATH_CORE_SIM')
expected = File.join('/some/xcode/path', axbundle_path)
expect(l10n.send(:uikit_bundle_l10n_path)).to be == expected
else
expect(l10n).to receive(:axbundle_path_for_sdk).at_least(:once).and_return 'path'
expect(File).to receive(:exist?).with('path').at_least(:once).and_return true

expect(l10n.send(:uikit_bundle_l10n_path)).to be == 'path'
end
end
end

describe '#lookup_localization_name' do

subject { l10n.lookup_localization_name('delete.key', localization) }

context 'when using the danish localization' do
let('localization') { 'da' }
it { is_expected.to be == 'Slet' }
end

context 'when using an unknown localization' do
let('localization') { 'not-real' }
it { is_expected.to be == nil }
end
end

describe '#lang_dir' do
subject { l10n.send(:lang_dir, localization) }

context 'existing sub localization' do
let(:localization) { 'en-GB' }
it { is_expected.to be == 'en_GB.lproj' }
end

context 'existing iso localization' do
let(:localization) { 'vi' }
it { is_expected.to be == 'vi.lproj' }
end

context 'specially named localization' do
let(:localization) { 'nl' }
it { is_expected.to be == 'Dutch.lproj' }
end

context 'non-existing sub localization with specially named super-localization' do
let(:localization) { 'en-XX' }
it { is_expected.to be == 'English.lproj' }
end

context 'non-existing sub localization with iso super-localization' do
let(:localization) { 'vi-VN' }
it { is_expected.to be == 'vi.lproj' }
end

context 'unknown localization' do
let(:localization) { 'xx' }
it { is_expected.to be == nil }
end
end
end
65 changes: 0 additions & 65 deletions spec/lib/xctools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,6 @@

let(:xcode) { xctools.send(:xcode) }

describe '#uikit_bundle_l10n_path' do
it 'return value' do
if Resources.shared.core_simulator_env?
stub_env('DEVELOPER_DIR', '/some/xcode/path')
axbundle_path = RunLoop::XCTools.const_get('UIKIT_AXBUNDLE_PATH_CORE_SIM')
expected = File.join('/some/xcode/path', axbundle_path)
expect(xctools.send(:uikit_bundle_l10n_path)).to be == expected
else
expect(xctools).to receive(:axbundle_path_for_sdk).at_least(:once).and_return 'path'
expect(File).to receive(:exist?).with('path').at_least(:once).and_return true

expect(xctools.send(:uikit_bundle_l10n_path)).to be == 'path'
end
end
end

describe '#lookup_localization_name' do

subject { xctools.lookup_localization_name('delete.key', localization) }

context 'when using the danish localization' do
let('localization') { 'da' }
it { is_expected.to be == 'Slet' }
end

context 'when using an unknown localization' do
let('localization') { 'not-real' }
it { is_expected.to be == nil }
end
end

describe '#lang_dir' do
subject { xctools.send(:lang_dir, localization) }

context 'existing sub localization' do
let(:localization) { 'en-GB' }
it { is_expected.to be == 'en_GB.lproj' }
end

context 'existing iso localization' do
let(:localization) { 'vi' }
it { is_expected.to be == 'vi.lproj' }
end

context 'specially named localization' do
let(:localization) { 'nl' }
it { is_expected.to be == 'Dutch.lproj' }
end

context 'non-existing sub localization with specially named super-localization' do
let(:localization) { 'en-XX' }
it { is_expected.to be == 'English.lproj' }
end

context 'non-existing sub localization with iso super-localization' do
let(:localization) { 'vi-VN' }
it { is_expected.to be == 'vi.lproj' }
end

context 'unknown localization' do
let(:localization) { 'xx' }
it { is_expected.to be == nil }
end
end

describe '#instruments' do
it 'checks its arguments' do
expect do
Expand Down