visionmedia-translatable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc ADDED
@@ -0,0 +1,4 @@
1
+
2
+ === 0.0.1 / YYYY-MM-DD
3
+
4
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,11 @@
1
+ benchmarks/translations.rb
2
+ History.rdoc
3
+ lib/translatable/string.rb
4
+ lib/translatable/translatable.rb
5
+ lib/translatable/version.rb
6
+ lib/translatable.rb
7
+ Manifest
8
+ Rakefile
9
+ README.rdoc
10
+ spec/translatable_spec.rb
11
+ Todo.rdoc
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+
2
+ = Translatable
3
+
4
+ High performance, minimalistic translation library
5
+
6
+ == License:
7
+
8
+ (The MIT License)
9
+
10
+ Copyright (c) 2008 TJ Holowaychuk
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining
13
+ a copy of this software and associated documentation files (the
14
+ 'Software'), to deal in the Software without restriction, including
15
+ without limitation the rights to use, copy, modify, merge, publish,
16
+ distribute, sublicense, an d/or sell copies of the Software, and to
17
+ permit persons to whom the Software is furnished to do so, subject to
18
+ the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be
21
+ included in all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
24
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+ require 'spec/rake/spectask'
6
+ require './lib/translatable.rb'
7
+
8
+ Echoe.new("translatable", Translatable::VERSION::STRING) do |p|
9
+ p.author = "TJ Holowaychuk"
10
+ p.email = "tj@vision-media.ca"
11
+ p.summary = "High performance, minimalistic translation library"
12
+ p.url = "http://github.com/visionmedia/translateable"
13
+ p.runtime_dependencies = []
14
+ end
15
+
16
+ desc 'Run specifications verbosely'
17
+ Spec::Rake::SpecTask.new(:spec) do |t|
18
+ sh 'clear'
19
+ t.libs << "lib"
20
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "translatable"]
21
+ end
22
+
23
+ desc 'Run benchmark suite (specify TIMES)'
24
+ task :benchmark do
25
+ sh 'clear'
26
+ sh 'ruby benchmarks/translations.rb'
27
+ end
data/Todo.rdoc ADDED
@@ -0,0 +1,6 @@
1
+
2
+ == Major:
3
+
4
+ * Faster !!!
5
+ * Docs
6
+ * HTML entity escaping (optional but default)
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ require File.dirname(__FILE__) + '/../lib/translatable'
5
+ require File.dirname(__FILE__) + '/faux_translations'
6
+
7
+ n = ENV['TIMES'].to_i || 1000
8
+ puts "\nEach report ran %d times with %d faux translations\n\n" % [n, translations[:en].length]
9
+ Benchmark.bm(25) do |x|
10
+ x.report("String") {
11
+ n.times do |i|
12
+ 'String'
13
+ end
14
+ }
15
+ x.report("String Sub #\{\}") {
16
+ n.times do |i|
17
+ "String #{i}"
18
+ end
19
+ }
20
+ x.report("String Sub %") {
21
+ n.times do |i|
22
+ 'String %d' % i
23
+ end
24
+ }
25
+ x.report("Translation Bypassed") {
26
+ n.times do |i|
27
+ 'String %d'.t i
28
+ end
29
+ }
30
+ x.report("Translation Performed") {
31
+ allow_english_translations
32
+ translate :en, 'String %d' => 'Index is %d'
33
+ n.times do |i|
34
+ 'String %d'.t i
35
+ end
36
+ }
37
+ end
@@ -0,0 +1,39 @@
1
+
2
+ class String
3
+
4
+ ##
5
+ # Translate a string.
6
+ #
7
+ # Pass _args_ which in turn are passed to String's % method, used
8
+ # for placeholders which should not be translated such as peoples names,
9
+ # emails, etc.
10
+ #
11
+ # When the language is english (default), this process
12
+ # is simply bypassed, returning the original string. Otherwise
13
+ # a lookup on the translations hash is performed, searching for a match,
14
+ # otherwise again the original string is returned.
15
+ #
16
+ # There are times when you may want to override strings even within the
17
+ # english language simply for flexibility, to do this set
18
+ # Translateable::BypassEnglishTranslations to false.
19
+ #
20
+ #
21
+ # === Examples:
22
+ #
23
+ # 'Hello'.t # => Hello
24
+ # 'Hello %s'.t 'TJ' # => Hello TJ
25
+ # 'Hello %s, welcome to %s'.t 'TJ', 'Magic Land' # => Hello TJ, welcome to Magic Land
26
+ # 'Welcome to %2$s %1$s'.t 'TJ', 'Magic Land' # => Welcome to Magic Land TJ
27
+ #
28
+
29
+ def translate *args
30
+ t = Translatable
31
+ if t.language == :en and not t.english_translations_allowed?
32
+ self % args
33
+ else
34
+ s = t.translations[t.language][self] rescue self
35
+ s % args
36
+ end
37
+ end
38
+ alias_method :t, :translate
39
+ end
@@ -0,0 +1,82 @@
1
+
2
+ module Translatable
3
+
4
+ ##
5
+ # Active language symbol.
6
+
7
+ @@language = :en
8
+
9
+ ##
10
+ # Hash of translations loaded.
11
+
12
+ @@translations = {}
13
+
14
+ ##
15
+ # Wither or not english string translation lookup should be bypassed
16
+ # for performance enhancements.
17
+
18
+ @@bypass_english_translations = true
19
+
20
+ module_function
21
+
22
+ def english_translations_allowed?
23
+ !@@bypass_english_translations
24
+ end
25
+
26
+ ##
27
+ # Allow overriding of english strings, this can be used
28
+ # to make alterations in situations such as a CMS where you
29
+ # may not want to alter core. However since most instances
30
+ # will be using english we typically bypass the translation
31
+ # process when #language is set to :en
32
+
33
+ def allow_english_translations
34
+ @@bypass_english_translations = false
35
+ end
36
+
37
+ ##
38
+ # Contrast of #allow_english_translations.
39
+
40
+ def disallow_english_translations
41
+ @@bypass_english_translations = true
42
+ end
43
+
44
+ ##
45
+ # Set or return _lanf_ code symbol. Default is :en
46
+
47
+ def language lang = nil
48
+ @@language = lang.to_sym unless lang.nil?
49
+ @@language
50
+ end
51
+
52
+ ##
53
+ # Translate one or more strings for _lang_.
54
+ #
55
+ # Where _lang_ is a symbol representation of the language
56
+ # such as :en, :fr, and _translations_ is a hash of one or more
57
+ # key / value pairs containing the original / translated string.
58
+ #
59
+ # === Examples:
60
+ #
61
+ # translate :fr, 'I like cookies' => 'J'aime des biscuits'
62
+ #
63
+ # OR
64
+ #
65
+ # translate :fr,
66
+ # 'I like cookies' => 'J'aime des biscuits'
67
+ # 'I like ruby' => '...'
68
+ # 'I like ferrets' => '...'
69
+ #
70
+
71
+ def translate lang, translations
72
+ @@translations[lang] = (@@translations[lang] || {}).merge translations
73
+ end
74
+
75
+ ##
76
+ # Return translation hash.
77
+
78
+ def translations
79
+ @@translations
80
+ end
81
+
82
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module Translatable
3
+ module VERSION #:nodoc:
4
+ MAJOR, MINOR, TINY = [0, 0, 1]
5
+ STRING = [MAJOR, MINOR, TINY].join '.'
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ #--
2
+ # Copyright (c) 2008 TJ Holowaychuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift File.dirname(__FILE__)
25
+
26
+ require 'translatable/version'
27
+ require 'translatable/translatable'
28
+ require 'translatable/string'
29
+
30
+ Object.send :include, Translatable
@@ -0,0 +1,34 @@
1
+
2
+ describe "Translateable" do
3
+
4
+ before :each do
5
+ disallow_english_translations
6
+ end
7
+
8
+ it "should not allow overriding of english strings" do
9
+ translate :en, 'foo' => 'bar'
10
+ 'foo'.t.should eql('foo')
11
+ 'foo'.t.should_not eql('bar')
12
+ end
13
+
14
+ it "should allow overriding of english strings" do
15
+ allow_english_translations
16
+ translate :en, 'foo' => 'bar'
17
+ 'foo'.t.should eql('bar')
18
+ end
19
+
20
+ it "should allow overriding of strings in english with placeholders" do
21
+ allow_english_translations
22
+ translate :en, 'Your username is %s, you may also login using %s.' => 'Try your email %2$s, or maybe the username %1$s.'
23
+ string = 'Your username is %s, you may also login using %s.'.t 'tjholowaychuk', 'tj@vision-media.ca'
24
+ string.should eql('Try your email tj@vision-media.ca, or maybe the username tjholowaychuk.')
25
+ end
26
+
27
+ it "should allow translations of strings using other languages" do
28
+ translate :fr, 'I like %s' => "J'aime %s"
29
+ language :fr
30
+ string = 'I like %s'.t 'cookies'
31
+ string.should eql("J'aime cookies")
32
+ end
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{translatable}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["TJ Holowaychuk"]
9
+ s.date = %q{2008-12-11}
10
+ s.description = %q{High performance, minimalistic translation library}
11
+ s.email = %q{tj@vision-media.ca}
12
+ s.extra_rdoc_files = ["lib/translatable/string.rb", "lib/translatable/translatable.rb", "lib/translatable/version.rb", "lib/translatable.rb", "README.rdoc"]
13
+ s.files = ["benchmarks/translations.rb", "History.rdoc", "lib/translatable/string.rb", "lib/translatable/translatable.rb", "lib/translatable/version.rb", "lib/translatable.rb", "Manifest", "Rakefile", "README.rdoc", "spec/translatable_spec.rb", "Todo.rdoc", "translatable.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/visionmedia/translateable}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Translatable", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{translatable}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{High performance, minimalistic translation library}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<echoe>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<echoe>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visionmedia-translatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - TJ Holowaychuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: High performance, minimalistic translation library
25
+ email: tj@vision-media.ca
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - lib/translatable/string.rb
32
+ - lib/translatable/translatable.rb
33
+ - lib/translatable/version.rb
34
+ - lib/translatable.rb
35
+ - README.rdoc
36
+ files:
37
+ - benchmarks/translations.rb
38
+ - History.rdoc
39
+ - lib/translatable/string.rb
40
+ - lib/translatable/translatable.rb
41
+ - lib/translatable/version.rb
42
+ - lib/translatable.rb
43
+ - Manifest
44
+ - Rakefile
45
+ - README.rdoc
46
+ - spec/translatable_spec.rb
47
+ - Todo.rdoc
48
+ - translatable.gemspec
49
+ has_rdoc: true
50
+ homepage: http://github.com/visionmedia/translateable
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --line-numbers
54
+ - --inline-source
55
+ - --title
56
+ - Translatable
57
+ - --main
58
+ - README.rdoc
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "1.2"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: translatable
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: High performance, minimalistic translation library
80
+ test_files: []
81
+