Module: Yast::I18n

Included in:
Builtins, Client, Module
Defined in:
src/ruby/yast/i18n.rb

Overview

Provides translation wrapper.

Constant Summary

DEFAULT_LOCALE =

if every heuristic fails then use the default for locale

"en_US".freeze

Instance Method Summary collapse

Instance Method Details

#_(str) ⇒ Object

translates given string



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'src/ruby/yast/i18n.rb', line 44

def _(str)
  # no textdomain configured yet
  return str unless @my_textdomain

  found = true
  # Switching textdomain clears gettext caches so avoid it if possible.
  if !@my_textdomain.include?(FastGettext.text_domain) || !key_exist?(str)
    # Set domain where key is defined.
    found = @my_textdomain.any? do |domain|
      FastGettext.text_domain = domain
      key_exist?(str)
    end
  end
  found ? FastGettext::Translation._(str) : str
end

#N_(str) ⇒ Object

No translation, only marks the text to be found by gettext when creating POT file, the text needs to be translated by #_ later.

Examples:

Error messages

begin
  # does not translate, the exception contains the untranslated string,
  # but it's recognized by gettext like normal _()
  raise FooError, N_("Foo failed.")
rescue FooError => e
  # log the original (untranslated) error
  log.error e.message

  # but display translated error to the user,
  # _() does the actual translation
  Popup.Error(_(e.message))
end

Translating Constants

class Foo
  # ERROR_MSG will not be translated, but the string will be found
  # by gettext when creating the POT file
  ERROR_MSG = N_("Something failed")
end

# here the string will be translated using the current locale
puts _(Foo::ERROR_MSG)


87
88
89
# File 'src/ruby/yast/i18n.rb', line 87

def N_(str)
  str
end

#n_(singular, plural, num) ⇒ Object

Gets translation based on number.

Parameters:

  • singular (String)

    text for translators for single value

  • plural (String)

    text for translators for bigger value



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'src/ruby/yast/i18n.rb', line 100

def n_(singular, plural, num)
  # no textdomain configured yet
  return fallback_n_(singular, plural, num) unless @my_textdomain

  # Switching textdomain clears gettext caches so avoid it if possible.
  # difference between _ and n_ is hat we need special cache for plural forms
  found = true
  if !@my_textdomain.include?(FastGettext.text_domain) || !cached_plural_find(singular, plural)
    # Set domain where key is defined.
    found = @my_textdomain.any? do |domain|
      FastGettext.text_domain = domain
      cached_plural_find(singular, plural)
    end
  end
  found ? FastGettext::Translation.n_(singular, plural, num) : fallback_n_(singular, plural, num)
end

#Nn_(*keys) ⇒ Object

No translation, only marks the texts to be found by gettext when creating POT file, the texts need to be translated by #n_ later.



93
94
95
# File 'src/ruby/yast/i18n.rb', line 93

def Nn_(*keys)
  keys
end

#textdomain(domain) ⇒ Object

sets new text domain



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
# File 'src/ruby/yast/i18n.rb', line 14

def textdomain(domain)
  # initialize FastGettext only if the locale directory exists
  return unless File.exist? LOCALE_DIR

  # FastGettext does not track which file/class uses which text domain,
  # it has just single global text domain (the current one),
  # remember the requested text domain here
  # One object can have multiple text domains via multiple Yast.include (bnc#877687).
  @my_textdomain ||= []
  @my_textdomain << domain unless @my_textdomain.include? domain

  # initialize available locales at first use or when the current language is changed
  if FastGettext.available_locales.nil? || current_language != FastGettext.locale
    available = available_locales
    if FastGettext.available_locales != available
      # reload the translations, a new language is available
      FastGettext.translation_repositories.keys.each do |dom|
        FastGettext.add_text_domain(dom, path: LOCALE_DIR)
      end
      FastGettext.available_locales = available
    end

    FastGettext.set_locale current_language
  end

  # add the text domain (only if missing to avoid re-reading translations)
  FastGettext.add_text_domain(domain, path: LOCALE_DIR) unless FastGettext.translation_repositories[domain]
end