11require 'puppet/util/logging'
22require 'json'
3+ require 'semantic_puppet/gem_version'
34
45# Support for modules
56class Puppet ::Module
@@ -52,6 +53,32 @@ def self.is_module_namespaced_name?(name)
5253 return false
5354 end
5455
56+ # @api private
57+ def self . parse_range ( range , strict )
58+ @parse_range_method ||= SemanticPuppet ::VersionRange . method ( :parse )
59+ if @parse_range_method . arity == 1
60+ @semver_gem_version ||= SemanticPuppet ::Version . parse ( SemanticPuppet ::VERSION )
61+
62+ # Give user a heads-up if the desired strict setting cannot be honored
63+ if strict
64+ if @semver_gem_version . major < 1
65+ Puppet . warn_once ( 'strict_version_ranges' , 'version_range_cannot_be_strict' ,
66+ _ ( 'VersionRanges will never be strict when using non-vendored SemanticPuppet gem, version %{version}' ) % { version : @semver_gem_version } ,
67+ :default , :default , :notice )
68+ end
69+ else
70+ if @semver_gem_version . major >= 1
71+ Puppet . warn_once ( 'strict_version_ranges' , 'version_range_always_strict' ,
72+ _ ( 'VersionRanges will always be strict when using non-vendored SemanticPuppet gem, version %{version}' ) % { version : @semver_gem_version } ,
73+ :default , :default , :notice )
74+ end
75+ end
76+ @parse_range_method . call ( range )
77+ else
78+ @parse_range_method . call ( range , strict )
79+ end
80+ end
81+
5582 attr_reader :name , :environment , :path , :metadata
5683 attr_writer :environment
5784
@@ -68,6 +95,17 @@ def initialize(name, path, environment, strict_semver = true)
6895 load_metadata
6996
7097 @absolute_path_to_manifests = Puppet ::FileSystem ::PathPattern . absolute ( manifests )
98+
99+ # i18n initialization for modules
100+ if Puppet ::GETTEXT_AVAILABLE
101+ begin
102+ initialize_i18n
103+ rescue Exception => e
104+ Puppet . warning _ ( "GettextSetup initialization for %{module_name} failed with: %{error_message}" ) % { module_name : name , error_message : e . message }
105+ end
106+ else
107+ Puppet . warning _ ( "GettextSetup is not available, skipping GettextSetup initialization for %{module_name}." ) % { module_name : name }
108+ end
71109 end
72110
73111 # @deprecated The puppetversion module metadata field is no longer used.
@@ -328,7 +366,7 @@ def unmet_dependencies
328366
329367 if version_string
330368 begin
331- required_version_semver_range = SemanticPuppet :: VersionRange . parse ( version_string , @strict_semver )
369+ required_version_semver_range = self . class . parse_range ( version_string , @strict_semver )
332370 actual_version_semver = SemanticPuppet ::Version . parse ( dep_mod . version )
333371 rescue ArgumentError
334372 error_details [ :reason ] = :non_semantic_version
@@ -358,8 +396,27 @@ def strict_semver?
358396 @strict_semver
359397 end
360398
399+ def initialize_i18n
400+ module_name = @forge_name . gsub ( "/" , "-" ) if @forge_name
401+ return if module_name . nil? || i18n_initialized? ( module_name )
402+
403+ locales_path = File . absolute_path ( 'locales' , path )
404+
405+ begin
406+ GettextSetup . initialize ( locales_path )
407+ Puppet . debug "#{ module_name } initialized for i18n: #{ GettextSetup . translation_repositories [ module_name ] } "
408+ rescue
409+ config_path = File . absolute_path ( 'config.yaml' , locales_path )
410+ Puppet . debug "Could not find locales configuration file for #{ module_name } at #{ config_path } . Skipping i18n initialization."
411+ end
412+ end
413+
361414 private
362415
416+ def i18n_initialized? ( module_name )
417+ GettextSetup . translation_repositories . has_key? module_name
418+ end
419+
363420 def wanted_manifests_from ( pattern )
364421 begin
365422 extended = File . extname ( pattern ) . empty? ? "#{ pattern } .pp" : pattern
0 commit comments