Initial import
This commit is contained in:
18
vendor/plugins/gibberish/LICENSE
vendored
Normal file
18
vendor/plugins/gibberish/LICENSE
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
Copyright (c) 2007 Chris Wanstrath
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
113
vendor/plugins/gibberish/README
vendored
Normal file
113
vendor/plugins/gibberish/README
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
= Gibberish
|
||||
|
||||
Yet another localization library. Maybe with the most agreeable API?
|
||||
|
||||
= Usage
|
||||
|
||||
It's simple. Your default language, by default, is English (:en).
|
||||
|
||||
>> "Hey there!"[:hey]
|
||||
=> "Hey there!"
|
||||
|
||||
Gibberish looks in RAILS_ROOT/lang/*.yml for translation files. Say you have RAILS_ROOT/lang/es.yml,
|
||||
right? Gibberish will detect that you know about the :es language and will serve up translations
|
||||
defined in that file if requested to do so.
|
||||
|
||||
Here's a real simple example file (it's just "key: translation"):
|
||||
|
||||
$ cat lang/es.yml
|
||||
hey: <20>Hey all<6C>!
|
||||
|
||||
And, as follows, a real simple example session:
|
||||
|
||||
>> "Hey there!"[:hey]
|
||||
=> "Hey there!"
|
||||
>> Gibberish.current_language
|
||||
=> :en
|
||||
>> Gibberish.current_language = :es
|
||||
=> :es
|
||||
>> "Hey there!"[:hey]
|
||||
=> "<22>Hey all<6C>!"
|
||||
>> Gibberish.current_language = nil
|
||||
=> nil
|
||||
>> "Hey there!"[:hey]
|
||||
=> "Hey there!"
|
||||
|
||||
It even works with simple interpolation:
|
||||
|
||||
>> "Hey, {name}!"[:hey_name, 'Chris']
|
||||
=> "Hey, Chris!"
|
||||
>> "{name} is from {place}"[:hey_place, 'Chris', 'the Dreamworld']
|
||||
=> "Chris is from the Dreamworld"
|
||||
|
||||
Notice we don't use hashes (#) like normal Ruby interpolation. Also, the names of the variables
|
||||
in the brackets don't really mean much. Interpolation is done in order -- the first argument replaces
|
||||
the first variable in brackets, the second the second, etc.
|
||||
|
||||
This of course works with your translations:
|
||||
|
||||
$ cat lang/es.yml
|
||||
hey: <20>Hey all<6C>!
|
||||
hey_name: <20>Hola {name}!
|
||||
|
||||
>> "Hey, {name}!"[:hey_name, 'Chris']
|
||||
=> "Hey, Chris!"
|
||||
>> Gibberish.current_language = :es
|
||||
=> :es
|
||||
>> "Hey, {name}!"[:hey_name, 'Crist<73>bal']
|
||||
=> <20>Hola Crist<73>bal!
|
||||
|
||||
Neat. What other methods do we get?
|
||||
|
||||
The classic around_filter:
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
around_filter :set_language
|
||||
|
||||
private
|
||||
def set_language
|
||||
Gibberish.use_language(session[:language]) { yield }
|
||||
end
|
||||
end
|
||||
|
||||
For the duration of the block, :es is set as the language of choice. After the block is run everything
|
||||
returns to normal. Rad.
|
||||
|
||||
Finally, some checking methods, if you need them:
|
||||
|
||||
>> Gibberish.default_language?
|
||||
=> true
|
||||
>> Gibberish.current_language = :es
|
||||
=> :es
|
||||
>> Gibberish.current_language
|
||||
=> :es
|
||||
>> Gibberish.default_language?
|
||||
=> false
|
||||
|
||||
Languages are loaded by default at Rails startup. In dev mode, language YAML files are reloaded when
|
||||
modified. No need to reboot the server.
|
||||
|
||||
>> Gibberish.load_languages!
|
||||
=> [:es, :fr, :de, :kl]
|
||||
>> Gibberish.languages
|
||||
=> [:es, :fr, :de, :kl]
|
||||
|
||||
More as it's needed.
|
||||
|
||||
= Warning
|
||||
|
||||
By default, Ruby returns nil when a symbol is passed to String's [] method. Some of Rails, it seems, depends
|
||||
on this behavior. Yes, I am changing !!core Ruby behavior!! The humanity!
|
||||
|
||||
To deal with this assumption, Gibberish has a reserved_keys array. It, by default, contains :limit (so Rails
|
||||
migrations don't explode on you.) To add to this array, just pass it more keys:
|
||||
|
||||
>> Gibberish.add_reserved_key :another_key
|
||||
=> [:limit, :another_key]
|
||||
>> Gibberish.add_reserved_keys :more, :keys
|
||||
=> [:limit, :another_key, :more, :keys]
|
||||
|
||||
You've been warned. It really shouldn't affect you, though.
|
||||
|
||||
>> Chris Wanstrath
|
||||
=> chris[at]ozmm[dot]org
|
||||
3
vendor/plugins/gibberish/init.rb
vendored
Normal file
3
vendor/plugins/gibberish/init.rb
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
require 'gibberish'
|
||||
|
||||
Gibberish.load_languages!
|
||||
3
vendor/plugins/gibberish/lang/es.yml
vendored
Normal file
3
vendor/plugins/gibberish/lang/es.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
welcome_friend: <EFBFBD>Recepci<EFBFBD>n, amigo!
|
||||
welcome_user: <EFBFBD>Recepci<EFBFBD>n, {user}!
|
||||
love_rails: Amo los carriles.
|
||||
3
vendor/plugins/gibberish/lang/fr.yml
vendored
Normal file
3
vendor/plugins/gibberish/lang/fr.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
welcome_friend: Bienvenue, ami!
|
||||
welcome_user: Bienvenue, {user}!
|
||||
love_rails: J'aime des rails.
|
||||
10
vendor/plugins/gibberish/lib/gibberish.rb
vendored
Normal file
10
vendor/plugins/gibberish/lib/gibberish.rb
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'gibberish/localize'
|
||||
require 'gibberish/string_ext'
|
||||
require 'gibberish/activerecord_ext'
|
||||
|
||||
String.send :include, Gibberish::StringExt
|
||||
|
||||
module Gibberish
|
||||
extend Localize
|
||||
end
|
||||
|
||||
20
vendor/plugins/gibberish/lib/gibberish/activerecord_ext.rb
vendored
Normal file
20
vendor/plugins/gibberish/lib/gibberish/activerecord_ext.rb
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
module ActiveRecord
|
||||
class Errors
|
||||
def full_messages
|
||||
full_messages = []
|
||||
|
||||
@errors.each_key do |attr|
|
||||
@errors[attr].each do |msg|
|
||||
next if msg.nil?
|
||||
|
||||
if attr == "base"
|
||||
full_messages << msg
|
||||
else
|
||||
full_messages << @base.class.human_attribute_name(attr.send("[]")) + " " + msg
|
||||
end
|
||||
end
|
||||
end
|
||||
full_messages
|
||||
end
|
||||
end
|
||||
end
|
||||
70
vendor/plugins/gibberish/lib/gibberish/localize.rb
vendored
Normal file
70
vendor/plugins/gibberish/lib/gibberish/localize.rb
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
module Gibberish
|
||||
module Localize
|
||||
@@default_language = :en
|
||||
mattr_reader :default_language
|
||||
|
||||
@@reserved_keys = [ :limit ]
|
||||
mattr_reader :reserved_keys
|
||||
|
||||
def add_reserved_key(*key)
|
||||
(@@reserved_keys += key.flatten).uniq!
|
||||
end
|
||||
alias :add_reserved_keys :add_reserved_key
|
||||
|
||||
@@languages = {}
|
||||
def languages
|
||||
@@languages.keys
|
||||
end
|
||||
|
||||
@@current_language = nil
|
||||
def current_language
|
||||
@@current_language || default_language
|
||||
end
|
||||
|
||||
def current_language=(language)
|
||||
load_languages! if defined? RAILS_ENV && RAILS_ENV == 'development'
|
||||
|
||||
language = language.to_sym if language.respond_to? :to_sym
|
||||
@@current_language = @@languages[language] ? language : nil
|
||||
end
|
||||
|
||||
def use_language(language)
|
||||
start_language = current_language
|
||||
self.current_language = language
|
||||
yield
|
||||
self.current_language = start_language
|
||||
end
|
||||
|
||||
def default_language?
|
||||
current_language == default_language
|
||||
end
|
||||
|
||||
def translations
|
||||
@@languages[current_language] || {}
|
||||
end
|
||||
|
||||
def translate(string, key, *args)
|
||||
return if reserved_keys.include? key
|
||||
File.open("#{RAILS_ROOT}/lang/tmp_keys", "a").puts key if ENV['GIBBERISH_EXPORT']
|
||||
target = translations[key] || string
|
||||
interpolate_string(target.dup, *args.dup)
|
||||
end
|
||||
|
||||
def load_languages!
|
||||
language_files.each do |file|
|
||||
key = File.basename(file, '.*').to_sym
|
||||
@@languages[key] = YAML.load_file(file).symbolize_keys
|
||||
end
|
||||
languages
|
||||
end
|
||||
|
||||
private
|
||||
def interpolate_string(string, *args)
|
||||
string.gsub(/\{\w+\}/) { args.shift }
|
||||
end
|
||||
|
||||
def language_files
|
||||
Dir[File.join(RAILS_ROOT, 'lang', '*.{yml,yaml}')]
|
||||
end
|
||||
end
|
||||
end
|
||||
17
vendor/plugins/gibberish/lib/gibberish/string_ext.rb
vendored
Normal file
17
vendor/plugins/gibberish/lib/gibberish/string_ext.rb
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
module Gibberish
|
||||
module StringExt
|
||||
def brackets_with_translation(*args)
|
||||
args = [underscore.tr(' ', '_').to_sym] if args.empty?
|
||||
return brackets_without_translation(*args) unless args.first.is_a? Symbol
|
||||
Gibberish.translate(self, args.shift, *args)
|
||||
end
|
||||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
alias :brackets :[]
|
||||
alias_method_chain :brackets, :translation
|
||||
alias :[] :brackets
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
182
vendor/plugins/gibberish/test/gibberish_test.rb
vendored
Normal file
182
vendor/plugins/gibberish/test/gibberish_test.rb
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
begin
|
||||
require 'rubygems'
|
||||
require 'test/spec'
|
||||
rescue LoadError
|
||||
puts "==> The test/spec library (gem) is required to run the Gibberish tests."
|
||||
exit
|
||||
end
|
||||
|
||||
$:.unshift File.dirname(__FILE__) + '/../lib'
|
||||
require 'active_support'
|
||||
require 'gibberish'
|
||||
|
||||
RAILS_ROOT = '.'
|
||||
Gibberish.load_languages!
|
||||
|
||||
context "After loading languages, Gibberish" do
|
||||
teardown do
|
||||
Gibberish.current_language = nil
|
||||
end
|
||||
|
||||
specify "should know what languages it has translations for" do
|
||||
Gibberish.languages.should.include :es
|
||||
end
|
||||
|
||||
specify "should know if it is using the default language" do
|
||||
Gibberish.should.be.default_language
|
||||
end
|
||||
|
||||
specify "should be able to switch between existing languages" do
|
||||
Gibberish.current_language = :es
|
||||
string = "Welcome, friend!"
|
||||
string[:welcome_friend].should.not.equal string
|
||||
|
||||
Gibberish.current_language = :fr
|
||||
string[:welcome_friend].should.not.equal string
|
||||
|
||||
Gibberish.current_language = nil
|
||||
string[:welcome_friend].should.equal string
|
||||
end
|
||||
|
||||
specify "should be able to switch languages using strings" do
|
||||
Gibberish.current_language = 'es'
|
||||
Gibberish.current_language.should.equal :es
|
||||
end
|
||||
|
||||
specify "should be able to switch to the default language at any time" do
|
||||
Gibberish.current_language = :fr
|
||||
Gibberish.should.not.be.default_language
|
||||
|
||||
Gibberish.current_language = nil
|
||||
Gibberish.should.be.default_language
|
||||
end
|
||||
|
||||
specify "should be able to switch to a certain language for the duration of a block" do
|
||||
Gibberish.should.be.default_language
|
||||
|
||||
string = "Welcome, friend!"
|
||||
string[:welcome_friend].should.equal string
|
||||
|
||||
Gibberish.use_language :es do
|
||||
string[:welcome_friend].should.not.equal string
|
||||
Gibberish.should.not.be.default_language
|
||||
end
|
||||
|
||||
Gibberish.should.be.default_language
|
||||
string[:welcome_friend].should.equal string
|
||||
end
|
||||
|
||||
specify "should return an array of the languages it loaded" do
|
||||
languages = Gibberish.load_languages!
|
||||
languages.should.be.an.instance_of Array
|
||||
languages.should.include :es
|
||||
languages.should.include :fr
|
||||
end
|
||||
|
||||
specify "should know what languages it has loaded" do
|
||||
languages = Gibberish.languages
|
||||
languages.should.be.an.instance_of Array
|
||||
languages.should.include :es
|
||||
languages.should.include :fr
|
||||
end
|
||||
|
||||
specify "should be able to accept new, unique reserved keys" do
|
||||
key = :something_evil
|
||||
Gibberish.add_reserved_key key
|
||||
Gibberish.reserved_keys.should.include key
|
||||
Gibberish.reserved_keys.size.should.equal 2
|
||||
Gibberish.add_reserved_key key
|
||||
Gibberish.add_reserved_key key
|
||||
Gibberish.reserved_keys.size.should.equal 2
|
||||
end
|
||||
end
|
||||
|
||||
context "When no language is set" do
|
||||
setup do
|
||||
Gibberish.current_language = nil
|
||||
end
|
||||
|
||||
specify "the default language should be used" do
|
||||
Gibberish.current_language.should.equal Gibberish.default_language
|
||||
end
|
||||
|
||||
specify "a gibberish string should return itself" do
|
||||
string = "Welcome, friend!"
|
||||
Gibberish.translate(string, :welcome_friend).should.equal string
|
||||
|
||||
string[:welcome_friend].should.equal string
|
||||
end
|
||||
end
|
||||
|
||||
context "When a non-existent language is set" do
|
||||
setup do
|
||||
Gibberish.current_language = :klingon
|
||||
end
|
||||
|
||||
specify "the default language should be used" do
|
||||
Gibberish.current_language.should.equal Gibberish.default_language
|
||||
end
|
||||
|
||||
specify "gibberish strings should return themselves" do
|
||||
string = "something gibberishy"
|
||||
string[:welcome_friend].should.equal string
|
||||
end
|
||||
end
|
||||
|
||||
context "A gibberish string (in general)" do
|
||||
specify "should be a string" do
|
||||
"gibberish"[:just_a_string].should.be.an.instance_of String
|
||||
"non-gibberish".should.be.an.instance_of String
|
||||
end
|
||||
|
||||
specify "should interpolate if passed arguments and replaces are present" do
|
||||
'Hi, {user} of {place}'[:hi_there, 'chris', 'france'].should.equal "Hi, chris of france"
|
||||
'{computer} omg?'[:puter, 'mac'].should.equal "mac omg?"
|
||||
end
|
||||
|
||||
specify "should not affect existing string methods" do
|
||||
string = "chris"
|
||||
answer = 'ch'
|
||||
string[0..1].should.equal answer
|
||||
string[0, 2].should.equal answer
|
||||
string[0].should.equal 99
|
||||
string[/ch/].should.equal answer
|
||||
string['ch'].should.equal answer
|
||||
string['bc'].should.be.nil
|
||||
string[/dog/].should.be.nil
|
||||
end
|
||||
|
||||
specify "should return nil if a reserved key is used" do
|
||||
"string"[:limit].should.be.nil
|
||||
end
|
||||
|
||||
specify "should set default key to underscored string" do
|
||||
Gibberish.current_language = :es
|
||||
'welcome friend'[].should == '<27>Recepci<63>n, amigo!'
|
||||
end
|
||||
end
|
||||
|
||||
context "When a non-default language is set" do
|
||||
setup do
|
||||
Gibberish.current_language = :es
|
||||
end
|
||||
|
||||
specify "that language should be used" do
|
||||
Gibberish.current_language.should.equal :es
|
||||
end
|
||||
|
||||
specify "the default language should not be used" do
|
||||
Gibberish.should.not.be.default_language
|
||||
end
|
||||
|
||||
specify "a gibberish string should return itself if a corresponding key is not found" do
|
||||
string = "The internet!"
|
||||
string[:the_internet].should.equal string
|
||||
end
|
||||
|
||||
specify "a gibberish string should return a translated version of itself if a corresponding key is found" do
|
||||
"Welcome, friend!"[:welcome_friend].should.equal "<EFBFBD>Recepci<EFBFBD>n, amigo!"
|
||||
"I love Rails."[:love_rails].should.equal "Amo los carriles."
|
||||
'Welcome, {user}!'[:welcome_user, 'Marvin'].should.equal "<EFBFBD>Recepci<EFBFBD>n, Marvin!"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user