Sha256: ce91e5706fd1e8465cf005baca956d33e8577f721e23e53617398aad8b702b9c

Contents?: true

Size: 1.59 KB

Versions: 25

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8
require 'fedux_org_stdlib/process_environment'
require 'fedux_org_stdlib/shell_language_detector/language'

module FeduxOrgStdlib
  class ShellLanguageDetector
    private

    attr_reader :environment, :default_encoding, :default_language_code, :default_country_code

    public

    def initialize(default_language_code: :en, default_country_code: :gb, default_encoding: :utf_8, environment: ProcessEnvironment.new)
      @default_language_code = default_language_code.to_sym
      @default_country_code  = default_country_code.to_sym
      @default_encoding      = default_encoding.to_sym
      @environment           = environment
    end

    # @param [Array, Symbol, String]  allowed
    #   The languages which are allowed
    #
    # @return [Language]
    #    The detected language or the default language if no allowed language was detected
    def detect(allowed: [], overwrite: [])
      allowed   = Array(allowed).map(&:to_sym)
      overwrite = Array(overwrite).map(&:to_sym)

      lang = environment.fetch 'LANG'
      language_code, country_code, encoding = lang.scan(/([^_-]+)[_-]([^.]+)\.([A-Za-z0-9_-]*)/i).flatten.map(&:downcase).map { |e| e.gsub('-', '_') }.map { |e| e.to_sym if !e.nil? }
      language_code, country_code, encoding = *overwrite if !overwrite.blank?

      return Language.new(language_code: language_code, country_code: country_code, encoding: encoding) if language_code && (allowed.blank? || allowed.include?(language_code))
      Language.new(language_code: default_language_code, country_code: default_country_code, encoding: default_encoding)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.7.8 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.7 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.6 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.5 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.4 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.3 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.2 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.1 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.7.0 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.54 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.53 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.52 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.51 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.50 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.48 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.47 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.46 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.45 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.44 lib/fedux_org_stdlib/shell_language_detector.rb
fedux_org-stdlib-0.6.43 lib/fedux_org_stdlib/shell_language_detector.rb