diff modules/translate/lib/puppet/functions/translate.rb @ 245:b0f8b88fea5c

Add "translate" module used by MySQL module
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Dec 2019 14:12:10 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/translate/lib/puppet/functions/translate.rb	Sat Dec 21 14:12:10 2019 -0500
@@ -0,0 +1,34 @@
+# @summary A function that calls the _() function in gettext. This is because _ is protected in the puppet language
+#
+# @note
+#   Translate with simple strings:
+#     - Takes in a string and passes it to fast_gettext's _() function. Primarily used for 'marking' a string to be added to a .pot file.
+#   Translate with interpolation:
+#     - Takes in a string and a hash. Please note that variables in the message are wrapped with %{VAR} not ${VAR}.
+#     - The hash contains key value pairs with marker and the variable it will be assigned to.
+#     - The translate module passes it to fast_gettext's _() function. Primarily used for 'marking' a string to be added to a .pot file.
+#
+# @example
+#  fail(translate("Failure message"))
+# @example
+#  fail(translate('message is %{color}'), {'color' => 'green'})
+#
+Puppet::Functions.create_function(:translate) do
+  # @param message Message to translate
+  # @param interpolation_values Optional.
+  # @return [String] translated message.
+  dispatch :translate do
+    param 'String', :message
+    optional_param 'Hash', :interpolation_values
+  end
+
+  def translate(message, interpolation_values = nil)
+    if interpolation_values.nil?
+      _(message)
+    else
+      # convert keys to symbols
+      interpolation_values = Hash[interpolation_values.map { |k, v| [k.to_sym, v] }]
+      _(message) % interpolation_values
+    end
+  end
+end