Skip to content

Commit 85bf8fc

Browse files
committed
(CODEMGMT-725) Externalize error message strings
This commit adds the ruby i18n libraries and externalizes all error messages that the gem throws. When adding or changing translation, the command `bundle exec rake gettext:pot` must be run in order to create 'locales/semantic_puppet.pot' Previously the gemspec required 'semantic_puppet', but that file had to be changed to include the gettext functionality. This caused an error when running any bundle command. The solution is to move the information that the gemspec needs (the version number) into a separate file (gem_version.rb) and include only that file.
1 parent 389bf25 commit 85bf8fc

File tree

9 files changed

+69
-11
lines changed

9 files changed

+69
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ doc
66
.idea/
77
/.project
88
Gemfile.lock
9+
locales/semantic_puppet.pot

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ publishing modules to [Puppet Forge](https://forge.puppetlabs.com) which is
5757
documented at
5858
[Publishing Modules on the Puppet Forge](https://docs.puppetlabs.com/puppet/latest/reference/modules_publishing.html#dependencies-in-metadatajson).
5959

60+
### i18n
61+
62+
When adding new error or log messages please follow the instructions for
63+
[writing translatable code](https://github.com/puppetlabs/gettext-setup-gem#writing-translatable-code).
64+
65+
The SemanticPuppet gem will default to outputing all error messages in English, but you may set the locale
66+
using the `FastGettext.set_locale` method. If translations do not exist for the language you request then the gem will
67+
default to English. The `set_locale` method will return the locale, as a string, that FastGettext will now
68+
use to report PuppetForge errors.
69+
70+
``` ruby
71+
# Assuming the German translations exist, this will set the reporting language
72+
# to German
73+
74+
locale = FastGettext.set_locale "de_DE"
75+
76+
# If it successfully finds Germany's locale, locale will be "de_DE"
77+
# If it fails to find any German locale, locale will be "en"
78+
```
79+
6080
Contributors
6181
------------
6282

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ begin
5656
rescue LoadError
5757
warn "[Warning]: Could not load `bundler/gem_tasks`."
5858
end
59+
60+
# Gettext tasks
61+
begin
62+
spec = Gem::Specification.find_by_name 'gettext-setup'
63+
load "#{spec.gem_dir}/lib/tasks/gettext.rake"
64+
GettextSetup.initialize(File.absolute_path('locales', File.dirname(__FILE__)))
65+
rescue LoadError
66+
warn "[Warning]: Could not load gettext tasks."
67+
end

lib/semantic_puppet.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
require 'gettext-setup'
2+
13
module SemanticPuppet
4+
GettextSetup.initialize(File.absolute_path('../locales', File.dirname(__FILE__)))
5+
26
autoload :Version, 'semantic_puppet/version'
37
autoload :VersionRange, 'semantic_puppet/version_range'
48
autoload :Dependency, 'semantic_puppet/dependency'
5-
6-
VERSION = '0.1.3'
79
end

lib/semantic_puppet/gem_version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SemanticPuppet
2+
VERSION = '0.1.3'
3+
end

lib/semantic_puppet/version.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def parse(ver)
1818
match, major, minor, patch, prerelease, build = *ver.match(/\A#{REGEX_FULL}\Z/)
1919

2020
if match.nil?
21-
raise "Unable to parse '#{ver}' as a semantic version identifier"
21+
raise _("Unable to parse '%{version}' as a semantic version identifier") % {version: ver}
2222
end
2323

2424
prerelease = parse_prerelease(prerelease) if prerelease
2525
# Build metadata is not yet supported in semantic_puppet, but we hope to.
2626
# The following code prevents build metadata for now.
2727
#build = parse_build_metadata(build) if build
2828
if !build.nil?
29-
raise "'#{ver}' MUST NOT include build identifiers"
29+
raise _("'%{version}' MUST NOT include build identifiers") % {version: ver}
3030
end
3131

3232
self.new(major.to_i, minor.to_i, patch.to_i, prerelease, build)
@@ -46,11 +46,11 @@ def parse_prerelease(prerelease)
4646
prerelease = prerelease.split('.', -1)
4747

4848
if prerelease.empty? or prerelease.any? { |x| x.empty? }
49-
raise "#{subject} MUST NOT be empty"
49+
raise _("%{subject} MUST NOT be empty") % {subject: subject}
5050
elsif prerelease.any? { |x| x =~ /[^0-9a-zA-Z-]/ }
51-
raise "#{subject} MUST use only ASCII alphanumerics and hyphens"
51+
raise _("%{subject} MUST use only ASCII alphanumerics and hyphens") % {subject: subject}
5252
elsif prerelease.any? { |x| x =~ /^0\d+$/ }
53-
raise "#{subject} MUST NOT contain leading zeroes"
53+
raise _("%{subject} MUST NOT contain leading zeroes") % {subject: subject}
5454
end
5555

5656
return prerelease.map { |x| x =~ /^\d+$/ ? x.to_i : x }
@@ -61,9 +61,9 @@ def parse_build_metadata(build)
6161
build = build.split('.', -1)
6262

6363
if build.empty? or build.any? { |x| x.empty? }
64-
raise "#{subject} MUST NOT be empty"
64+
raise _("%{subject} MUST NOT be empty") % {subject: subject}
6565
elsif build.any? { |x| x =~ /[^0-9a-zA-Z-]/ }
66-
raise "#{subject} MUST use only ASCII alphanumerics and hyphens"
66+
raise _("%{subject} MUST use only ASCII alphanumerics and hyphens") % {subject: subject}
6767
end
6868

6969
return build

lib/semantic_puppet/version_range.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def parse(range_str)
4646
end
4747

4848
rescue ArgumentError
49-
raise ArgumentError, "Unparsable version range: #{range_str.inspect}"
49+
raise ArgumentError, _("Unparsable version range: %{range}") % {range: range_str.inspect}
5050
end
5151

5252
private

locales/config.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# This is the project-specific configuration file for setting up
3+
# fast_gettext for your project.
4+
gettext:
5+
# This is used for the name of the .pot and .po files; they will be
6+
# called <project_name>.pot?
7+
project_name: 'semantic_puppet'
8+
# This is used in comments in the .pot and .po files to indicate what
9+
# project the files belong to and should bea little more desctiptive than
10+
# <project_name>
11+
package_name: Semantic Puppet Gem
12+
# The locale that the default messages in the .pot file are in
13+
default_locale: en
14+
# The email used for sending bug reports.
15+
bugs_address: [email protected]
16+
# The holder of the copyright.
17+
copyright_holder: Puppet, Inc.
18+
# Patterns for +Dir.glob+ used to find all files that might contain
19+
# translatable content, relative to the project root directory
20+
source_files:
21+
- 'lib/**/*.rb'

semantic_puppet.gemspec

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- encoding: utf-8 -*-
22
$:.push File.expand_path("../lib", __FILE__)
3-
require "semantic_puppet"
3+
require "semantic_puppet/gem_version"
44

55
spec = Gem::Specification.new do |s|
66
# Metadata
@@ -22,6 +22,8 @@ spec = Gem::Specification.new do |s|
2222
# Dependencies
2323
s.required_ruby_version = '>= 1.8.7'
2424

25+
s.add_dependency "gettext-setup", ">= 0.3"
26+
2527
s.add_development_dependency "rake"
2628
s.add_development_dependency "rspec"
2729
s.add_development_dependency "simplecov"

0 commit comments

Comments
 (0)