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