parent
c3110dcb93
commit
7a8e3ec065
@ -1,6 +1,6 @@
|
|||||||
.menu
|
.menu
|
||||||
%h1= App.title
|
%h1= App.title
|
||||||
%ul
|
%ul
|
||||||
%li= link_to "Courses"[].titleize, courses_url, :accesskey => 'd'
|
%li= link_to t(:courses).titleize, courses_url, :accesskey => 'd'
|
||||||
%li= link_to "Users"[].titleize, users_url, :accesskey => 'p'
|
%li= link_to t(:users).titleize, users_url, :accesskey => 'p'
|
||||||
%li= link_to "Mudanças recentes", log_url, :accesskey => 'r'
|
%li= link_to "Mudanças recentes", log_url, :accesskey => 'r'
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
- if session[:user_id]
|
- if session[:user_id]
|
||||||
.menu
|
.menu
|
||||||
%h1= "User"[].titleize
|
%h1= t(:user).titleize
|
||||||
%ul
|
%ul
|
||||||
%li= link_to("Dashboard"[].titleize, dashboard_path, :accesskey => '.')
|
%li= link_to(t(:dashboard).titleize, dashboard_path, :accesskey => '.')
|
||||||
%li= link_to("User profile"[].titleize, user_url(@current_user), :accesskey => ',')
|
%li= link_to(t(:user_profile).titleize, user_url(@current_user), :accesskey => ',')
|
||||||
%li= link_to("Edit settings"[].titleize, settings_url, :accesskey => 's')
|
%li= link_to(t(:edit_settings).titleize, settings_url, :accesskey => 's')
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
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.
|
|
@ -1,113 +0,0 @@
|
|||||||
= 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: ¡Hey allí!
|
|
||||||
|
|
||||||
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]
|
|
||||||
=> "¡Hey allí!"
|
|
||||||
>> 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: ¡Hey allí!
|
|
||||||
hey_name: ¡Hola {name}!
|
|
||||||
|
|
||||||
>> "Hey, {name}!"[:hey_name, 'Chris']
|
|
||||||
=> "Hey, Chris!"
|
|
||||||
>> Gibberish.current_language = :es
|
|
||||||
=> :es
|
|
||||||
>> "Hey, {name}!"[:hey_name, 'Cristóbal']
|
|
||||||
=> ¡Hola Cristó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
|
|
@ -1,3 +0,0 @@
|
|||||||
require 'gibberish'
|
|
||||||
|
|
||||||
Gibberish.load_languages!
|
|
@ -1,3 +0,0 @@
|
|||||||
welcome_friend: ¡Recepción, amigo!
|
|
||||||
welcome_user: ¡Recepción, {user}!
|
|
||||||
love_rails: Amo los carriles.
|
|
@ -1,3 +0,0 @@
|
|||||||
welcome_friend: Bienvenue, ami!
|
|
||||||
welcome_user: Bienvenue, {user}!
|
|
||||||
love_rails: J'aime des rails.
|
|
@ -1,10 +0,0 @@
|
|||||||
require 'gibberish/localize'
|
|
||||||
require 'gibberish/string_ext'
|
|
||||||
require 'gibberish/activerecord_ext'
|
|
||||||
|
|
||||||
String.send :include, Gibberish::StringExt
|
|
||||||
|
|
||||||
module Gibberish
|
|
||||||
extend Localize
|
|
||||||
end
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
|||||||
#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
|
|
@ -1,70 +0,0 @@
|
|||||||
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
|
|
@ -1,17 +0,0 @@
|
|||||||
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
|
|
@ -1,182 +0,0 @@
|
|||||||
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 == '¡Recepció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 "¡Recepción, amigo!"
|
|
||||||
"I love Rails."[:love_rails].should.equal "Amo los carriles."
|
|
||||||
'Welcome, {user}!'[:welcome_user, 'Marvin'].should.equal "¡Recepción, Marvin!"
|
|
||||||
end
|
|
||||||
end
|
|
Reference in new issue