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
-
.add_repository_to_domain(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) ⇒ Object
private
Add the translations for this project to the domain’s repository chain chain for the currently selected text domain, if needed.
-
.clear_text_domain ⇒ Object
private
Resets the thread’s configured text_domain to the default text domain.
-
.copy_default_translations(domain_name) ⇒ Object
private
Adds translations from the default text domain to the specified text domain.
-
.create_default_text_domain ⇒ Object
private
Creates a default text domain containing the translations for Puppet as the start of chain.
-
.current_locale ⇒ String
private
Returns the currently selected locale from FastGettext, or ‘en’ of gettext has not been loaded.
-
.delete_all_text_domains ⇒ Object
private
Delete all text domains.
-
.delete_environment_text_domains ⇒ Object
private
Deletes all text domains except the default one.
-
.delete_text_domain(domain_name) ⇒ Object
private
Deletes the text domain with the given name.
-
.disable_gettext ⇒ Object
private
Prevent future gettext initializations.
-
.gettext_loaded? ⇒ Boolean
private
Whether we were able to require fast_gettext and locale.
-
.load_translations(project_name, locale_dir, file_format, text_domain = FastGettext.text_domain) ⇒ Object
private
Attempt to load translations for the given project.
-
.loaded_text_domains ⇒ [String]
private
Returns a list of the names of the loaded text domains.
-
.puppet_locale_path ⇒ String
private
Search for puppet gettext config files.
-
.reset_text_domain(domain_name) ⇒ Object
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.
-
.set_locale(locale) ⇒ Object
private
Sets the language in which to display strings.
-
.setup_locale ⇒ Object
private
Sets FastGettext’s locale to the current system locale.
-
.translation_mode(conf_path) ⇒ Symbol
private
Determine which translation file format to use.
-
.use_text_domain(domain_name) ⇒ Object
private
Switches the active text domain, if the requested domain exists.
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.
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/puppet/gettext/config.rb', line 251 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_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.
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.
87 88 89 90 91 |
# File 'lib/puppet/gettext/config.rb', line 87 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.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/puppet/gettext/config.rb', line 178 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_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.
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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/puppet/gettext/config.rb', line 102 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_locale ⇒ 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 the currently selected locale from FastGettext, or ‘en’ of gettext has not been loaded
44 45 46 47 48 49 50 |
# File 'lib/puppet/gettext/config.rb', line 44 def self.current_locale if gettext_loaded? FastGettext.default_locale else 'en' end end |
.delete_all_text_domains ⇒ 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.
Delete all text domains.
132 133 134 135 136 |
# File 'lib/puppet/gettext/config.rb', line 132 def self.delete_all_text_domains FastGettext.translation_repositories.clear FastGettext.default_text_domain = nil FastGettext.text_domain = nil end |
.delete_environment_text_domains ⇒ 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 all text domains except the default one
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/puppet/gettext/config.rb', line 157 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
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/puppet/gettext/config.rb', line 141 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_gettext ⇒ 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.
Prevent future gettext initializations
218 219 220 |
# File 'lib/puppet/gettext/config.rb', line 218 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
36 37 38 |
# File 'lib/puppet/gettext/config.rb', line 36 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.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/puppet/gettext/config.rb', line 228 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) 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
55 56 57 58 59 |
# File 'lib/puppet/gettext/config.rb', line 55 def self.loaded_text_domains return [] if @gettext_disabled || !gettext_loaded? FastGettext.translation_repositories.keys end |
.puppet_locale_path ⇒ 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.
Search for puppet gettext config files
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/puppet/gettext/config.rb', line 192 def self.puppet_locale_path if Puppet::FileSystem.exist?(LOCAL_PATH) LOCAL_PATH elsif Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(WINDOWS_PATH) WINDOWS_PATH elsif !Puppet::Util::Platform.windows? && Puppet::FileSystem.exist?(POSIX_PATH) 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/puppet/gettext/config.rb', line 66 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.
274 275 276 277 278 279 280 281 |
# File 'lib/puppet/gettext/config.rb', line 274 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_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 FastGettext’s locale to the current system locale
265 266 267 268 269 |
# File 'lib/puppet/gettext/config.rb', line 265 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
208 209 210 211 212 213 214 |
# File 'lib/puppet/gettext/config.rb', line 208 def self.translation_mode(conf_path) if WINDOWS_PATH == conf_path || POSIX_PATH == conf_path :mo else :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.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/puppet/gettext/config.rb', line 117 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 |