File: internal.rb

package info (click to toggle)
ruby-stringex 2.8.5-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 3,745; makefile: 5
file content (47 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (2)
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
46
47
module Stringex
  module Localization
    module Backend
      class Internal < Base
        DEFAULT_LOCALE = :en

        class << self
          def locale
            defined?(@locale) && @locale ? @locale : default_locale
          end

          def locale=(new_locale)
            @locale = new_locale.to_sym
          end

          def default_locale
            defined?(@default_locale) && @default_locale ? @default_locale : DEFAULT_LOCALE
          end

          def default_locale=(new_locale)
            @default_locale = @locale = new_locale.to_sym
          end

          def with_locale(new_locale, &block)
            original_locale = locale
            self.locale = new_locale
            yield
            self.locale = original_locale
          end

          def translations
            # Set up hash like translations[:en][:transliterations]["é"]
            @translations ||= Hash.new { |k, v| k[v] = Hash.new({}) }
          end

          def store_translations(locale, scope, data)
            self.translations[locale.to_sym][scope.to_sym] = Hash[data.map { |k, v| [k.to_sym, v] }] # Symbolize keys
          end

          def initial_translation(scope, key, locale)
            translations[locale][scope][key.to_sym]
          end
        end
      end
    end
  end
end