Module: Puppet::GettextConfig

Defined in:
lib/puppet/gettext/config.rb

Constant Summary collapse

LOCAL_PATH =
File.absolute_path('../../../locales', File.dirname(__FILE__))
POSIX_PATH =
File.absolute_path('../../../../../share/locale', File.dirname(__FILE__))
WINDOWS_PATH =
File.absolute_path('../../../../../../puppet/share/locale', File.dirname(__FILE__))
DEFAULT_TEXT_DOMAIN =

This is the only domain name that won’t be a symbol, making it unique from environments.

'default-text-domain'

Class Method Summary collapse

Class Method Details

.add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add the translations for this project to the domain’s repository chain chain for the currently selected text domain, if needed.

Parameters:

  • project_name (String)

    the name of the project for which to load translations

  • locale_dir (String)

    the path to the directory containing translations

  • file_format (Symbol)

    the format of the translations files, :po or :mo



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/puppet/gettext/config.rb', line 245

def self.add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain)
  return if @gettext_disabled || !gettext_loaded?

  current_chain = FastGettext.translation_repositories[text_domain].chain

  repository = FastGettext::TranslationRepository.build(project_name,
                                                        path: locale_dir,
                                                        type: file_format,
                                                        report_warning: false)
  current_chain << repository
end

.clear_text_domainObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the thread’s configured text_domain to the default text domain. In Puppet Server, thread A may process a compile request that configures a domain, while thread B may invalidate that environment and delete the domain. That leaves thread A with an invalid text_domain selected. To avoid that, clear_text_domain after any processing that needs the non-default text domain.



84
85
86
87
# File 'lib/puppet/gettext/config.rb', line 84

def self.clear_text_domain
  return if @gettext_disabled || !gettext_loaded?
  FastGettext.text_domain = nil
end

.copy_default_translations(domain_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds translations from the default text domain to the specified text domain. Creates the default text domain if one does not exist (this will load Puppet’s translations).

Since we are currently (Nov 2017) vendoring semantic_puppet, in normal flows these translations will be copied along with Puppet’s.

Parameters:

  • domain_name (Symbol)

    the name of the domain to add translations to



172
173
174
175
176
177
178
179
180
181
# File 'lib/puppet/gettext/config.rb', line 172

def self.copy_default_translations(domain_name)
  return if @gettext_disabled || !gettext_loaded?

  if FastGettext.default_text_domain.nil?
    create_default_text_domain
  end

  puppet_translations = FastGettext.translation_repositories[FastGettext.default_text_domain].chain
  FastGettext.translation_repositories[domain_name].chain.push(*puppet_translations)
end

.create_default_text_domainObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a default text domain containing the translations for Puppet as the start of chain. When semantic_puppet gets initialized, its translations are added to this chain. This is used as a cache so that all non-module translations only need to be loaded once as we create and reset environment-specific text domains.

otherwise

Returns:

  • true if Puppet translations were successfully loaded, false



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet/gettext/config.rb', line 98

def self.create_default_text_domain
  return if @gettext_disabled || !gettext_loaded?

  FastGettext.add_text_domain(DEFAULT_TEXT_DOMAIN,
                              type: :chain,
                              chain: [],
                              report_warning: false)
  FastGettext.default_text_domain = DEFAULT_TEXT_DOMAIN

  load_translations('puppet', puppet_locale_path, translation_mode(puppet_locale_path), DEFAULT_TEXT_DOMAIN)
end

.current_localeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the currently selected locale from FastGettext, or ‘en’ of gettext has not been loaded

Returns:

  • (String)

    the active locale



42
43
44
45
46
47
48
# File 'lib/puppet/gettext/config.rb', line 42

def self.current_locale
  if gettext_loaded?
    return FastGettext.default_locale
  else
    return 'en'
  end
end

.delete_all_text_domainsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete all text domains.



127
128
129
130
131
# File 'lib/puppet/gettext/config.rb', line 127

def self.delete_all_text_domains
  FastGettext.translation_repositories.clear
  FastGettext.default_text_domain = nil
  FastGettext.text_domain = nil
end

.delete_environment_text_domainsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes all text domains except the default one



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/puppet/gettext/config.rb', line 151

def self.delete_environment_text_domains
  return if @gettext_disabled || !gettext_loaded?

  FastGettext.translation_repositories.keys.each do |key|
    # do not clear default translations
    next if key == DEFAULT_TEXT_DOMAIN

    FastGettext.translation_repositories.delete(key)
  end
  FastGettext.text_domain = nil
end

.delete_text_domain(domain_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes the text domain with the given name

Parameters:

  • domain_name (String, Symbol)

    the name of the domain to delete



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet/gettext/config.rb', line 136

def self.delete_text_domain(domain_name)
  return if @gettext_disabled || !gettext_loaded?
  domain_name = domain_name.to_sym

  deleted = FastGettext.translation_repositories.delete(domain_name)
  if FastGettext.text_domain == domain_name
    Puppet.debug "Deleted current text domain #{domain_name.inspect}: #{!deleted.nil?}"
    FastGettext.text_domain = nil
  else
    Puppet.debug "Deleted text domain #{domain_name.inspect}: #{!deleted.nil?}"
  end
end

.disable_gettextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prevent future gettext initializations



212
213
214
# File 'lib/puppet/gettext/config.rb', line 212

def self.disable_gettext
  @gettext_disabled = true
end

.gettext_loaded?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether we were able to require fast_gettext and locale

Returns:

  • (Boolean)

    true if translation gems were successfully loaded



34
35
36
# File 'lib/puppet/gettext/config.rb', line 34

def self.gettext_loaded?
  @gettext_loaded
end

.load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attempt to load translations for the given project.

Parameters:

  • project_name (String)

    the project whose translations we want to load

  • locale_dir (String)

    the path to the directory containing translations

  • file_format (Symbol)

    translation file format to use, either :po or :mo

Returns:

  • true if initialization succeeded, false otherwise



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/puppet/gettext/config.rb', line 222

def self.load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain)
  if project_name.nil? || project_name.empty?
    raise Puppet::Error, "A project name must be specified in order to initialize translations."
  end

  return false if @gettext_disabled || !@gettext_loaded

  return false unless locale_dir && Puppet::FileSystem.exist?(locale_dir)

  unless file_format == :po || file_format == :mo
    raise Puppet::Error, "Unsupported translation file format #{file_format}; please use :po or :mo"
  end

  add_repository_to_domain(project_name, locale_dir, file_format, text_domain)
  return true
end

.loaded_text_domains[String]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a list of the names of the loaded text domains

Returns:

  • ([String])

    the names of the loaded text domains



53
54
55
56
57
# File 'lib/puppet/gettext/config.rb', line 53

def self.loaded_text_domains
  return [] if @gettext_disabled || !gettext_loaded?

  return FastGettext.translation_repositories.keys
end

.puppet_locale_pathString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Search for puppet gettext config files

Returns:

  • (String)

    path to the config, or nil if not found



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/puppet/gettext/config.rb', line 186

def self.puppet_locale_path
  if Puppet::FileSystem.exist?(LOCAL_PATH)
    return LOCAL_PATH
  elsif Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(WINDOWS_PATH)
    return WINDOWS_PATH
  elsif !Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(POSIX_PATH)
    return POSIX_PATH
  else
    nil
  end
end

.reset_text_domain(domain_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears the translation repository for the given text domain, creating it if it doesn’t exist, then adds default translations and switches to using this domain.

Parameters:

  • domain_name (String, Symbol)

    the name of the domain to create



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/gettext/config.rb', line 64

def self.reset_text_domain(domain_name)
  return if @gettext_disabled || !gettext_loaded?
  domain_name = domain_name.to_sym

  Puppet.debug "Reset text domain to #{domain_name.inspect}"
  FastGettext.add_text_domain(domain_name,
                              type: :chain,
                              chain: [],
                              report_warning: false)
  copy_default_translations(domain_name)
  FastGettext.text_domain = domain_name
end

.set_locale(locale) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the language in which to display strings.

Parameters:

  • locale (String)

    the language portion of a locale string (e.g. “ja”)



268
269
270
271
272
273
274
# File 'lib/puppet/gettext/config.rb', line 268

def self.set_locale(locale)
  return if @gettext_disabled || !gettext_loaded?
  # make sure we're not using the `available_locales` machinery
  FastGettext.default_available_locales = nil

  FastGettext.default_locale = locale
end

.setup_localeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets FastGettext’s locale to the current system locale



259
260
261
262
263
# File 'lib/puppet/gettext/config.rb', line 259

def self.setup_locale
  return if @gettext_disabled || !gettext_loaded?

  set_locale(Locale.current.language)
end

.translation_mode(conf_path) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine which translation file format to use

Parameters:

  • conf_path (String)

    the path to the gettext config file

Returns:

  • (Symbol)

    :mo if in a package structure, :po otherwise



202
203
204
205
206
207
208
# File 'lib/puppet/gettext/config.rb', line 202

def self.translation_mode(conf_path)
  if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path
    return :mo
  else
    return :po
  end
end

.use_text_domain(domain_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Switches the active text domain, if the requested domain exists.

Parameters:

  • domain_name (String, Symbol)

    the name of the domain to switch to



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/gettext/config.rb', line 113

def self.use_text_domain(domain_name)
  return if @gettext_disabled || !gettext_loaded?
  domain_name = domain_name.to_sym

  if FastGettext.translation_repositories.include?(domain_name)
    Puppet.debug "Use text domain #{domain_name.inspect}"
    FastGettext.text_domain = domain_name
  else
    Puppet.debug "Requested unknown text domain #{domain_name.inspect}"
  end
end