Module: Puppet::GettextConfig Private

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

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

Constant Summary collapse

LOCAL_PATH =

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

File.absolute_path('../../../locales', File.dirname(__FILE__))
POSIX_PATH =

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

File.absolute_path('../../../../../share/locale', File.dirname(__FILE__))
WINDOWS_PATH =

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

File.absolute_path('../../../../../../puppet/share/locale', File.dirname(__FILE__))
DEFAULT_TEXT_DOMAIN =

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

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



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

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.



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

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



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

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



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

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



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

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.



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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”)



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

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



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

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



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

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



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

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