module Puppet::GettextConfig

Constants

DEFAULT_TEXT_DOMAIN

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

LOCAL_PATH
POSIX_PATH
WINDOWS_PATH

Public Class Methods

add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) click to toggle source

@api private Add the translations for this project to the domain's repository chain chain for the currently selected text domain, if needed. @param [String] project_name the name of the project for which to load translations @param [String] locale_dir the path to the directory containing translations @param [Symbol] file_format the format of the translations files, :po or :mo

    # File lib/puppet/gettext/config.rb
246 def self.add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain)
247   return if @gettext_disabled || !gettext_loaded?
248 
249   current_chain = FastGettext.translation_repositories[text_domain].chain
250 
251   repository = FastGettext::TranslationRepository.build(project_name,
252                                                         path: locale_dir,
253                                                         type: file_format,
254                                                         report_warning: false)
255   current_chain << repository
256 end
clear_text_domain() click to toggle source

@api private 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.

   # File lib/puppet/gettext/config.rb
85 def self.clear_text_domain
86   return if @gettext_disabled || !gettext_loaded?
87   FastGettext.text_domain = nil
88 end
copy_default_translations(domain_name) click to toggle source

@api private 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.

@param [Symbol] domain_name the name of the domain to add translations to

    # File lib/puppet/gettext/config.rb
173 def self.copy_default_translations(domain_name)
174   return if @gettext_disabled || !gettext_loaded?
175 
176   if FastGettext.default_text_domain.nil?
177     create_default_text_domain
178   end
179 
180   puppet_translations = FastGettext.translation_repositories[FastGettext.default_text_domain].chain
181   FastGettext.translation_repositories[domain_name].chain.push(*puppet_translations)
182 end
create_default_text_domain() click to toggle source

@api private 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.

@return true if Puppet translations were successfully loaded, false otherwise

    # File lib/puppet/gettext/config.rb
 99 def self.create_default_text_domain
100   return if @gettext_disabled || !gettext_loaded?
101 
102   FastGettext.add_text_domain(DEFAULT_TEXT_DOMAIN,
103                               type: :chain,
104                               chain: [],
105                               report_warning: false)
106   FastGettext.default_text_domain = DEFAULT_TEXT_DOMAIN
107 
108   load_translations('puppet', puppet_locale_path, translation_mode(puppet_locale_path), DEFAULT_TEXT_DOMAIN)
109 end
current_locale() click to toggle source

@api private Returns the currently selected locale from FastGettext, or 'en' of gettext has not been loaded @return [String] the active locale

   # File lib/puppet/gettext/config.rb
43 def self.current_locale
44   if gettext_loaded?
45     return FastGettext.default_locale
46   else
47     return 'en'
48   end
49 end
delete_all_text_domains() click to toggle source

@api private Delete all text domains.

    # File lib/puppet/gettext/config.rb
128 def self.delete_all_text_domains
129   FastGettext.translation_repositories.clear
130   FastGettext.default_text_domain = nil
131   FastGettext.text_domain = nil
132 end
delete_environment_text_domains() click to toggle source

@api private Deletes all text domains except the default one

    # File lib/puppet/gettext/config.rb
152 def self.delete_environment_text_domains
153   return if @gettext_disabled || !gettext_loaded?
154 
155   FastGettext.translation_repositories.keys.each do |key|
156     # do not clear default translations
157     next if key == DEFAULT_TEXT_DOMAIN
158 
159     FastGettext.translation_repositories.delete(key)
160   end
161   FastGettext.text_domain = nil
162 end
delete_text_domain(domain_name) click to toggle source

@api private Deletes the text domain with the given name @param [String, Symbol] domain_name the name of the domain to delete

    # File lib/puppet/gettext/config.rb
137 def self.delete_text_domain(domain_name)
138   return if @gettext_disabled || !gettext_loaded?
139   domain_name = domain_name.to_sym
140 
141   deleted = FastGettext.translation_repositories.delete(domain_name)
142   if FastGettext.text_domain == domain_name
143     Puppet.debug { "Deleted current text domain #{domain_name.inspect}: #{!deleted.nil?}" }
144     FastGettext.text_domain = nil
145   else
146     Puppet.debug { "Deleted text domain #{domain_name.inspect}: #{!deleted.nil?}" }
147   end
148 end
disable_gettext() click to toggle source

@api private Prevent future gettext initializations

    # File lib/puppet/gettext/config.rb
213 def self.disable_gettext
214   @gettext_disabled = true
215 end
gettext_loaded?() click to toggle source

@api private Whether we were able to require fast_gettext and locale @return [Boolean] true if translation gems were successfully loaded

   # File lib/puppet/gettext/config.rb
35 def self.gettext_loaded?
36   @gettext_loaded
37 end
load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) click to toggle source

@api private Attempt to load translations for the given project. @param [String] project_name the project whose translations we want to load @param [String] locale_dir the path to the directory containing translations @param [Symbol] file_format translation file format to use, either :po or :mo @return true if initialization succeeded, false otherwise

    # File lib/puppet/gettext/config.rb
223 def self.load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain)
224   if project_name.nil? || project_name.empty?
225     raise Puppet::Error, "A project name must be specified in order to initialize translations."
226   end
227 
228   return false if @gettext_disabled || !@gettext_loaded
229 
230   return false unless locale_dir && Puppet::FileSystem.exist?(locale_dir)
231 
232   unless file_format == :po || file_format == :mo
233     raise Puppet::Error, "Unsupported translation file format #{file_format}; please use :po or :mo"
234   end
235 
236   add_repository_to_domain(project_name, locale_dir, file_format, text_domain)
237   return true
238 end
loaded_text_domains() click to toggle source

@api private Returns a list of the names of the loaded text domains @return [[String]] the names of the loaded text domains

   # File lib/puppet/gettext/config.rb
54 def self.loaded_text_domains
55   return [] if @gettext_disabled || !gettext_loaded?
56 
57   return FastGettext.translation_repositories.keys
58 end
puppet_locale_path() click to toggle source

@api private Search for puppet gettext config files @return [String] path to the config, or nil if not found

    # File lib/puppet/gettext/config.rb
187 def self.puppet_locale_path
188   if Puppet::FileSystem.exist?(LOCAL_PATH)
189     return LOCAL_PATH
190   elsif Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(WINDOWS_PATH)
191     return WINDOWS_PATH
192   elsif !Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(POSIX_PATH)
193     return POSIX_PATH
194   else
195     nil
196   end
197 end
reset_text_domain(domain_name) click to toggle source

@api private 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. @param [String, Symbol] domain_name the name of the domain to create

   # File lib/puppet/gettext/config.rb
65 def self.reset_text_domain(domain_name)
66   return if @gettext_disabled || !gettext_loaded?
67   domain_name = domain_name.to_sym
68 
69   Puppet.debug { "Reset text domain to #{domain_name.inspect}" }
70   FastGettext.add_text_domain(domain_name,
71                               type: :chain,
72                               chain: [],
73                               report_warning: false)
74   copy_default_translations(domain_name)
75   FastGettext.text_domain = domain_name
76 end
set_locale(locale) click to toggle source

@api private Sets the language in which to display strings. @param [String] locale the language portion of a locale string (e.g. “ja”)

    # File lib/puppet/gettext/config.rb
269 def self.set_locale(locale)
270   return if @gettext_disabled || !gettext_loaded?
271   # make sure we're not using the `available_locales` machinery
272   FastGettext.default_available_locales = nil
273 
274   FastGettext.default_locale = locale
275 end
setup_locale() click to toggle source

@api private Sets FastGettext's locale to the current system locale

    # File lib/puppet/gettext/config.rb
260 def self.setup_locale
261   return if @gettext_disabled || !gettext_loaded?
262 
263   set_locale(Locale.current.language)
264 end
translation_mode(conf_path) click to toggle source

@api private Determine which translation file format to use @param [String] conf_path the path to the gettext config file @return [Symbol] :mo if in a package structure, :po otherwise

    # File lib/puppet/gettext/config.rb
203 def self.translation_mode(conf_path)
204   if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path
205     return :mo
206   else
207     return :po
208   end
209 end
use_text_domain(domain_name) click to toggle source

@api private Switches the active text domain, if the requested domain exists. @param [String, Symbol] domain_name the name of the domain to switch to

    # File lib/puppet/gettext/config.rb
114 def self.use_text_domain(domain_name)
115   return if @gettext_disabled || !gettext_loaded?
116   domain_name = domain_name.to_sym
117 
118   if FastGettext.translation_repositories.include?(domain_name)
119     Puppet.debug { "Use text domain #{domain_name.inspect}" }
120     FastGettext.text_domain = domain_name
121   else
122     Puppet.debug { "Requested unknown text domain #{domain_name.inspect}" }
123   end
124 end