Skip to content

Commit 13ecb80

Browse files
committed
gibberish
0 parents  commit 13ecb80

File tree

9 files changed

+233
-0
lines changed

9 files changed

+233
-0
lines changed

‎LICENSE‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright (c) 2007 Chris Wanstrath
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
= Gibberish
2+
3+
Yet another localization library. Maybe with the most agreeable API.
4+
5+
>> Chris Wanstrath
6+
=> chris[at]ozmm[dot]org

‎init.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'gibberish'

‎lang/es.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
welcome_friend: ¡Recepción, amigo!
2+
welcome_user: ¡Recepción, {user}!
3+
love_rails: Amo los carriles.

‎lang/fr.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
welcome_friend: Bienvenue, ami!
2+
welcome_user: Bienvenue, {user}!
3+
love_rails: J'aime des rails.

‎lib/gibberish.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'gibberish/localize'
2+
require 'gibberish/string_ext'
3+
4+
String.send :include, Gibberish::StringExt
5+
6+
module Gibberish
7+
extend Localize
8+
end

‎lib/gibberish/localize.rb‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Gibberish
2+
module Localize
3+
@@config = {}
4+
mattr_accessor :config
5+
6+
@@languages = {}
7+
mattr_accessor :languages
8+
9+
@@default_language = :en
10+
mattr_reader :default_language
11+
12+
@@current_language = nil
13+
def current_language
14+
@@current_language || default_language
15+
end
16+
17+
def current_language=(language)
18+
@@current_language = languages[language] ? language : nil
19+
end
20+
21+
def default_language?
22+
current_language == default_language
23+
end
24+
25+
def use_default_language
26+
end
27+
28+
def translations
29+
@@languages[current_language] || {}
30+
end
31+
32+
def translate(string, key, *args)
33+
target = translations[key] || string
34+
interpolate_string(target.dup, *args.dup)
35+
end
36+
37+
def load_languages!
38+
language_files.each do |file|
39+
key = File.basename(file, '.*').to_sym
40+
@@languages[key] = YAML.load_file(file).symbolize_keys
41+
end
42+
end
43+
44+
private
45+
def interpolate_string(string, *args)
46+
string.gsub(/\{\w+\}/) { args.shift }
47+
end
48+
49+
def language_files
50+
Dir[File.join(RAILS_ROOT, 'lang', '*.{yml,yaml}')]
51+
end
52+
end
53+
end

‎lib/gibberish/string_ext.rb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Gibberish
2+
module StringExt
3+
def brackets_with_translation(*args)
4+
return brackets_without_translation(*args) unless args.first.is_a? Symbol
5+
Gibberish.translate(self, args.shift, *args)
6+
end
7+
8+
def self.included(base)
9+
base.class_eval do
10+
alias :brackets :[]
11+
alias_method_chain :brackets, :translation
12+
alias :[] :brackets
13+
end
14+
end
15+
end
16+
end

‎test/gibberish_test.rb‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
begin
2+
require 'test/spec'
3+
rescue LoadError
4+
puts "==> The test/spec library (gem) is required to run the Gibberish tests."
5+
exit
6+
end
7+
8+
$:.unshift File.dirname(__FILE__) + '/../lib'
9+
require 'active_support'
10+
require 'gibberish'
11+
12+
RAILS_ROOT = '.'
13+
Gibberish.load_languages!
14+
15+
context "After loading languages, Gibberish" do
16+
teardown do
17+
Gibberish.current_language = nil
18+
end
19+
20+
specify "should know what languages it has translations for" do
21+
Gibberish.languages.keys.should.include :es
22+
end
23+
24+
specify "should know if it is using the default language" do
25+
Gibberish.should.be.default_language
26+
end
27+
28+
specify "should be able to switch between existing languages" do
29+
Gibberish.current_language = :es
30+
string = "Welcome, friend!"
31+
string[:welcome_friend].should.not.equal string
32+
33+
Gibberish.current_language = :fr
34+
string[:welcome_friend].should.not.equal string
35+
end
36+
37+
specify "should be able to switch to the default language at any time" do
38+
Gibberish.current_language = :fr
39+
Gibberish.should.not.be.default_language
40+
41+
Gibberish.current_language = nil
42+
Gibberish.should.be.default_language
43+
end
44+
end
45+
46+
context "When no language is set" do
47+
setup do
48+
Gibberish.current_language = nil
49+
end
50+
51+
specify "the default language should be used" do
52+
Gibberish.current_language.should.equal Gibberish.default_language
53+
end
54+
55+
specify "a gibberish string should return itself" do
56+
string = "Welcome, friend!"
57+
Gibberish.translate(string, :welcome_friend).should.equal string
58+
59+
string[:welcome_friend].should.equal string
60+
end
61+
end
62+
63+
context "When a non-existent language is set" do
64+
setup do
65+
Gibberish.current_language = :klingon
66+
end
67+
68+
specify "the default language should be used" do
69+
Gibberish.current_language.should.equal Gibberish.default_language
70+
end
71+
72+
specify "gibberish strings should return themselves" do
73+
string = "something gibberishy"
74+
string[:welcome_friend].should.equal string
75+
end
76+
end
77+
78+
context "A gibberish string (in general)" do
79+
specify "should be a string" do
80+
"gibberish"[:just_a_string].should.be.an.instance_of String
81+
"non-gibberish".should.be.an.instance_of String
82+
end
83+
84+
specify "should interpolate if passed arguments and replaces are present" do
85+
'Hi, {user} of {place}'[:hi_there, 'chris', 'france'].should.equal "Hi, chris of france"
86+
'{computer} omg?'[:puter, 'mac'].should.equal "mac omg?"
87+
end
88+
89+
specify "should not affect existing string methods" do
90+
string = "chris"
91+
answer = 'ch'
92+
string[0..1].should.equal answer
93+
string[0, 2].should.equal answer
94+
string[0].should.equal 99
95+
string[/ch/].should.equal answer
96+
string['ch'].should.equal answer
97+
string['bc'].should.be.nil
98+
string[/dog/].should.be.nil
99+
end
100+
end
101+
102+
context "When a non-default language is set" do
103+
setup do
104+
Gibberish.current_language = :es
105+
end
106+
107+
specify "that language should be used" do
108+
Gibberish.current_language.should.equal :es
109+
end
110+
111+
specify "the default language should not be used" do
112+
Gibberish.should.not.be.default_language
113+
end
114+
115+
specify "a gibberish string should return itself if a corresponding key is not found" do
116+
string = "The internet!"
117+
string[:the_internet].should.equal string
118+
end
119+
120+
specify "a gibberish string should return a translated version of itself if a corresponding key is found" do
121+
"Welcome, friend!"[:welcome_friend].should.equal "¡Recepción, amigo!"
122+
"I love Rails."[:love_rails].should.equal "Amo los carriles."
123+
'Welcome, {user}!'[:welcome_user, 'Marvin'].should.equal "¡Recepción, Marvin!"
124+
end
125+
end

0 commit comments

Comments
 (0)