q_translate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,5 +14,4 @@ rdoc
14
14
  spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
- tmp
18
- /nbproject/
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p385@q_translate --create
data/Gemfile CHANGED
@@ -1,4 +1,2 @@
1
1
  source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in q_translate.gemspec
4
2
  gemspec
data/lib/q_translate.rb CHANGED
@@ -1,40 +1,40 @@
1
1
  require "q_translate/version"
2
2
 
3
- module QTranslate
4
-
3
+ module QTranslate
5
4
  class Translation
6
5
 
7
- attr_accessor :original
6
+ attr_accessor :text, :locale, :fallback
8
7
 
9
- def initialize text
10
- @original = text
8
+ def initialize text, options={}
9
+ @text = text
10
+ @locale = options[:locale] || I18n.locale
11
+ @fallback = options[:fallback] || true
11
12
  end
12
13
 
13
- def locale
14
- I18n.locale
14
+ def translate
15
+ return @text if @text and @text.blank?
16
+ return translations[@locale] if translations.has_key?(@locale)
17
+ return translations[I18n.default_locale] if @fallback and translations.has_key?(I18n.default_locale)
18
+ return '' if translations.keys.any?
19
+ @text
15
20
  end
16
21
 
17
- def translate
18
- return @original if @original.blank?
19
- if translations.has_key? locale
20
- translations[locale]
21
- elsif translations.has_key? I18n.default_locale
22
- translations[I18n.default_locale]
23
- else
24
- @original
25
- end
22
+ def translations
23
+ @translations ||= split_locales
24
+ @translations
26
25
  end
27
26
 
28
- def translations
29
- @translations = {}
30
- @original.split('<!--:-->').each do |t|
27
+ protected
28
+
29
+ def split_locales
30
+ translations_per_locale = {}
31
+ self.text.split('<!--:-->').each do |t|
31
32
  t.match(/^<!--:([\w]{2})/) do |lang|
32
- @translations[lang[1].to_sym] = t.gsub(/<!--:#{lang[1]}-->(.*)/, '\1')
33
+ translations_per_locale[lang[1].to_sym] = t.gsub(/<!--:#{lang[1]}-->(.*)/, '\1')
33
34
  end
34
35
  end
35
- @translations
36
+ translations_per_locale
36
37
  end
37
-
38
- end
39
-
38
+
39
+ end
40
40
  end
@@ -1,3 +1,3 @@
1
1
  module QTranslate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/q_translate.gemspec CHANGED
@@ -4,17 +4,18 @@ require File.expand_path('../lib/q_translate/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = "q_translate"
6
6
  gem.authors = ["Mathieu Gagné"]
7
- gem.email = ["mathieu@orangebrule.com"]
7
+ gem.email = ["gagne.mathieu@hotmail.com"]
8
8
  gem.description = %q{Extends String with a 'translate' method. Returned value is the according translated text for a Wordpress Post translated by QTranslate plugin.}
9
9
  gem.summary = %q{Extends String with a 'translate' method. Returned value is the according translated text for a Wordpress Post translated by QTranslate plugin. Useful for Wordpress integration in a Rails app where the admin bloggin side is handled by Wordpress Engine. The default value if translation is not provided is English.}
10
- gem.homepage = "http://github.com/orangebrule/q-translate"
10
+ gem.homepage = "http://github.com/mathieugagne/q-translate"
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.test_files = gem.files.grep(%r{^(spec)/})
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = QTranslate::VERSION
17
17
 
18
+ gem.add_development_dependency 'rails'
18
19
  gem.add_development_dependency 'rspec'
19
20
 
20
21
  end
@@ -3,50 +3,62 @@ require 'spec_helper'
3
3
  TEST_STRING = "<!--:en-->And this is how the Universe ended.<!--:--><!--:fr-->Et c'est ainsi que l'univers connu cessa d'exister.<!--:-->"
4
4
 
5
5
  describe QTranslate::Translation do
6
-
7
- before :each do
8
- @text = QTranslate::Translation.new(TEST_STRING)
9
- end
10
6
 
11
- describe '.translate' do
12
-
13
- describe 'if translated' do
14
-
15
- it "should return French translation if locale is French" do
7
+ describe '#translate' do
8
+
9
+ context 'when translated' do
10
+
11
+ it "returns appropriate translation if locale is set" do
16
12
  I18n.locale = :fr
13
+ @text = QTranslate::Translation.new(TEST_STRING)
17
14
  @text.translate.should match "Et c'est ainsi que l'univers connu cessa d'exister."
18
15
  end
19
16
 
20
- it "should return English translation if locale is English" do
21
- I18n.locale = :en
17
+ it "returns appropriate translation and overrides default if locale is passed as option" do
18
+ I18n.locale = :fr
19
+ @text = QTranslate::Translation.new(TEST_STRING, locale: :en)
22
20
  @text.translate.should match "And this is how the Universe ended."
23
21
  end
24
-
22
+
25
23
  end
26
24
 
27
- describe 'if not translated' do
28
-
29
- it "should use default locale" do
30
- I18n.locale = :es
31
- @text.translate.should match 'And this is how the Universe ended.'
25
+ context 'when not translated' do
26
+
27
+ context 'when fallback is true' do
28
+ it "returns default locale" do
29
+ I18n.locale = :es
30
+ @text = QTranslate::Translation.new(TEST_STRING)
31
+ @text.translate.should match 'And this is how the Universe ended.'
32
+ end
33
+ end
34
+ context 'when fallback is false' do
35
+ it 'returns an empty string' do
36
+ I18n.locale = :es
37
+ @text = QTranslate::Translation.new(TEST_STRING)
38
+ @text.translate.should match ''
39
+ end
32
40
  end
33
41
 
34
- it "should return itself" do
42
+ it "returns itself if no sign of translation" do
35
43
  @text = QTranslate::Translation.new("Whatever man!")
36
44
  @text.translate.should match "Whatever man!"
37
45
  end
46
+
47
+ it "returns an empty string if translations are present but none match" do
48
+ end
38
49
 
39
50
  end
40
51
  end
41
-
42
- describe '.translations' do
43
-
44
- it "should return a hash of locales and associated translated text" do
52
+
53
+ describe '#translations' do
54
+
55
+ it "returns a hash of locales and associated translated text" do
56
+ @text = QTranslate::Translation.new(TEST_STRING)
45
57
  @text.translations.should be_a Hash
46
58
  @text.translations.should have(2).keys
47
59
  @text.translations[:en].should match "And this is how the Universe ended."
48
60
  @text.translations[:fr].should match "Et c'est ainsi que l'univers connu cessa d'exister."
49
61
  end
50
-
62
+
51
63
  end
52
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: q_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: !ruby/object:Gem::Requirement
15
+ name: rails
16
+ requirement: &18422460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,35 +21,39 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ version_requirements: *18422460
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &18421980 !ruby/object:Gem::Requirement
25
28
  none: false
26
29
  requirements:
27
30
  - - ! '>='
28
31
  - !ruby/object:Gem::Version
29
32
  version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18421980
30
36
  description: Extends String with a 'translate' method. Returned value is the according
31
37
  translated text for a Wordpress Post translated by QTranslate plugin.
32
38
  email:
33
- - mathieu@orangebrule.com
39
+ - gagne.mathieu@hotmail.com
34
40
  executables: []
35
41
  extensions: []
36
42
  extra_rdoc_files: []
37
43
  files:
38
44
  - .gitignore
39
45
  - .rspec
46
+ - .rvmrc
40
47
  - Gemfile
41
48
  - LICENSE
42
49
  - README.md
43
50
  - Rakefile
44
51
  - lib/q_translate.rb
45
52
  - lib/q_translate/version.rb
46
- - nbproject/private/rake-d.txt
47
- - nbproject/project.properties
48
- - nbproject/project.xml
49
53
  - q_translate.gemspec
50
54
  - spec/q_translate/q_translate_spec.rb
51
55
  - spec/spec_helper.rb
52
- homepage: http://github.com/orangebrule/q-translate
56
+ homepage: http://github.com/mathieugagne/q-translate
53
57
  licenses: []
54
58
  post_install_message:
55
59
  rdoc_options: []
@@ -69,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
73
  version: '0'
70
74
  requirements: []
71
75
  rubyforge_project:
72
- rubygems_version: 1.8.18
76
+ rubygems_version: 1.8.10
73
77
  signing_key:
74
78
  specification_version: 3
75
79
  summary: Extends String with a 'translate' method. Returned value is the according