talk_like_a_pirate 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: db932721c1e5caeccac58345b7379689344ef469
4
- data.tar.gz: d2dd475fec4e4ed4a7728e0996f0ed236f7f56bf
2
+ SHA256:
3
+ metadata.gz: b944c0e195a2399e63c0d1465b6dd3ff7067cab4bfb5d585cba2d1dc46252fe2
4
+ data.tar.gz: 4668368266d7e559ff0f89949f16314e495f944415056fac7628aa2128190862
5
5
  SHA512:
6
- metadata.gz: 15454681ef634d24030283d78ca7078c0c33298c261a79983641c29b32ab3129794164b3ac14e518e2e9e7c56ac23c3c618861ffa8367bfed4b01548f2635d6b
7
- data.tar.gz: 4cd7aa50c037cacf5c4de9fae228d28d381c63528afeb461430e4f319f606f0ba7a3d19f3155a7eb27a28b53d3e5af3366f3879ff31fb0556abf8c2eb7cd3241
6
+ metadata.gz: 04baa81e63b7bda7d53de3564b9549894ffeaf0816885082944d8d0b16dd620dbf5c96034e716ba2eddedc84943b49fa7604623d6ec95c05417dc32a4239bb35
7
+ data.tar.gz: e43391d9ecea0bd54e3314ce2f4c1e0e6c5af5d9992cfb98699ad02473c359ff80168ed0059ee49209c12ed11b22370bd4cbc5133da436c91dff4c7a716eff67
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gemfile.lock
2
+ gemfiles/*
3
+ log
4
+ *.gem
data/Appraisals ADDED
@@ -0,0 +1,31 @@
1
+ appraise "rails-3-2" do
2
+ gem "activesupport", "~> 3.2.0"
3
+ end
4
+
5
+ appraise "rails-4-0" do
6
+ gem "activesupport", "~> 4.0.0"
7
+ end
8
+
9
+ appraise "rails-4-1" do
10
+ gem "activesupport", "~> 4.1.0"
11
+ end
12
+
13
+ appraise "rails-4-2" do
14
+ gem "activesupport", "~> 4.2.0"
15
+ end
16
+
17
+ appraise "rails-5-0" do
18
+ gem "activesupport", "~> 5.0.0"
19
+ end
20
+
21
+ appraise "rails-5-1" do
22
+ gem "activesupport", "~> 5.1.0"
23
+ end
24
+
25
+ appraise "rails-5-2" do
26
+ gem "activesupport", "~> 5.2.0"
27
+ end
28
+
29
+ appraise "rails-6-0" do
30
+ gem "activesupport", "~> 6.0.0"
31
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ## [0.2.1] - 2018-10-01
2
+ ### Added
3
+ - rake pirate:initialize installs a sample YAML file into your Rails config folder
4
+ - Specs for I18n generation
5
+
6
+ ### Changed
7
+ - I18n generation now works with YAML files with prefixes (i.e. devise.en.yml)
8
+
9
+ ## [0.2.0] - 2018-10-01
10
+ ### Added
11
+ - Now compatible with Rails 3.2 through 5.2.X
12
+ - Specs for all supported Rails versions
13
+
14
+ ### Changed
15
+ - Class public methods
data/Gemfile CHANGED
@@ -1,7 +1,2 @@
1
1
  source 'https://rubygems.org'
2
- group :test do
3
- gem "rspec"
4
- gem "rake"
5
- gem 'activesupport', '~> 3.0.0', :require => false
6
- gem 'i18n'#, :require => false
7
- end
2
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,24 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ talk_like_a_pirate (0.2.1)
5
+ activesupport (>= 3.0.0, < 6.0.1)
6
+ i18n
7
+
1
8
  GEM
2
9
  remote: https://rubygems.org/
3
10
  specs:
4
- activesupport (3.0.20)
5
- diff-lcs (1.2.4)
6
- i18n (0.6.4)
7
- rake (10.0.4)
8
- rspec (2.13.0)
9
- rspec-core (~> 2.13.0)
10
- rspec-expectations (~> 2.13.0)
11
- rspec-mocks (~> 2.13.0)
12
- rspec-core (2.13.1)
13
- rspec-expectations (2.13.0)
14
- diff-lcs (>= 1.1.3, < 2.0)
15
- rspec-mocks (2.13.1)
11
+ activesupport (6.0.0)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ zeitwerk (~> 2.1, >= 2.1.8)
17
+ appraisal (2.2.0)
18
+ bundler
19
+ rake
20
+ thor (>= 0.14.0)
21
+ concurrent-ruby (1.1.5)
22
+ diff-lcs (1.3)
23
+ i18n (1.7.0)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.12.2)
26
+ rake (10.5.0)
27
+ rspec (3.9.0)
28
+ rspec-core (~> 3.9.0)
29
+ rspec-expectations (~> 3.9.0)
30
+ rspec-mocks (~> 3.9.0)
31
+ rspec-core (3.9.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-expectations (3.9.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-mocks (3.9.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-support (3.9.0)
40
+ thor (0.20.3)
41
+ thread_safe (0.3.6)
42
+ tzinfo (1.2.5)
43
+ thread_safe (~> 0.1)
44
+ zeitwerk (2.2.0)
16
45
 
17
46
  PLATFORMS
18
47
  ruby
19
48
 
20
49
  DEPENDENCIES
21
- activesupport (~> 3.0.0)
22
- i18n
23
- rake
24
- rspec
50
+ appraisal (~> 2.2)
51
+ bundler (~> 1.9)
52
+ rake (~> 10.0)
53
+ rspec (~> 3)
54
+ talk_like_a_pirate!
55
+
56
+ BUNDLED WITH
57
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Steve Hodges
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  Ahoy, mateys!
2
2
  =====
3
3
 
4
- Delight yer users with the flavor of the high seas!
4
+ Translate English to Pirate on the fly! Delight yer users with the flavor of the high seas!
5
5
 
6
- Add a pirate translation layer to your Ruby or Rails app! Talk, like a Pirate!
6
+ Plus, add a pirate translation layer to your Ruby or Rails app! Talk, like a Pirate!
7
7
 
8
- Try the gem out on [Ahoy Mat.ee](http://www.ahoymat.ee "Translate web pages into pirate"), which allows you to translate any page on the public internets.
8
+ This gem has been functional tested against a public domain version of The Bible (which may or may not be available as an e-book on Amazon.com...)
9
+
10
+ [![Gem Version](https://badge.fury.io/rb/talk_like_a_pirate.svg)](https://badge.fury.io/rb/talk_like_a_pirate)
9
11
 
10
- Translate English to Pirate on the fly!
11
12
  ----
12
13
 
13
14
  TalkLikeAPirate.translate("Today is a good day to die")
@@ -25,12 +26,20 @@ It'll iterate through all of your en.yml files in that location and build a pira
25
26
 
26
27
  Config
27
28
  ----
28
- The pirate dictionary is fairly generic. You my have domain-specific lingo you think would be fucking hillarious in pirate. So, add on to the dictionary!
29
+ The pirate dictionary is fairly generic. You may have domain-specific lingo you think would be hillarious in pirate. So, add on to the dictionary!
30
+
31
+ You can generate a config file using the rake task:
32
+
33
+ rake pirate:initialize
29
34
 
30
- Add a config file at
35
+ Or, add a config file at
31
36
 
37
+ * Rails:
32
38
  config/pirate_booty.yml
33
39
 
40
+ * Without Rails
41
+ Specify the path to your YAML file in ENV['TALK_LIKE_A_PIRATE_CONFIG_PATH']
42
+
34
43
  And format it like so (all keys are optional):
35
44
 
36
45
  locale: pirate # This defaults to arr, but you can override it if you'd prefer a different locale string!
@@ -52,15 +61,24 @@ If you want more sophistication in your translations, you can add the Rails Acti
52
61
  require "active_support"
53
62
  require "active_support/inflector"
54
63
 
55
- And, due to dependency weirdness in ActiveSupport, you'll also need the i18n gem installed. So, in your Gemfile:
56
-
57
- gem 'activesupport', '~> 3.0.0'
58
- gem 'i18n'
59
-
60
64
  Contributions
61
65
  =====
62
66
  Feel free to write specs, add to the standard dictionary, etc. Submit a pull request and we'll see what happens!
63
67
 
68
+ Tests
69
+ =====
70
+
71
+ To test the gem against the current version of ActiveSupport (in [Gemfile.lock](Gemfile.lock)):
72
+
73
+ 1. `bundle install`
74
+ 2. `bundle exec rspec`
75
+
76
+ Or, you can run tests for all supported Rails versions
77
+
78
+ 1. `gem install appraisal`
79
+ 1. `bundle exec appraisal install` *(this Generates gemfiles for all permutations of our dependencies, so you'll see lots of bundler output))*
80
+ 1. `bundle exec appraisal rspec`. *(This runs rspec for each dependency permutation. If one fails, appraisal exits immediately and does not test permutations it hasn't gotten to yet. Tests are not considered passing until all permutations are passing)*
81
+
64
82
  Credits
65
83
  =====
66
84
  Copyright (c) 2013, developed and maintained by Steve Hodges, and is released under the open MIT Licence
File without changes
@@ -0,0 +1,9 @@
1
+ dictionary:
2
+ # Capitalization will be handled by talk_like_a_pirate.
3
+ computer: voodoo
4
+ bill: debt
5
+ policies: creeds
6
+ inventory: treasure chest
7
+
8
+ pirate_flavor: # the flavoring occasionally added to the ends of sentences
9
+ - "T' Davy Jones' locker wit ya"
@@ -0,0 +1,48 @@
1
+ class TalkLikeAPirate
2
+ class InstallLocalConfig
3
+ class << self
4
+ def install(target_file_path=nil)
5
+ target_file_path = set_target_file_path(target_file_path)
6
+ if File.exists?(target_file_path)
7
+ "Configuration file already exists at #{target_file_path}"
8
+ else
9
+ copy_local_config_template_to(target_file_path)
10
+ output_local_instructions(target_file_path)
11
+ end
12
+ end
13
+
14
+ private #####################################################################
15
+
16
+ def set_target_file_path(target_file_path)
17
+ if TalkLikeAPirate.on_rails?
18
+ Rails.root.join 'config', 'pirate_booty.yml'
19
+ else
20
+ target_file_path = path_looks_like_file(target_file_path) ? FileUtils.pwd : target_file_path
21
+ File.join target_file_path, 'pirate_booty.yml'
22
+ end
23
+ end
24
+
25
+ def copy_local_config_template_to(target_file_path)
26
+ target_directory = File.dirname(target_file_path)
27
+ FileUtils.mkdir_p(target_directory) unless Dir.exists?(target_directory)
28
+ source_path = File.join File.dirname(__FILE__), 'config', 'sample_config.yml'
29
+
30
+ FileUtils.copy source_path, target_file_path
31
+ end
32
+
33
+ def output_local_instructions(target_file_path)
34
+ return "Configuration installed at #{target_file_path}" if TalkLikeAPirate.on_rails?
35
+ <<-INSTRUCTIONS
36
+ ********************************************************************************************
37
+ Be sure to specify the path to your YAML file in ENV['TALK_LIKE_A_PIRATE_CONFIG_PATH'], i.e.
38
+ export TALK_LIKE_A_PIRATE_CONFIG_PATH="#{target_file_path}"
39
+ ********************************************************************************************
40
+ INSTRUCTIONS
41
+ end
42
+
43
+ def path_looks_like_file(target_file_path)
44
+ target_file_path =~ /\.[^\.\/]*\z/
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ class TalkLikeAPirate
2
+ class I18nTranslator
3
+ def initialize(from_locale: 'en', verbose: false)
4
+ @source_locale = from_locale
5
+ @verbose = verbose
6
+ end
7
+
8
+ def translate_file_or_directory(path='config/locales')
9
+ path = Rails.root.join(path) if TalkLikeAPirate.on_rails? && !path.include?(Rails.root.to_s)
10
+ return translate(path) if File.exists?(path) && file_is_source_yaml_file?(path)
11
+
12
+ directory_contents(path).each do |file_path|
13
+ process_file_or_directory file_path
14
+ end
15
+ end
16
+ alias :translate :translate_file_or_directory
17
+
18
+ private #####################################################################
19
+
20
+ def process_file_or_directory(file_path)
21
+ if File.directory? file_path
22
+ source_path = File.join(file_path, "#{@source_locale}.yml")
23
+ translate_file(source_path) if File.exists?(source_path)
24
+ translate_file_or_directory file_path
25
+ elsif file_is_source_yaml_file?(file_path)
26
+ translate_file file_path
27
+ end
28
+ end
29
+
30
+ def file_is_source_yaml_file?(file_path)
31
+ file_path.to_s.match source_yaml_file_matcher
32
+ end
33
+
34
+ def source_yaml_file_matcher
35
+ # prefix_with_dot locale_name sublocale_name .yml
36
+ /([^\/]*\.)?#{@source_locale}([a-zA-Z-]*).yml/
37
+ end
38
+
39
+ def translate_file(source_file_path)
40
+ pirate_locale_name = pirate_locale_name_for(source_file_path)
41
+ target_file_path = File.join File.dirname(source_file_path), "#{source_filename_prefix(source_file_path)}#{pirate_locale_name}.yml"
42
+ puts "Translatin' #{source_file_path} to #{pirate_locale_name}.yml" if @verbose
43
+
44
+ en_yml = YAML::load_file(source_file_path)
45
+ translation = {pirate_locale_name => parse_element(en_yml).values.first}
46
+ File.open(target_file_path, 'w:utf-8'){|f| YAML::dump translation, f }
47
+ end
48
+
49
+ def pirate_locale_name_for(source_filename)
50
+ TalkLikeAPirate.pirate_locale + source_filename.match(source_yaml_file_matcher)[2]
51
+ end
52
+
53
+ def source_filename_prefix(source_filename)
54
+ source_filename.match(source_yaml_file_matcher)[1]
55
+ end
56
+
57
+ def parse_element(element)
58
+ case element
59
+ when Hash
60
+ new_hash = {}
61
+ element.each{|k,v| new_hash[k] = parse_element(v)}
62
+ new_hash
63
+ when Array
64
+ element.map{|el| parse_element(element)}
65
+ when String
66
+ TalkLikeAPirate.translate element
67
+ else
68
+ element
69
+ end
70
+ end
71
+
72
+ def directory_contents(path)
73
+ Dir.new(path).entries.map do |filename|
74
+ File.join(path, filename) unless ['.', '..'].include?(filename)
75
+ end.compact
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'i18n_translator'
2
+ require_relative '../install_local_config'
3
+
4
+ class LoadTasks < Rails::Railtie
5
+ rake_tasks do
6
+
7
+ namespace 'pirate' do
8
+ desc 'Translate config/locales dictionary into pirrrrate'
9
+ task :translate, [:dir_or_file_to_translate] => :environment do |t, args|
10
+ args.with_defaults(dir_or_file_to_translate: 'config/locales')
11
+ TalkLikeAPirate::I18nTranslator.new.translate args[:dir_or_file_to_translate]
12
+ end
13
+ end
14
+
15
+ namespace 'pirate' do
16
+ desc 'Add a pirate config file to enable customization of the translation dictionary'
17
+ task :initialize, [:target_path] => :environment do |t, args|
18
+ message = TalkLikeAPirate::InstallLocalConfig.install(args[:target_path])
19
+ puts "#{message}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,126 +1,145 @@
1
1
  class TalkLikeAPirate
2
2
 
3
- def self.translate(me_string)
4
- me_string.is_a?(String) ? build_string(me_string) : me_string
5
- end
3
+ class << self
4
+ def translate(me_string)
5
+ me_string.is_a?(String) ? build_string(me_string) : me_string
6
+ end
6
7
 
7
- private #####################################################################
8
+ def pirate_locale
9
+ @@locale ||= local_config['locale'] || config['locale']
10
+ end
8
11
 
9
- def self.build_string(me_string)
10
- sentence = translate_string(me_string)
11
- if sentence.split(" ").length > 5 && sentence.match(/[!.?]\Z/) && rand(5) == 0
12
- sentence = prepare_original_sentence(sentence) + " " + build_piratey_sentence
12
+ def on_rails?
13
+ Object.const_defined?(:Rails)
13
14
  end
14
- sentence
15
- end
16
15
 
17
- def self.translate_string(me_string)
18
- me_string.split(/ /).map do |word|
16
+ private #####################################################################
19
17
 
20
- leading_punctuation, word, trailing_punctuation = extract_punctuation word
21
- capitalized = (word.slice(0,1) == word.slice(0,1).upcase)
22
- fully_capitalized = (word == word.upcase)
18
+ def build_string(me_string)
19
+ sentence = translate_string(me_string)
20
+ if sentence.split(" ").length > 5 && sentence.match(/[!.?]\Z/) && rand(5) == 0
21
+ sentence = prepare_original_sentence(sentence) + " " + build_piratey_sentence
22
+ end
23
+ sentence
24
+ end
23
25
 
24
- word = Object.const_defined?(:ActiveSupport) ? piratize_with_pluralization(word.downcase) : piratize(word.downcase)
26
+ def translate_string(me_string)
27
+ me_string.split(/ /).map do |word|
25
28
 
26
- word = capitalize_first(word) if capitalized
27
- word = word.upcase if fully_capitalized
28
- "#{leading_punctuation}#{word}#{trailing_punctuation}"
29
- end.join(" ")
30
- end
29
+ leading_punctuation, word, trailing_punctuation = extract_punctuation word
30
+ capitalized = (word.slice(0,1) == word.slice(0,1).upcase)
31
+ fully_capitalized = (word == word.upcase)
32
+
33
+ word = Object.const_defined?(:ActiveSupport) ? piratize_with_pluralization(word.downcase) : piratize(word.downcase)
31
34
 
32
- def self.piratize_with_pluralization(word)
33
- pluralized = pluralize(word) == word
34
- if dictionary.has_key? singularize(word)
35
- word = dictionary[singularize(word)]
36
- pluralized ? pluralize(word) : word
37
- else
38
- translate_if_gerund word
35
+ word = capitalize_first(word) if capitalized
36
+ word = word.upcase if fully_capitalized
37
+ "#{leading_punctuation}#{word}#{trailing_punctuation}"
38
+ end.join(" ")
39
39
  end
40
- end
41
40
 
42
- def self.piratize(word)
43
- if dictionary.has_key? word
44
- dictionary[word]
45
- else
46
- translate_if_gerund word
41
+ def piratize_with_pluralization(word)
42
+ pluralized = pluralize(word) == word
43
+ if dictionary.has_key? singularize(word)
44
+ word = dictionary[singularize(word)]
45
+ pluralized ? pluralize(word) : word
46
+ else
47
+ translate_if_gerund word
48
+ end
47
49
  end
48
- end
49
50
 
50
- def self.translate_if_gerund(word)
51
- if word[/ing\Z/]
52
- word.sub(/ing\Z/, "in'")
53
- elsif word[/ings\Z/]
54
- word.sub(/ings\Z/, "in's")
55
- else
56
- word
51
+ def piratize(word)
52
+ if dictionary.has_key? word
53
+ dictionary[word]
54
+ else
55
+ translate_if_gerund word
56
+ end
57
57
  end
58
- end
59
58
 
60
- def self.extract_punctuation(word)
61
- leading_punctuation = word.match(/\A([^a-zA-Z]*)/)[1] rescue ""
62
- trailing_punctuation = word.match(/[a-zA-Z]+([^a-zA-Z]*)\Z/)[1] rescue ""
63
- word_length = word.length - leading_punctuation.length - trailing_punctuation.length
64
- word = word[leading_punctuation.length, word_length]
65
- return leading_punctuation, word, trailing_punctuation
66
- end
59
+ def translate_if_gerund(word)
60
+ if word[/ing\Z/]
61
+ word.sub(/ing\Z/, "in'")
62
+ elsif word[/ings\Z/]
63
+ word.sub(/ings\Z/, "in's")
64
+ else
65
+ word
66
+ end
67
+ end
67
68
 
68
- def self.prepare_original_sentence(sentence)
69
- sentence.gsub!(/\.\z/, "")
70
- sentence = sentence + "." if sentence.match(/\w\z/)
71
- capitalize_first(sentence)
72
- end
69
+ def extract_punctuation(word)
70
+ leading_punctuation = word.match(/\A([^a-zA-Z]*)/)[1] rescue ''
71
+ trailing_punctuation = word.match(/[a-zA-Z]+([^a-zA-Z]*)\Z/)[1] rescue ''
72
+ word_length = word.length - leading_punctuation.length - trailing_punctuation.length
73
+ word = word[leading_punctuation.length, word_length]
74
+ return leading_punctuation, word, trailing_punctuation
75
+ end
73
76
 
74
- def self.build_piratey_sentence
75
- capitalize_first(sprinklings_of_flavor.sample) + ["!!","!","."].sample
76
- end
77
+ def prepare_original_sentence(sentence)
78
+ sentence.gsub!(/\.\z/, "")
79
+ sentence = sentence + "." if sentence.match(/\w\z/)
80
+ capitalize_first(sentence)
81
+ end
77
82
 
78
- def self.pirate_locale
79
- @@locale ||= local_config.has_key?("locale") ? local_config["locale"] : config["locale"]
80
- end
83
+ def build_piratey_sentence
84
+ capitalize_first(sprinklings_of_flavor.sample) + ["!!","!","."].sample
85
+ end
81
86
 
82
- def self.dictionary
83
- @@dictionary_map ||= config["dictionary"].merge(local_dictionary)
84
- end
87
+ def dictionary
88
+ @@dictionary_map ||= config['dictionary'].merge(local_dictionary)
89
+ end
85
90
 
86
- def self.sprinklings_of_flavor
87
- @@fill ||= (config["pirate_flavor"] << local_flavor).compact.flatten
88
- end
91
+ def sprinklings_of_flavor
92
+ @@fill ||= (config['pirate_flavor'] << local_flavor).compact.flatten
93
+ end
89
94
 
90
- def self.config
91
- @@config ||= YAML::load_file(File.dirname(__FILE__) + "/talk_like_a_pirate/pirate_booty.yml")
92
- end
95
+ def config
96
+ @@config ||= begin
97
+ gem_config_path = File.join File.dirname(__FILE__), 'config', 'pirate_booty.yml'
98
+ YAML::load_file gem_config_path
99
+ end
100
+ end
93
101
 
94
- def self.local_dictionary
95
- local_config.has_key?("dictionary") ? local_config["dictionary"] : {}
96
- end
102
+ def local_dictionary
103
+ local_config['dictionary'] || {}
104
+ end
97
105
 
98
- def self.local_flavor
99
- local_config.has_key?("pirate_flavor") ? local_config["pirate_flavor"] : nil
100
- end
106
+ def local_flavor
107
+ local_config['pirate_flavor'] || nil
108
+ end
101
109
 
102
- def self.local_config
103
- @@local_configs ||= YAML::load_file("#{Rails.root.to_s}/config/pirate_booty.yml") rescue {}
104
- end
110
+ def local_config
111
+ @@local_configs ||= begin
112
+ local_config_path = if on_rails?
113
+ Rails.root.join 'config', 'pirate_booty.yml'
114
+ else
115
+ ENV['TALK_LIKE_A_PIRATE_CONFIG_PATH']
116
+ end
117
+ if local_config_path && File.exist?(local_config_path)
118
+ YAML::load_file(local_config_path)
119
+ else
120
+ {}
121
+ end
122
+ end
123
+ end
105
124
 
106
- def self.capitalize_first(string)
107
- return string unless string.is_a? String
108
- return string.upcase if string.length < 2
109
- string.slice(0,1).capitalize + string.slice(1..-1)
110
- end
125
+ def capitalize_first(string)
126
+ return string unless string.is_a? String
127
+ return string.upcase if string.length < 2
128
+ string.slice(0,1).capitalize + string.slice(1..-1)
129
+ end
111
130
 
112
- def self.singularize(word)
113
- ActiveSupport::Inflector.singularize(word)
114
- end
131
+ def singularize(word)
132
+ ActiveSupport::Inflector.singularize(word)
133
+ end
115
134
 
116
- def self.pluralize(word)
117
- ActiveSupport::Inflector.pluralize(word)
135
+ def pluralize(word)
136
+ ActiveSupport::Inflector.pluralize(word)
137
+ end
118
138
  end
119
-
120
139
  end
121
140
 
122
- if Object.const_defined? :Rails
123
- require "talk_like_a_pirate/railties"
141
+ if TalkLikeAPirate.on_rails?
142
+ require 'rails/railties'
124
143
  else
125
- require "yaml"
144
+ require 'yaml'
126
145
  end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ include I18nHelperMethods
3
+
4
+ describe TalkLikeAPirate::I18nTranslator do
5
+ let(:source_locale) { 'en' }
6
+ let(:instance) { TalkLikeAPirate::I18nTranslator.new(from_locale: source_locale) }
7
+ setup_temp_folder
8
+ after { remove_temp_files }
9
+
10
+ describe '#translate_file_or_directory' do
11
+ let(:source_filename) { 'en.yml' }
12
+ let(:source_path) { nil }
13
+
14
+ subject do
15
+ create_source_file source_path, source_filename
16
+ instance.translate_file_or_directory(@temp_folder)
17
+ target_filename = source_filename.sub(source_locale, 'arr')
18
+ tempfile_exists? File.join(*[source_path, target_filename].compact)
19
+ end
20
+
21
+ describe 'translated file creation' do
22
+ it { is_expected.to be true }
23
+
24
+ context 'prefixed filenames' do
25
+ let(:source_filename) { 'devise.en.yml' }
26
+ it { is_expected.to be true }
27
+ end
28
+
29
+ context 'sublocale filenames' do
30
+ let(:source_filename) { 'en-US.yml' }
31
+ it { is_expected.to be true }
32
+ end
33
+
34
+ context 'subfolders' do
35
+ let(:source_path) { 'subfolder' }
36
+ it { is_expected.to be true }
37
+ end
38
+
39
+ context 'nested subfolders' do
40
+ let(:source_path) { 'subfolder/another_subfolder' }
41
+ it { is_expected.to be true }
42
+ end
43
+
44
+ context 'multiple source files in folder' do
45
+ before do
46
+ create_source_file '', source_filename_1
47
+ create_source_file '', source_filename_2
48
+ end
49
+ let(:source_filename_1) { 'en.yml' }
50
+ let(:source_filename_2) { 'en-US.yml' }
51
+ it { expect(tempfile_exists?(source_filename_1)).to be true }
52
+ it { expect(tempfile_exists?(source_filename_2)).to be true }
53
+ end
54
+ end
55
+
56
+ describe 'yaml top level key' do
57
+ subject do
58
+ create_source_file '', source_filename
59
+ instance.translate_file_or_directory(@temp_folder)
60
+ target_filename = source_filename.sub(source_locale, 'arr')
61
+
62
+ yaml_for(target_filename).keys[0]
63
+ end
64
+
65
+ it { is_expected.to eq 'arr' }
66
+
67
+ context 'prefixed filenames' do
68
+ let(:source_filename) { 'devise.en.yml' }
69
+ it { is_expected.to eq 'arr' }
70
+ end
71
+
72
+ context 'sublocale filenames' do
73
+ let(:source_filename) { 'en-US.yml' }
74
+ it { is_expected.to eq 'arr-US' }
75
+ end
76
+ end
77
+
78
+ describe 'translations' do
79
+ def subject(yaml_path)
80
+ create_source_file '', 'en.yml'
81
+ instance.translate_file_or_directory(@temp_folder)
82
+ yaml_for('arr.yml').dig *yaml_path
83
+ end
84
+ it { expect(subject(['arr', 'house', 'address'])).to eq "Port o' call"}
85
+ it { expect(subject(['arr', 'house', 'country'])).to eq "Land"}
86
+ it { expect(subject(['arr', 'person', 'employee'])).to eq "Crew"}
87
+ it { expect(subject(['arr', 'person', 'family'])).to eq "kin"}
88
+ end
89
+ end
90
+ end
@@ -1,57 +1,63 @@
1
- require 'talk_like_a_pirate'
1
+ require 'spec_helper'
2
2
 
3
3
  describe TalkLikeAPirate do
4
+ def execute(string)
5
+ TalkLikeAPirate.translate(string)
6
+ end
4
7
 
5
- it "should make a word piratey" do
6
- TalkLikeAPirate.translate('between').should == "betwixt"
8
+ it "makes a word piratey" do
9
+ expect(execute('between')).to eq "betwixt"
7
10
  end
8
11
 
9
- it "shouldn't translate plural words without ActiveSupport" do
10
- TalkLikeAPirate.translate("islands").should == 'islands'
12
+ it "makes gerunds piratey" do
13
+ expect(execute('having')).to eq "havin'"
11
14
  end
12
15
 
13
- it "should make gerunds piratey" do
14
- TalkLikeAPirate.translate('having').should == "havin'"
16
+ it "makes plural gerunds piratey" do
17
+ expect(execute('havings')).to eq "havin's"
15
18
  end
16
19
 
17
- it "should make plural gerunds piratey" do
18
- TalkLikeAPirate.translate('havings').should == "havin's"
20
+ it "capitalizes single words" do
21
+ expect(execute('Boss')).to eq "Admiral"
19
22
  end
20
23
 
21
- it "should properly capitalize single words" do
22
- TalkLikeAPirate.translate('Boss').should == "Admiral"
24
+ it "capitalizes all caps words" do
25
+ expect(execute('BOSS')).to eq "ADMIRAL"
26
+ expect(execute('BOSS!!!!')).to eq "ADMIRAL!!!!"
23
27
  end
24
28
 
25
- it "should properly capitalize all caps words" do
26
- TalkLikeAPirate.translate('BOSS').should == "ADMIRAL"
27
- TalkLikeAPirate.translate('BOSS!!!!').should == "ADMIRAL!!!!"
29
+ it "capitalizes phrases" do
30
+ expect(execute('Bourbon Country')).to eq "Rum Land"
31
+ expect(execute('Bourbon country')).to eq "Rum land"
28
32
  end
29
33
 
30
- it "should properly capitalize phrases" do
31
- TalkLikeAPirate.translate('Bourbon Country').should == "Rum Land"
32
- TalkLikeAPirate.translate('Bourbon country').should == "Rum land"
34
+ it "translates words with trailing punctuation" do
35
+ expect(execute('man!!!')).to eq "pirate!!!"
36
+ expect(execute('man!?!?!?!')).to eq "pirate!?!?!?!"
33
37
  end
34
38
 
35
- it "should properly translate words with trailing punctuation" do
36
- TalkLikeAPirate.translate('man!!!').should == "pirate!!!"
37
- TalkLikeAPirate.translate('man!?!?!?!').should == "pirate!?!?!?!"
39
+ it "translates plural gerunds with trailing punctuation" do
40
+ expect(execute('belongings!')).to eq "belongin's!"
38
41
  end
39
42
 
40
- it "should properly translate plural gerunds with trailing punctuation" do
41
- TalkLikeAPirate.translate('belongings!').should == "belongin's!"
43
+ it "punctuates and translates words with leading and punctuation" do
44
+ expect(execute('"The boss said kill."')).to eq '"Tha admiral said keelhaul."'
45
+ expect(execute('"The boss said to kill the dude!"')).to eq '"Tha admiral said t\' keelhaul tha pirate!"'
42
46
  end
43
47
 
44
- it "should properly punctuate and translate words with leading and punctuation" do
45
- TalkLikeAPirate.translate('"The boss said kill."').should == '"Tha admiral said keelhaul."'
46
- TalkLikeAPirate.translate('"The boss said to kill the dude!"').split(" ")[0..6].join(" ").should == '"Tha admiral said t\' keelhaul tha pirate!"'
48
+ context 'without ActiveSupport' do
49
+ it "does not translate plural words" do
50
+ expect(execute("islands")).to eq 'islands'
51
+ end
47
52
  end
48
53
 
49
- it "should translate plural words when ActiveSupport's available" do
50
- require "active_support"
51
- require "active_support/inflector"
54
+ context 'with ActiveSupport' do
55
+ it "translates plural words" do
56
+ require "active_support"
57
+ require "active_support/inflector"
52
58
 
53
- TalkLikeAPirate.translate("islands").should == 'isles'
54
- TalkLikeAPirate.translate("men").should == 'pirates'
59
+ expect(execute("islands")).to eq 'isles'
60
+ expect(execute("men")).to eq 'pirates'
61
+ end
55
62
  end
56
-
57
63
  end
@@ -0,0 +1,5 @@
1
+ require 'talk_like_a_pirate'
2
+ require 'support/i18n_helper_methods'
3
+ require 'rails/i18n_translator'
4
+ RSpec.configure do |c|
5
+ end
@@ -0,0 +1,49 @@
1
+ module I18nHelperMethods
2
+ def setup_temp_folder
3
+ before(:all) do
4
+ tmp_path = File.join File.dirname(__FILE__), 'tmp'
5
+ @temp_folder = FileUtils.mkdir(tmp_path)[0]
6
+ end
7
+ after(:all) do
8
+ FileUtils.rm_r(@temp_folder) unless @temp_folder.nil?
9
+ @temp_folder = nil
10
+ end
11
+ end
12
+
13
+ def tempfile_exists?(target_filename)
14
+ File.exists? tempfile(target_filename)
15
+ end
16
+
17
+ def tempfile_contents(target_filename)
18
+ File.read tempfile(target_filename)
19
+ end
20
+
21
+ def yaml_for(target_filename)
22
+ YAML.load(tempfile_contents(target_filename))
23
+ end
24
+
25
+ def tempfile(target_filename)
26
+ File.join(@temp_folder, target_filename)
27
+ end
28
+
29
+ def remove_temp_files
30
+ return if @temp_folder.nil? || @temp_folder == ''
31
+ Dir.glob(@temp_folder).each do |file_path|
32
+ next if ['.','..',@temp_folder].include?(file_path)
33
+ FileUtils.remove_entry file_path
34
+ end
35
+ end
36
+
37
+ def create_source_file(target_path, target_filename)
38
+ target_path = File.join [@temp_folder, target_path].compact
39
+ FileUtils.mkdir_p(target_path) unless Dir.exists?(target_path)
40
+
41
+ source_path = File.join File.dirname(__FILE__), 'source_template.yml'
42
+ target_file_path = File.join target_path, target_filename
43
+ FileUtils.copy source_path, target_file_path
44
+ end
45
+
46
+ def delete_config_file
47
+ FileUtils.remove_entry File.dirname(target_file_path)
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ en:
2
+ house:
3
+ address: Address
4
+ country: Country
5
+ person:
6
+ employee: Employee
7
+ family: family
@@ -1,17 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- Gem::Specification.new do |s|
3
- s.name = 'talk_like_a_pirate'
4
- s.version = '0.1.0'
5
- s.authors = ['Steve Hodges']
6
- s.email = ['sjhodges@gmail.com']
7
- s.homepage = 'https://github.com/stevehodges/talk_like_a_pirate'
8
- s.summary = 'Talk Like A Pirate'
9
- s.description = 'Add a pirate translation layer to your Rails app! Talk, like a Pirate!'
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'talk_like_a_pirate'
4
+ spec.version = '0.2.2'
5
+ spec.authors = ['Steve Hodges']
6
+ spec.email = ['shodges317@gmail.com']
7
+ spec.homepage = 'https://github.com/stevehodges/talk_like_a_pirate'
8
+ spec.summary = 'Talk Like A Pirate'
9
+ spec.description = 'Add a pirate translation layer to your Rails app! Talk, like a Pirate!'
10
10
 
11
- s.files = `git ls-files`.split("\n")
12
- s.test_files = `git ls-files -- {spec}/*`.split("\n")
11
+ spec.files = `git ls-files`.split("\n")
12
+ spec.test_files = `git ls-files -- {spec}/*`.split("\n")
13
13
 
14
- s.add_development_dependency 'rake'
15
- s.add_development_dependency 'rspec'
16
- s.require_paths = ['lib']
14
+ spec.add_dependency 'activesupport', '>= 3.0.0', '< 6.0.1'
15
+ spec.add_dependency 'i18n'
16
+
17
+ spec.add_development_dependency 'appraisal', '~> 2.2'
18
+ spec.add_development_dependency 'bundler', '~> 1.9'
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'rspec', '~> 3'
21
+ spec.require_paths = ['lib']
17
22
  end
metadata CHANGED
@@ -1,59 +1,131 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: talk_like_a_pirate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hodges
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-14 00:00:00.000000000 Z
11
+ date: 2022-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: i18n
15
35
  requirement: !ruby/object:Gem::Requirement
16
36
  requirements:
17
- - - '>='
37
+ - - ">="
18
38
  - !ruby/object:Gem::Version
19
39
  version: '0'
20
- type: :development
40
+ type: :runtime
21
41
  prerelease: false
22
42
  version_requirements: !ruby/object:Gem::Requirement
23
43
  requirements:
24
- - - '>='
44
+ - - ">="
25
45
  - !ruby/object:Gem::Version
26
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: appraisal
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.2'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.9'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.9'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
27
89
  - !ruby/object:Gem::Dependency
28
90
  name: rspec
29
91
  requirement: !ruby/object:Gem::Requirement
30
92
  requirements:
31
- - - '>='
93
+ - - "~>"
32
94
  - !ruby/object:Gem::Version
33
- version: '0'
95
+ version: '3'
34
96
  type: :development
35
97
  prerelease: false
36
98
  version_requirements: !ruby/object:Gem::Requirement
37
99
  requirements:
38
- - - '>='
100
+ - - "~>"
39
101
  - !ruby/object:Gem::Version
40
- version: '0'
102
+ version: '3'
41
103
  description: Add a pirate translation layer to your Rails app! Talk, like a Pirate!
42
104
  email:
43
- - sjhodges@gmail.com
105
+ - shodges317@gmail.com
44
106
  executables: []
45
107
  extensions: []
46
108
  extra_rdoc_files: []
47
109
  files:
110
+ - ".gitignore"
111
+ - Appraisals
112
+ - CHANGELOG.md
48
113
  - Gemfile
49
114
  - Gemfile.lock
115
+ - LICENSE
50
116
  - README.md
51
117
  - Rakefile
118
+ - lib/config/pirate_booty.yml
119
+ - lib/config/sample_config.yml
120
+ - lib/install_local_config.rb
121
+ - lib/rails/i18n_translator.rb
122
+ - lib/rails/railties.rb
52
123
  - lib/talk_like_a_pirate.rb
53
- - lib/talk_like_a_pirate/pirate_booty.yml
54
- - lib/talk_like_a_pirate/railties.rb
55
- - lib/talk_like_a_pirate/tasks/pirate.rake
124
+ - spec/lib/i18n_translator_spec.rb
56
125
  - spec/lib/talk_like_a_pirate_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/i18n_helper_methods.rb
128
+ - spec/support/source_template.yml
57
129
  - talk_like_a_pirate.gemspec
58
130
  homepage: https://github.com/stevehodges/talk_like_a_pirate
59
131
  licenses: []
@@ -64,17 +136,16 @@ require_paths:
64
136
  - lib
65
137
  required_ruby_version: !ruby/object:Gem::Requirement
66
138
  requirements:
67
- - - '>='
139
+ - - ">="
68
140
  - !ruby/object:Gem::Version
69
141
  version: '0'
70
142
  required_rubygems_version: !ruby/object:Gem::Requirement
71
143
  requirements:
72
- - - '>='
144
+ - - ">="
73
145
  - !ruby/object:Gem::Version
74
146
  version: '0'
75
147
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.0.2
148
+ rubygems_version: 3.1.4
78
149
  signing_key:
79
150
  specification_version: 4
80
151
  summary: Talk Like A Pirate
@@ -1,5 +0,0 @@
1
- class LoadTasks < Rails::Railtie
2
- rake_tasks do
3
- Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
4
- end
5
- end
@@ -1,57 +0,0 @@
1
- namespace "pirate" do
2
- desc "Use this task to translate config/locales dictionary into pirrrrate"
3
- task :translate, :dir_or_file_to_translate do |t, args|
4
- require 'cgi'
5
- args.with_defaults(:dir_or_file_to_translate => "config/locales")
6
- translate_locale(args[:dir_or_file_to_translate])
7
- end
8
- end
9
-
10
- def translate(filename)
11
- en_yml = YAML::load_file(filename)
12
- target_filename = TalkLikeAPirate.pirate_locale + filename.match(/\/en([a-zA-Z-]*).yml/)[1]
13
- dirname = filename.split("/")[0..-2].join("/")
14
- translation = {target_filename => parse_element(en_yml).values.first}
15
- File.open(dirname + "/#{target_filename}.yml", 'w:utf-8') do |f|
16
- YAML::dump translation, f
17
- end
18
- end
19
-
20
- def translate_locale(path)
21
- path = "#{Rails.root}/#{path}" unless path.include? Rails.root.to_s
22
- return translate(path) if File.exists?(path) && path.match("/\/en([a-zA-Z-]*).yml")
23
-
24
- relevent_file_paths(path).each do |file_path|
25
- if File.directory? file_path
26
- translate(file_path + "/en.yml") if File.exists?(file_path + "/en.yml")
27
- translate_locale file_path
28
- elsif file_path.match("\/en([a-zA-Z-]*).yml")
29
- puts file_path
30
- translate file_path
31
- end
32
- end
33
- end
34
-
35
- def parse_element(element)
36
- case element
37
- when Hash
38
- new_hash = {}
39
- element.each {|k,v| new_hash[k] = parse_element(v)}
40
- new_hash
41
- when Array
42
- element.map {|el| parse_element(element)}
43
- when String
44
- TalkLikeAPirate.translate element
45
- else
46
- element
47
- end
48
- end
49
-
50
- def relevent_file_paths(path)
51
- Dir.new(path).entries.reject do |filename|
52
- [".", ".."].include? filename
53
- end.map do |filename|
54
- "#{path}/#{filename}"
55
- end
56
- end
57
-