File: mo.rb

package info (click to toggle)
ruby-fast-gettext 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 720 kB
  • ctags: 301
  • sloc: ruby: 3,092; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'fast_gettext/translation_repository/base'
module FastGettext
  module TranslationRepository
     # Responsibility:
    #  - find and store mo files
    #  - provide access to translations in mo files
    class Mo < Base
      def initialize(name,options={})
        super
        @eager_load = options.fetch(:eager_load, false)
        reload
      end

      def available_locales
        @files.keys
      end

      def pluralisation_rule
        current_translations.pluralisation_rule
      end

      def reload
        find_and_store_files(@name, @options)
        super
      end

      def all_translations
        current_translations.data
      end

      protected

      def find_and_store_files(name,options)
        # parse all .mo files with the right name, that sit in locale/LC_MESSAGES folders
        find_files_in_locale_folders(File.join('LC_MESSAGES',"#{name}.mo"), options[:path]) do |locale,file|
          MoFile.new(file, eager_load: @eager_load)
        end
      end

      def current_translations
        @files[FastGettext.locale] || MoFile.empty
      end
    end
  end
end