diff --git a/lib/run_loop.rb b/lib/run_loop.rb index 90282b75..04fbb6b3 100644 --- a/lib/run_loop.rb +++ b/lib/run_loop.rb @@ -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' diff --git a/lib/run_loop/l10n.rb b/lib/run_loop/l10n.rb new file mode 100644 index 00000000..07630734 --- /dev/null +++ b/lib/run_loop/l10n.rb @@ -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 diff --git a/lib/run_loop/xctools.rb b/lib/run_loop/xctools.rb index 07463fdb..480f0d26 100644 --- a/lib/run_loop/xctools.rb +++ b/lib/run_loop/xctools.rb @@ -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 diff --git a/spec/lib/l10n_spec.rb b/spec/lib/l10n_spec.rb new file mode 100644 index 00000000..504dd8a3 --- /dev/null +++ b/spec/lib/l10n_spec.rb @@ -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 diff --git a/spec/lib/xctools_spec.rb b/spec/lib/xctools_spec.rb index d72ff951..ba173b05 100644 --- a/spec/lib/xctools_spec.rb +++ b/spec/lib/xctools_spec.rb @@ -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