localization_generator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/USAGE +24 -0
- data/localization_generator.rb +17 -0
- data/templates/localization.rb +70 -0
- data/templates/localization_environment.rb +12 -0
- metadata +51 -0
data/USAGE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
NAME
|
2
|
+
localization - rails localization generator
|
3
|
+
|
4
|
+
SYNOPSIS
|
5
|
+
ruby script/generate localization
|
6
|
+
|
7
|
+
DESCRIPTION
|
8
|
+
This generator installs a module, as originally implemented in RForum,
|
9
|
+
for localization of strings.
|
10
|
+
|
11
|
+
EXAMPLE
|
12
|
+
|
13
|
+
In order to display a localized string, you must do a few simple things.
|
14
|
+
First, instead of assigning your string as "var = str" do "var =
|
15
|
+
l(:str_label)." In your top level project directory, you will find
|
16
|
+
a subdirectory called lang. Popoulate this directory with a .yaml file
|
17
|
+
for each language you need intend to translate strings into.
|
18
|
+
The filename should be of the form [lang].yaml. Then, include
|
19
|
+
"config/environments/localization_environment" from your "config/
|
20
|
+
environment.rb" file. Finally, set the default language in this file
|
21
|
+
via the variable 'default_lang.' The value should be the prefix of the
|
22
|
+
.yaml file in the lang directory.
|
23
|
+
|
24
|
+
That's it! Thanks to Andreas Schwarz for the code.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class LocalizationGenerator < Rails::Generator::NamedBase
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
# Check for collisions
|
5
|
+
#bm.class_collisions class_path, "#{class_name}"
|
6
|
+
|
7
|
+
# Languages directory
|
8
|
+
m.directory "lang"
|
9
|
+
|
10
|
+
# Library file
|
11
|
+
m.template "localization.rb", "lib/#{file_name}.rb"
|
12
|
+
|
13
|
+
# Configuration
|
14
|
+
m.template "localization_environment.rb", "config/environments/#{file_name}_environment.rb"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'iconv'
|
2
|
+
|
3
|
+
module <%= class_name %>
|
4
|
+
|
5
|
+
LOCALIZED_STRINGS = {}
|
6
|
+
|
7
|
+
def l(symbol, *arguments)
|
8
|
+
language = CONFIG[:default_language]
|
9
|
+
|
10
|
+
symbol = symbol.to_sym if symbol.is_a? String
|
11
|
+
|
12
|
+
# translation of an LString is simply a call to to_s
|
13
|
+
return symbol.to_s if symbol.is_a? LString
|
14
|
+
|
15
|
+
# translation of an array
|
16
|
+
if symbol.is_a? Array
|
17
|
+
raise ArgumentError.new("Cannot translate an empty array") if symbol.empty?
|
18
|
+
raise ArgumentError.new("Symbol is an array, arguments must be empty") unless arguments.empty?
|
19
|
+
arguments = symbol
|
20
|
+
symbol = arguments.shift
|
21
|
+
end
|
22
|
+
|
23
|
+
translation = (LOCALIZED_STRINGS[language][symbol] or
|
24
|
+
LOCALIZED_STRINGS['en'][symbol] or
|
25
|
+
symbol.to_s)
|
26
|
+
begin
|
27
|
+
return translation % arguments
|
28
|
+
rescue => e
|
29
|
+
raise ArgumentError.new("Translation value #{translation.inspect} " +
|
30
|
+
"with arguments #{arguments.inspect} caused error '#{e.message}'")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_language?(language)
|
35
|
+
LOCALIZED_STRINGS.has_key? language
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.load_localized_strings
|
39
|
+
# Load language files
|
40
|
+
Dir[RAILS_ROOT + '/lang/*.yaml'].each do |filename|
|
41
|
+
filename =~ /([a-z]+)\.yaml$/
|
42
|
+
hash = YAML::load(File.read(filename))
|
43
|
+
file_charset = hash['file_charset'] || 'ascii'
|
44
|
+
|
45
|
+
# convert string keys to symbols
|
46
|
+
symbol_hash = Hash.new
|
47
|
+
Iconv.open(CONFIG[:web_charset], file_charset) do |i|
|
48
|
+
hash.each do |key, value|
|
49
|
+
symbol_hash[key.to_sym] = i.iconv(value)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
LOCALIZED_STRINGS[$1] = symbol_hash
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class LString
|
58
|
+
|
59
|
+
include <%= class_name %>
|
60
|
+
|
61
|
+
def initialize(symbol, arguments)
|
62
|
+
@symbol, @arguments = symbol, arguments
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_s
|
66
|
+
l(@symbol, @arguments)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.8
|
3
|
+
specification_version: 1
|
4
|
+
name: localization_generator
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2005-04-19
|
8
|
+
summary: "[Rails] Localization generator."
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: jhosteny@mac.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: Localization utility for Rails apps
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Andreas Schwarz
|
29
|
+
- Joe Hosteny
|
30
|
+
files:
|
31
|
+
- USAGE
|
32
|
+
- localization_generator.rb
|
33
|
+
- templates/localization.rb
|
34
|
+
- templates/localization_environment.rb
|
35
|
+
test_files: []
|
36
|
+
rdoc_options: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
requirements: []
|
41
|
+
dependencies:
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rails
|
44
|
+
version_requirement:
|
45
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
46
|
+
requirements:
|
47
|
+
-
|
48
|
+
- ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 0.10.0
|
51
|
+
version:
|