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/'
UIKIT_AXBUNDLE_PATH_CORE_SIM_XCODE_9 =
"Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/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

#is_full_name?(two_letter_country_code) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/run_loop/l10n.rb', line 81

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



85
86
87
88
# File 'lib/run_loop/l10n.rb', line 85

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



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
71
# File 'lib/run_loop/l10n.rb', line 45

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



73
74
75
76
77
78
79
# File 'lib/run_loop/l10n.rb', line 73

def uikit_bundle_l10n_path
  if xcode.version_gte_90?
    File.join(xcode.developer_dir, UIKIT_AXBUNDLE_PATH_CORE_SIM_XCODE_9)
  else
    File.join(xcode.developer_dir, UIKIT_AXBUNDLE_PATH_CORE_SIM)
  end
end