simple_i18n 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +92 -0
- data/Rakefile +2 -0
- data/demo_project/i18nspec.rb +8 -0
- data/demo_project/language/en +8 -0
- data/demo_project/language/zh +9 -0
- data/demo_project/lib/libfoo.rb +14 -0
- data/demo_project/test.rb +18 -0
- data/lib/simple_i18n.rb +35 -0
- data/lib/simple_i18n/language.rb +59 -0
- data/lib/simple_i18n/specification.rb +44 -0
- data/lib/simple_i18n/version.rb +3 -0
- data/simple_i18n.gemspec +20 -0
- metadata +63 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Shou Ya
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Simple I18n
|
2
|
+
|
3
|
+
Using this gem is very easy to add multilanguage support for small projects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_i18n'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_i18n
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
First you'd create a language specification file. This file is recommended to
|
22
|
+
named as `i18nspec.rb` and put in the root directory of project.
|
23
|
+
|
24
|
+
Which shall contain the content like:
|
25
|
+
|
26
|
+
require 'simple_i18n'
|
27
|
+
|
28
|
+
SimpleI18n.load_language_file('language/en')
|
29
|
+
SimpleI18n.load_language_file('language/zh')
|
30
|
+
# ... other languages
|
31
|
+
SimpleI18n.set_default_language('en')
|
32
|
+
|
33
|
+
|
34
|
+
Where you'd create a directory names `language` too. And inside there shall be
|
35
|
+
the language translation files, right name as form of `lang-en` as above.
|
36
|
+
|
37
|
+
The language files should contain the translation tables. Fortunately they are
|
38
|
+
defined in ruby also, in the form of:
|
39
|
+
|
40
|
+
# in file `en`
|
41
|
+
|
42
|
+
define_language('en', 'English') do |en|
|
43
|
+
en['display-username'] = 'Display Username'
|
44
|
+
en['reset-password'] = 'Reset Password'
|
45
|
+
# ... others translations
|
46
|
+
end
|
47
|
+
|
48
|
+
Different translation tables should define same tokens, for example:
|
49
|
+
|
50
|
+
# in file `zh`
|
51
|
+
|
52
|
+
# -*- encoding: utf-8 -*-
|
53
|
+
|
54
|
+
define_language('zh', '中文') do |zh|
|
55
|
+
zh['display-username'] = '顯示用戶名'
|
56
|
+
zh['reset-password'] = '重置密碼'
|
57
|
+
# ... blah blah blah
|
58
|
+
end
|
59
|
+
|
60
|
+
Note the encoding specification might be required for non-ascii files.
|
61
|
+
|
62
|
+
Then you can use these in your code:
|
63
|
+
|
64
|
+
# a file in project
|
65
|
+
require '../i18nspec'
|
66
|
+
|
67
|
+
def turn_to_english
|
68
|
+
swith_language('en')
|
69
|
+
end
|
70
|
+
def turn_to_chinese
|
71
|
+
switch_language('zh')
|
72
|
+
end
|
73
|
+
|
74
|
+
def foo
|
75
|
+
tr['display-username'] #=> 'Display Username' or '顯示用戶名'
|
76
|
+
end
|
77
|
+
|
78
|
+
Via refering translations with `tr` and changing current language with
|
79
|
+
`switch_language` methods whereever you need.
|
80
|
+
|
81
|
+
|
82
|
+
If there are places that you don't understand, you can move on the
|
83
|
+
demonstration project, which is located in `demo_project` directory.
|
84
|
+
And if that doesn't help too, welcome to write to me.
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require './lib/libfoo'
|
4
|
+
|
5
|
+
# default language is English as previously set
|
6
|
+
puts foo #=> 'Display Username'
|
7
|
+
|
8
|
+
switch_to_zh
|
9
|
+
puts foo #=> '顯示用戶名'
|
10
|
+
|
11
|
+
|
12
|
+
switch_to_en
|
13
|
+
puts foo #=> 'Display Username'
|
14
|
+
|
15
|
+
|
16
|
+
puts current_language #=> 'en'
|
17
|
+
puts current_language(:full_name => true) #=> 'English'
|
18
|
+
|
data/lib/simple_i18n.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
$LOAD_PATH << '.'
|
3
|
+
|
4
|
+
require 'simple_i18n/version'
|
5
|
+
require 'simple_i18n/language'
|
6
|
+
require 'simple_i18n/specification'
|
7
|
+
|
8
|
+
module SimpleI18n
|
9
|
+
class << self
|
10
|
+
include Specification
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
Kernel.class_eval do
|
15
|
+
def tr
|
16
|
+
return Language.current
|
17
|
+
end
|
18
|
+
|
19
|
+
def switch_language(lang)
|
20
|
+
unless Language.table.has_key?(lang)
|
21
|
+
warn 'Language %s is not defined.' % lang
|
22
|
+
return Language.current
|
23
|
+
end
|
24
|
+
Language.current = Language.table[lang]
|
25
|
+
end
|
26
|
+
|
27
|
+
def current_language(option={})
|
28
|
+
return option[:full_name] ?
|
29
|
+
Language.current.full_name :
|
30
|
+
Language.current.abbreviation
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
module SimpleI18n
|
3
|
+
class Language
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :default, :current
|
7
|
+
attr_accessor :table
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :abbreviation, :full_name
|
11
|
+
attr :translations
|
12
|
+
|
13
|
+
def initialize(abbr, full_name)
|
14
|
+
@translations = {}
|
15
|
+
@abbreviation = abbr
|
16
|
+
@full_name = full_name
|
17
|
+
|
18
|
+
self.class.table ||= {}
|
19
|
+
self.class.default ||= self
|
20
|
+
self.class.current ||= self
|
21
|
+
|
22
|
+
if self.class.table.has_key?(abbr)
|
23
|
+
warn 'Redefining %s language.' % abbr
|
24
|
+
end
|
25
|
+
|
26
|
+
self.class.table[abbr] = self
|
27
|
+
end
|
28
|
+
|
29
|
+
def token_defined?(token_name)
|
30
|
+
return @translations.has_key?(token_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def [](token)
|
34
|
+
tran = @translations[token]
|
35
|
+
return tran if tran
|
36
|
+
|
37
|
+
default_lang = self.class.default
|
38
|
+
|
39
|
+
if default_lang == self or (not default_lang.token_defined? token)
|
40
|
+
warn 'No token [%s] defined.' % token
|
41
|
+
return token
|
42
|
+
end
|
43
|
+
|
44
|
+
warn 'No token [%s] defined in %s, take from %s.' % \
|
45
|
+
[token, @abbreviation, default_lang.abbreviation]
|
46
|
+
return default_lang[token]
|
47
|
+
end
|
48
|
+
|
49
|
+
def []=(token, translation)
|
50
|
+
if translations.has_key?(token)
|
51
|
+
warn 'Token [%s] redefined in language %s.' % [token, @abbreviation]
|
52
|
+
end
|
53
|
+
@translations[token] = translation
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
require_relative './language'
|
3
|
+
|
4
|
+
module SimpleI18n
|
5
|
+
module Specification
|
6
|
+
|
7
|
+
def set_default_language(lang_abbr)
|
8
|
+
unless Language.table.key?(lang_abbr)
|
9
|
+
warn 'No language [%s] defined.' % lang_abbr
|
10
|
+
return
|
11
|
+
end
|
12
|
+
|
13
|
+
Language.default = Language.table[lang_abbr]
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_language_file(filename, options = {})
|
17
|
+
BasicObject.new.instance_eval do
|
18
|
+
class << self
|
19
|
+
include TranslationDefinition
|
20
|
+
end
|
21
|
+
Kernel.eval(File.read(filename), Kernel.binding)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
module TranslationDefinition
|
27
|
+
|
28
|
+
def define_language(abbreviation, full_name, &block)
|
29
|
+
lang = Language.new(abbreviation, full_name)
|
30
|
+
|
31
|
+
if block.arity == 0
|
32
|
+
lang.instance_eval(&block)
|
33
|
+
elsif block.arity == 1
|
34
|
+
block.call(lang)
|
35
|
+
else
|
36
|
+
warn 'Pass one or zero argument to `define_language\' please.'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/simple_i18n.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8; ruby -*-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/simple_i18n/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Shou Ya"]
|
7
|
+
gem.email = ["shouyatf@gmail.com"]
|
8
|
+
gem.description = 'A simple internationalization library for' \
|
9
|
+
'easily creating multilanguage ruby program'
|
10
|
+
gem.summary = gem.description
|
11
|
+
|
12
|
+
gem.homepage = "https://github.com/shouya/simple_i18n" # TODO
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($\)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.name = "simple_i18n"
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.version = SimpleI18n::VERSION
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_i18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Shou Ya
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple internationalization library foreasily creating multilanguage
|
15
|
+
ruby program
|
16
|
+
email:
|
17
|
+
- shouyatf@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- demo_project/i18nspec.rb
|
28
|
+
- demo_project/language/en
|
29
|
+
- demo_project/language/zh
|
30
|
+
- demo_project/lib/libfoo.rb
|
31
|
+
- demo_project/test.rb
|
32
|
+
- lib/simple_i18n.rb
|
33
|
+
- lib/simple_i18n/language.rb
|
34
|
+
- lib/simple_i18n/specification.rb
|
35
|
+
- lib/simple_i18n/version.rb
|
36
|
+
- simple_i18n.gemspec
|
37
|
+
homepage: https://github.com/shouya/simple_i18n
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.23
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: A simple internationalization library foreasily creating multilanguage ruby
|
61
|
+
program
|
62
|
+
test_files: []
|
63
|
+
has_rdoc:
|