Module: Beaker::I18nHelper

Includes:
DSL, DSL::Helpers::FacterHelpers
Defined in:
lib/beaker/i18n_helper.rb,
lib/beaker/i18n_helper/version.rb

Overview

Beaker i18n Helper

Constant Summary collapse

VERSION =
'1.1.0'.freeze

Instance Method Summary collapse

Instance Method Details

#change_locale_on(hsts, lang) ⇒ nil

Changes the locale on each host to the given language

Parameters:

  • hsts (Array)

    beaker hosts array

  • lang (String)

    the POSIX locale identifier as a string, e.g. ‘ja_JP.utf8’

Returns:

  • (nil)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/beaker/i18n_helper.rb', line 66

def change_locale_on(hsts, lang)
  valid_lang_string?(lang)
  Array(hsts).each do |host|
    begin
      host.clear_env_var('LANG')
      host.add_env_var('LANG', lang)
      host.clear_env_var('LANGUAGE')
      host.add_env_var('LANGUAGE', lang)
    rescue RuntimeError
      raise "Unable to change locale to #{lang} on #{host}"
    end
  end
end

#install_language_on(hsts, lang) ⇒ nil

Installs the language pack on each host for a given language (if necessary)

Parameters:

  • hsts (Array)

    beaker hosts array

  • lang (String)

    the POSIX locale identifier as a string, e.g. ‘ja_JP.utf8’

Returns:

  • (nil)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/beaker/i18n_helper.rb', line 42

def install_language_on(hsts, lang)
  valid_lang_string?(lang)
  lang = parse_lang(lang)

  Array(hsts).each do |host|
    begin
      case fact('operatingsystem')
      when 'Debian'
        install_locales_all_on(host)
      when 'Ubuntu'
        install_language_pack_on(host, lang)
      end
    rescue RuntimeError
      raise "Unable to install language pack for #{lang[0]} on #{host}"
    end
  end
end

#parse_lang(lang) ⇒ Array

Parses a given POSIX locale identifier into parts for the other functions to consume

Parameters:

  • lang (String)

    the identifier as a string, e.g. ‘ja_JP.utf8’

Returns:

  • (Array)

    the identifier as an array of [lang,region,charset]



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/beaker/i18n_helper.rb', line 24

def parse_lang(lang)
  lang = lang.split('.')[0] if lang =~ %r{\.\S}

  if lang =~ %r{_}
    lang = lang.split('_')
  elsif lang =~ %r{-}
    lang = lang.split('-')
  end

  lang
end

#valid_lang_string?(lang) ⇒ Boolean

Validates POSIX locale identifier format

Parameters:

  • lang (String)

    the identifier as a string, e.g. ‘ja_JP.utf8’

Returns:

  • (Boolean)

    true if it’s a valid identifier



13
14
15
16
17
# File 'lib/beaker/i18n_helper.rb', line 13

def valid_lang_string?(lang)
  lang_regex = %r{^[a-zA-Z][a-zA-Z](\_|\-)[a-zA-Z][a-zA-Z](\..*)?$}
  raise "Please use correct format for lang: #{lang_regex}" unless lang.match lang_regex
  true
end