class ChefConfig::Config

def self.guess_internal_locale

available English UTF-8 locale. However, all modern POSIXen should support 'locale -a'.
If there is no 'locale -a' then we return 'en_US.UTF-8' since that is the most commonly

default rather than drop English.
exception that ruby will throw it is more obvious what is broken if we drop UTF-8 by
we will blow up on UTF-8 characters. Between the warn we throw and the Encoding
things like 'svn info' return Japanese and we can't parse them. OTOH, if we pick 'C' then
to do the work to return a non-US UTF-8 locale then we fail inside of providers when
back to 'C' if we do not. The choice of fallback is pick-your-poison. If we try
AIX, etc do not have that locale. We then try to find an English locale and fall
able to use that even if there is no English locale on the server, but Mac, Solaris,
find one that we can use. On Ubuntu systems we should find 'C.UTF-8' and be
to use the 'locale -a' command and search through a list of preferences until we
Chef requires an English-language UTF-8 locale to function properly. We attempt
def self.guess_internal_locale
  # https://github.com/opscode/chef/issues/2181
  # Some systems have the `locale -a` command, but the result has
  # invalid characters for the default encoding.
  #
  # For example, on CentOS 6 with ENV['LANG'] = "en_US.UTF-8",
  # `locale -a`.split fails with ArgumentError invalid UTF-8 encoding.
  cmd = Mixlib::ShellOut.new("locale -a").run_command
  cmd.error!
  locales = cmd.stdout.split
  case
  when locales.include?('C.UTF-8')
    'C.UTF-8'
  when locales.include?('en_US.UTF-8'), locales.include?('en_US.utf8')
    'en_US.UTF-8'
  when locales.include?('en.UTF-8')
    'en.UTF-8'
  else
    # Will match en_ZZ.UTF-8, en_ZZ.utf-8, en_ZZ.UTF8, en_ZZ.utf8
    guesses = locales.select { |l| l =~ /^en_.*UTF-?8$/i }
    unless guesses.empty?
      guessed_locale = guesses.first
      # Transform into the form en_ZZ.UTF-8
      guessed_locale.gsub(/UTF-?8$/i, "UTF-8")
    else
      ChefConfig.logger.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support."
      'C'
    end
  end
rescue
  if ChefConfig.windows?
    ChefConfig.logger.debug "Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else."
  else
    ChefConfig.logger.debug "No usable locale -a command found, assuming you have en_US.UTF-8 installed."
  end
  'en_US.UTF-8'
end