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
@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 245 def self.add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) 246 return if @gettext_disabled || !gettext_loaded? 247 248 current_chain = FastGettext.translation_repositories[text_domain].chain 249 250 repository = FastGettext::TranslationRepository.build(project_name, 251 path: locale_dir, 252 type: file_format, 253 report_warning: false) 254 current_chain << repository 255 end
@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 84 def self.clear_text_domain 85 return if @gettext_disabled || !gettext_loaded? 86 FastGettext.text_domain = nil 87 end
@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 172 def self.copy_default_translations(domain_name) 173 return if @gettext_disabled || !gettext_loaded? 174 175 if FastGettext.default_text_domain.nil? 176 create_default_text_domain 177 end 178 179 puppet_translations = FastGettext.translation_repositories[FastGettext.default_text_domain].chain 180 FastGettext.translation_repositories[domain_name].chain.push(*puppet_translations) 181 end
@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 98 def self.create_default_text_domain 99 return if @gettext_disabled || !gettext_loaded? 100 101 FastGettext.add_text_domain(DEFAULT_TEXT_DOMAIN, 102 type: :chain, 103 chain: [], 104 report_warning: false) 105 FastGettext.default_text_domain = DEFAULT_TEXT_DOMAIN 106 107 load_translations('puppet', puppet_locale_path, translation_mode(puppet_locale_path), DEFAULT_TEXT_DOMAIN) 108 end
@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 42 def self.current_locale 43 if gettext_loaded? 44 return FastGettext.default_locale 45 else 46 return 'en' 47 end 48 end
@api private Delete all text domains.
# File lib/puppet/gettext/config.rb 127 def self.delete_all_text_domains 128 FastGettext.translation_repositories.clear 129 FastGettext.default_text_domain = nil 130 FastGettext.text_domain = nil 131 end
@api private Deletes all text domains except the default one
# File lib/puppet/gettext/config.rb 151 def self.delete_environment_text_domains 152 return if @gettext_disabled || !gettext_loaded? 153 154 FastGettext.translation_repositories.keys.each do |key| 155 # do not clear default translations 156 next if key == DEFAULT_TEXT_DOMAIN 157 158 FastGettext.translation_repositories.delete(key) 159 end 160 FastGettext.text_domain = nil 161 end
@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 136 def self.delete_text_domain(domain_name) 137 return if @gettext_disabled || !gettext_loaded? 138 domain_name = domain_name.to_sym 139 140 deleted = FastGettext.translation_repositories.delete(domain_name) 141 if FastGettext.text_domain == domain_name 142 Puppet.debug { "Deleted current text domain #{domain_name.inspect}: #{!deleted.nil?}" } 143 FastGettext.text_domain = nil 144 else 145 Puppet.debug { "Deleted text domain #{domain_name.inspect}: #{!deleted.nil?}" } 146 end 147 end
@api private Prevent future gettext initializations
# File lib/puppet/gettext/config.rb 212 def self.disable_gettext 213 @gettext_disabled = true 214 end
@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 34 def self.gettext_loaded? 35 @gettext_loaded 36 end
@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 222 def self.load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) 223 if project_name.nil? || project_name.empty? 224 raise Puppet::Error, "A project name must be specified in order to initialize translations." 225 end 226 227 return false if @gettext_disabled || !@gettext_loaded 228 229 return false unless locale_dir && Puppet::FileSystem.exist?(locale_dir) 230 231 unless file_format == :po || file_format == :mo 232 raise Puppet::Error, "Unsupported translation file format #{file_format}; please use :po or :mo" 233 end 234 235 add_repository_to_domain(project_name, locale_dir, file_format, text_domain) 236 return true 237 end
@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 53 def self.loaded_text_domains 54 return [] if @gettext_disabled || !gettext_loaded? 55 56 return FastGettext.translation_repositories.keys 57 end
@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 186 def self.puppet_locale_path 187 if Puppet::FileSystem.exist?(LOCAL_PATH) 188 return LOCAL_PATH 189 elsif Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(WINDOWS_PATH) 190 return WINDOWS_PATH 191 elsif !Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(POSIX_PATH) 192 return POSIX_PATH 193 else 194 nil 195 end 196 end
@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 64 def self.reset_text_domain(domain_name) 65 return if @gettext_disabled || !gettext_loaded? 66 domain_name = domain_name.to_sym 67 68 Puppet.debug { "Reset text domain to #{domain_name.inspect}" } 69 FastGettext.add_text_domain(domain_name, 70 type: :chain, 71 chain: [], 72 report_warning: false) 73 copy_default_translations(domain_name) 74 FastGettext.text_domain = domain_name 75 end
@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 268 def self.set_locale(locale) 269 return if @gettext_disabled || !gettext_loaded? 270 # make sure we're not using the `available_locales` machinery 271 FastGettext.default_available_locales = nil 272 273 FastGettext.default_locale = locale 274 end
@api private Sets FastGettext's locale to the current system locale
# File lib/puppet/gettext/config.rb 259 def self.setup_locale 260 return if @gettext_disabled || !gettext_loaded? 261 262 set_locale(Locale.current.language) 263 end
@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 202 def self.translation_mode(conf_path) 203 if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path 204 return :mo 205 else 206 return :po 207 end 208 end
@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 113 def self.use_text_domain(domain_name) 114 return if @gettext_disabled || !gettext_loaded? 115 domain_name = domain_name.to_sym 116 117 if FastGettext.translation_repositories.include?(domain_name) 118 Puppet.debug { "Use text domain #{domain_name.inspect}" } 119 FastGettext.text_domain = domain_name 120 else 121 Puppet.debug { "Requested unknown text domain #{domain_name.inspect}" } 122 end 123 end