Class: RunLoop::L10N

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/l10n.rb

Constant Summary collapse

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'
}

Instance Method Summary collapse

Instance Method Details

#axbundle_path_for_sdk(developer_dir, sdk) ⇒ Object

Xcode 5.1.1 path.


93
94
95
96
97
98
# File 'lib/run_loop/l10n.rb', line 93

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

#is_full_name?(two_letter_country_code) ⇒ Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/run_loop/l10n.rb', line 100

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

#key_name_lookup_table(lang_dir_name) ⇒ Object


104
105
106
107
# File 'lib/run_loop/l10n.rb', line 104

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

#lang_dir(localized_lang) ⇒ Object

maps the ios keyboard localization to a language directory where we can find a key-code -> localized-label mapping


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/run_loop/l10n.rb', line 44

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

#lookup_localization_name(key_code, localized_lang) ⇒ String

Find the localized name for a given key_code

Examples:

lookup_localization_name('delete.key', 'da') => 'Slet'

Parameters:

  • key_code (String)

    the localization signifier, e.g. 'delete.key'

  • localized_lang (String)

    an iso language code returned by calabash ios server

Returns:

  • (String)

    the localized name


13
14
15
16
17
18
# File 'lib/run_loop/l10n.rb', line 13

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_bundle_l10n_pathObject


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/run_loop/l10n.rb', line 72

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