bai-rubyglot 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/libraries/rubyglot.rb +10 -0
- data/libraries/rubyglot/dictionary.rb +27 -0
- data/libraries/rubyglot/dictionary_definition.rb +22 -0
- data/libraries/rubyglot/i18n.rb +31 -0
- data/rakefile +56 -0
- data/readme +0 -0
- data/rubyglot.gemspec +15 -0
- data/specifications/i18n_spec.rb +33 -0
- data/specifications/spec_helper.rb +30 -0
- data/templates/dictionary.erb +17 -0
- metadata +66 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
%w(dictionary i18n).map { |x| require File.join("rubyglot", x) }
|
5
|
+
|
6
|
+
class Object
|
7
|
+
def localize(*args)
|
8
|
+
RubyGlot.lookup(*args)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RubyGlot #:nodoc:
|
2
|
+
class Dictionary #:nodoc:
|
3
|
+
attr_reader :locale, :entries
|
4
|
+
|
5
|
+
@@dictionaries = {} # defined dictionaries
|
6
|
+
|
7
|
+
def initialize(locale, &block)
|
8
|
+
@locale, @entries = locale, {}
|
9
|
+
block.call(self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def entry(original, entry)
|
13
|
+
@entries[original] = entry
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def get(identifier)
|
18
|
+
unless instance = @@dictionaries[identifier]
|
19
|
+
instance = Definitions.dictionaries[identifier]
|
20
|
+
@@dictionaries[identifier] = instance
|
21
|
+
end
|
22
|
+
|
23
|
+
instance
|
24
|
+
end
|
25
|
+
end # class << self
|
26
|
+
end # class Dictionary
|
27
|
+
end # module RubyGlot
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RubyGlot #:nodoc:
|
2
|
+
module DictionaryDefinition #:nodoc:
|
3
|
+
def self.included(base)
|
4
|
+
super
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
base.instance_eval { @dictionaries = {} }
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods #:nodoc:
|
10
|
+
# Defines a dictionary with alocale and block. The block will be evaluated to obtain all the entries for the
|
11
|
+
# dictionary.
|
12
|
+
def dictionary(locale, &block)
|
13
|
+
@dictionaries[locale] = Dictionary.new(locale, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a frozen hash of all the dictionaries that have been defined.
|
17
|
+
def dictionaries
|
18
|
+
@dictionaries.freeze
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end # module DictionaryDefinition
|
22
|
+
end # module RubyGlot
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RubyGlot #:nodoc:
|
2
|
+
@@default_locale = "en_US.UTF-8" # default locale
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def default_locale
|
6
|
+
@@default_locale
|
7
|
+
end
|
8
|
+
|
9
|
+
def default_locale=(locale)
|
10
|
+
@@default_locale = locale
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_locale
|
14
|
+
Thread.current[:locale] ||= default_locale
|
15
|
+
end
|
16
|
+
|
17
|
+
def current_locale=(locale)
|
18
|
+
Thread.current[:locale] = locale
|
19
|
+
@@dictionary = Dictionary.get(locale)
|
20
|
+
end
|
21
|
+
|
22
|
+
def dictionary
|
23
|
+
@@dictionary
|
24
|
+
end
|
25
|
+
|
26
|
+
def lookup(string_to_lookup, context = :default, *args)
|
27
|
+
localized = @@dictionary.entries[string_to_lookup][context.to_sym][:localized] rescue string_to_lookup
|
28
|
+
sprintf(localized, *args)
|
29
|
+
end
|
30
|
+
end # class << self
|
31
|
+
end # module RubyGlot
|
data/rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
%w(rubygems rake/gempackagetask spec/rake/spectask erb libraries/rubyglot pp).each { |x| require x }
|
2
|
+
|
3
|
+
Spec::Rake::SpecTask.new("specs") do |t|
|
4
|
+
t.spec_opts << '--format' << 'specdoc' << '--colour'
|
5
|
+
t.spec_files = Dir.glob("specifications/**/*_spec.rb")
|
6
|
+
end
|
7
|
+
|
8
|
+
namespace :rubyglot do
|
9
|
+
desc "Bootstrap initial language files."
|
10
|
+
task :bootstrap do
|
11
|
+
scan, storage = ENV["SCAN"], ENV["STORAGE"]
|
12
|
+
locales = ENV["LOCALES"] ? ENV["LOCALES"].split(",") : ["en_US.UTF-8"]
|
13
|
+
|
14
|
+
template = File.read(File.join("templates", "dictionary.erb"))
|
15
|
+
|
16
|
+
@strings = get_localizable_strings(scan)
|
17
|
+
|
18
|
+
locales.each do |locale|
|
19
|
+
@locale = locale
|
20
|
+
File.open("#{storage}/#{locale}.rb", "w") { |f| f.puts ERB.new(template, nil, "-").result }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Update language files for a specified locale."
|
25
|
+
task :update do
|
26
|
+
scan = ENV["SCAN"]
|
27
|
+
storage = ENV["STORAGE"]
|
28
|
+
locales = ENV["LOCALES"] ? ENV["LOCALES"].split(",") : Dir.glob("#{ENV['STORAGE']}/*.rb").collect { |f| File.basename(f).gsub(".rb", "") }
|
29
|
+
template = File.read(File.join("templates", "dictionary.erb"))
|
30
|
+
|
31
|
+
locales.each do |locale|
|
32
|
+
RubyGlot.current_locale = locale
|
33
|
+
|
34
|
+
old = RubyGlot.dictionary.entries
|
35
|
+
new = get_localizable_strings(scan)
|
36
|
+
unused = old.dup.delete_if { |key, value| new.include?(key) }
|
37
|
+
|
38
|
+
@locale = locale
|
39
|
+
@entries = old.sort_by do |key, value|
|
40
|
+
[key.downcase, key]
|
41
|
+
end.each do |key, value|
|
42
|
+
value.each do |k, v|
|
43
|
+
v[:state] = unused.has_key?(key) ? "unused" : "active"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
File.open("#{storage}/#{locale}.rb", "w") { |f| f.puts ERB.new(template, nil, "-").result }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_localizable_strings(folder)
|
53
|
+
Dir.glob("#{folder}/**/*.{erb,rb}").collect do |f|
|
54
|
+
File.read(f).scan(/localize\s*\(?(\"|\')(.*?)\1\)?/).map { |x| x[1] }
|
55
|
+
end.uniq.flatten.sort.uniq
|
56
|
+
end
|
data/readme
ADDED
File without changes
|
data/rubyglot.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rubyglot"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.date = "2008-07-07"
|
5
|
+
s.summary = "Ruby internationalization."
|
6
|
+
s.email = "beawesomeinstead@yahoo.com"
|
7
|
+
s.homepage = "http://github.com/beawesomeinstead/rubyglot/wikis"
|
8
|
+
s.description = "Simple internationalization solution for Merb/Rails."
|
9
|
+
s.authors = ["beawesomeinstead"]
|
10
|
+
s.has_rdoc = false
|
11
|
+
s.require_paths = ["libraries"]
|
12
|
+
s.files = ["libraries", "libraries/rubyglot", "libraries/rubyglot/dictionary.rb", "libraries/rubyglot/i18n.rb", "libraries/rubyglot/dictionary_definition.rb", "libraries/rubyglot.rb", "rakefile", "rubyglot.gemspec", "specifications", "specifications/i18n_spec.rb", "specifications/spec_helper.rb", "readme", "templates", "templates/dictionary.erb", "readme", "rakefile", "rubyglot.gemspec"]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Dir.glob("**/*") + ["readme", "rakefile", "rubyglot.gemspec"]
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
2
|
+
|
3
|
+
describe RubyGlot do
|
4
|
+
it "should translate strings (Original/English)" do
|
5
|
+
RubyGlot.current_locale = "en_US.UTF-8"
|
6
|
+
localize("lolcatz").should == "lolcatz localized"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should translate strings (Original/Russian)" do
|
10
|
+
RubyGlot.current_locale = "ru_RU.UTF-8"
|
11
|
+
localize("lolcatz").should == "смешные котята"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should translate strings with namespace (Original/English)" do
|
15
|
+
RubyGlot.current_locale = "en_US.UTF-8"
|
16
|
+
localize("lolcatz", "dogs").should == "loldogs"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should translate strings with namespace (Original/Russian)" do
|
20
|
+
RubyGlot.current_locale = "ru_RU.UTF-8"
|
21
|
+
localize("lolcatz", "dogs").should == "смешные собачки"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should translate strings with number formatting (Original/English)" do
|
25
|
+
RubyGlot.current_locale = "en_US.UTF-8"
|
26
|
+
localize("lol %03d dogs", "dogs", 16).should == "lol 016 dogs"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should translate strings with number formatting (Original/Russian)" do
|
30
|
+
RubyGlot.current_locale = "ru_RU.UTF-8"
|
31
|
+
localize("lol %03d dogs", "dogs", 16).should == "лол 0016 собачек"
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
%w(rubygems spec).each { |x| require x }
|
2
|
+
require File.join(File.dirname(__FILE__), "..", "libraries", "rubyglot")
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), "..", "libraries", "rubyglot", "dictionary_definition")
|
5
|
+
|
6
|
+
module RubyGlot
|
7
|
+
module Definitions
|
8
|
+
include DictionaryDefinition
|
9
|
+
|
10
|
+
dictionary "en_US.UTF-8" do |d|
|
11
|
+
d.entry "lolcatz", {
|
12
|
+
:default => { :localized => "lolcatz localized", :state => "active" },
|
13
|
+
:dogs => { :localized => "loldogs", :state => "active" }
|
14
|
+
}
|
15
|
+
d.entry "lol %03d dogs", {
|
16
|
+
:dogs => { :localized => "lol %03d dogs", :state => "active" }
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
dictionary "ru_RU.UTF-8" do |d|
|
21
|
+
d.entry "lolcatz", {
|
22
|
+
:default => { :localized => "смешные котята", :state => "active" },
|
23
|
+
:dogs => { :localized => "смешные собачки", :state => "active" }
|
24
|
+
}
|
25
|
+
d.entry "lol %03d dogs", {
|
26
|
+
:dogs => { :localized => "лол %04d собачек", :state => "active" }
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rubyglot/dictionary_definition"
|
2
|
+
|
3
|
+
module RubyGlot
|
4
|
+
module Definitions
|
5
|
+
include DictionaryDefinition
|
6
|
+
|
7
|
+
dictionary "<%= @locale %>" do |d|
|
8
|
+
<% @entries.each do |entry| -%>
|
9
|
+
d.entry "<%= entry[0] %>", {
|
10
|
+
<% (array = entry[1].to_a).each do |k, v| -%>
|
11
|
+
:<%= k %> => { :localized => "<%= v[:localized] %>", :state => "<%= v[:state] %>" }<%= "," unless k == array.last[0] %>
|
12
|
+
<% end -%>
|
13
|
+
}
|
14
|
+
<% end -%>
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bai-rubyglot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- beawesomeinstead
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Simple internationalization solution for Merb/Rails.
|
17
|
+
email: beawesomeinstead@yahoo.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- libraries
|
26
|
+
- libraries/rubyglot
|
27
|
+
- libraries/rubyglot/dictionary.rb
|
28
|
+
- libraries/rubyglot/i18n.rb
|
29
|
+
- libraries/rubyglot/dictionary_definition.rb
|
30
|
+
- libraries/rubyglot.rb
|
31
|
+
- rakefile
|
32
|
+
- rubyglot.gemspec
|
33
|
+
- specifications
|
34
|
+
- specifications/i18n_spec.rb
|
35
|
+
- specifications/spec_helper.rb
|
36
|
+
- readme
|
37
|
+
- templates
|
38
|
+
- templates/dictionary.erb
|
39
|
+
has_rdoc: false
|
40
|
+
homepage: http://github.com/beawesomeinstead/rubyglot/wikis
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- libraries
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Ruby internationalization.
|
65
|
+
test_files: []
|
66
|
+
|