Sha256: fba048613793a00f35a4b1ee6de809be75439eddcbaa2d2a10407721456a0e56

Contents?: true

Size: 1.5 KB

Versions: 21

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'i18n'

module Intranet
  class Core
    # @!visibility protected
    # Wrapps the initialisation of the i18n internationalization gem.
    module Locales
      # The default path to the locales files.
      DEFAULT_LOCALES_DIR = File.join(__dir__, '..', 'resources', 'locales')

      # Initializes translation units.
      # @param default_locales_dir [String] The path to the directory containing default translation
      #                                     units.
      def self.initialize(default_locales_dir = DEFAULT_LOCALES_DIR)
        I18n.load_path = translation_files(default_locales_dir)
        I18n.available_locales = available_locales(default_locales_dir)
        begin
          I18n.default_locale = ENV['LANG'].split('.')[0].split('_')[0].to_sym
        rescue NoMethodError, I18n::InvalidLocale
          I18n.default_locale = :en
        end
      end

      # Adds a translation units directory.
      # @param dir [String] The path to the directory to add.
      def self.add_path(dir)
        I18n.load_path = Dir.glob(File.join(dir, '*.yml')) + I18n.load_path # prepend load_path
      end

      # private

      def self.translation_files(dir)
        Dir.glob(File.join(dir, '*.yml'))
      end
      private_class_method :translation_files

      def self.available_locales(dir)
        translation_files(dir).map do |file|
          File.basename(file, File.extname(file)).to_sym
        end
      end
      private_class_method :available_locales
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
intranet-core-2.5.0 lib/intranet/core/locales.rb
intranet-core-2.4.5 lib/intranet/core/locales.rb
intranet-core-2.4.4 lib/intranet/core/locales.rb
intranet-core-2.4.3 lib/intranet/core/locales.rb
intranet-core-2.4.1 lib/intranet/core/locales.rb
intranet-core-2.4.0 lib/intranet/core/locales.rb
intranet-core-2.3.3 lib/intranet/core/locales.rb
intranet-core-2.3.2 lib/intranet/core/locales.rb
intranet-core-2.3.1 lib/intranet/core/locales.rb
intranet-core-2.3.0 lib/intranet/core/locales.rb
intranet-core-2.2.0 lib/intranet/core/locales.rb
intranet-core-2.1.4 lib/intranet/core/locales.rb
intranet-core-2.1.2 lib/intranet/core/locales.rb
intranet-core-2.1.1 lib/intranet/core/locales.rb
intranet-core-2.1.0 lib/intranet/core/locales.rb
intranet-core-2.0.0 lib/intranet/core/locales.rb
intranet-core-1.2.0 lib/intranet/core/locales.rb
intranet-core-1.1.1 lib/intranet/core/locales.rb
intranet-core-1.0.2 lib/intranet/core/locales.rb
intranet-core-1.0.1 lib/intranet/core/locales.rb