localization_generator 1.0.0 → 1.0.1
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/USAGE +5 -3
- data/localization_generator.rb +7 -0
- data/templates/README +51 -0
- data/templates/lang.yaml +2 -0
- data/templates/localization.rb +9 -4
- data/templates/localization_test.rb +47 -0
- metadata +4 -1
data/USAGE
CHANGED
@@ -2,8 +2,10 @@ NAME
|
|
2
2
|
localization - rails localization generator
|
3
3
|
|
4
4
|
SYNOPSIS
|
5
|
-
ruby script/generate localization
|
6
|
-
|
5
|
+
ruby script/generate localization class_name
|
6
|
+
|
7
|
+
Good names are Localization, LocalizationSettings.
|
8
|
+
|
7
9
|
DESCRIPTION
|
8
10
|
This generator installs a module, as originally implemented in RForum,
|
9
11
|
for localization of strings.
|
@@ -21,4 +23,4 @@ EXAMPLE
|
|
21
23
|
via the variable 'default_lang.' The value should be the prefix of the
|
22
24
|
.yaml file in the lang directory.
|
23
25
|
|
24
|
-
That's it! Thanks to Andreas Schwarz for the code.
|
26
|
+
That's it! Thanks to Andreas Schwarz for the code.
|
data/localization_generator.rb
CHANGED
@@ -12,6 +12,13 @@ class LocalizationGenerator < Rails::Generator::NamedBase
|
|
12
12
|
|
13
13
|
# Configuration
|
14
14
|
m.template "localization_environment.rb", "config/environments/#{file_name}_environment.rb"
|
15
|
+
|
16
|
+
# Tests
|
17
|
+
m.template "localization_test.rb", "test/unit/#{file_name}_test.rb"
|
18
|
+
m.template "lang.yaml", "lang/#{plural_name}.yaml"
|
19
|
+
|
20
|
+
m.template "README", "README_#{class_name.upcase}"
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
24
|
+
|
data/templates/README
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
== Installation
|
2
|
+
|
3
|
+
After generating the login system, open your application.rb controller.
|
4
|
+
The beginning of your ApplicationController should look something like this:
|
5
|
+
|
6
|
+
require '<%= singular_name %>'
|
7
|
+
|
8
|
+
class ApplicationController < ActionController::Base
|
9
|
+
include <%= class_name %>
|
10
|
+
|
11
|
+
Additionally, you'll need to includes this module as appropriate to use it
|
12
|
+
elsewhere, such as in your views. You also need to add the following at the end
|
13
|
+
of your config/environment.rb file:
|
14
|
+
|
15
|
+
require 'environments/<%= singular_name %>_environment'
|
16
|
+
require '<%= singular_name %>'
|
17
|
+
<%= class_name %>::load_localized_strings
|
18
|
+
|
19
|
+
Under the 'enviroments' subdirectory, you'll find <%= singular_name %>_environment.rb.
|
20
|
+
Edit this file as necessary...
|
21
|
+
|
22
|
+
== Requirements
|
23
|
+
|
24
|
+
You need to put your translations in the files lang/[language].yaml. Then, the
|
25
|
+
:default_language => [language] configuration variable will ensure that the
|
26
|
+
appropriate file is loaded.
|
27
|
+
|
28
|
+
== How to use it
|
29
|
+
|
30
|
+
When you want a localized string such as "localized_hello", simply create a new
|
31
|
+
entry in all supported lannguage YAML files of the form:
|
32
|
+
|
33
|
+
In en.yaml:
|
34
|
+
|
35
|
+
localized_hello: hello
|
36
|
+
|
37
|
+
In sp.yaml:
|
38
|
+
|
39
|
+
localized_hello: hola
|
40
|
+
|
41
|
+
Then in your code, you assign the string as follows:
|
42
|
+
|
43
|
+
str_var = l(:localized_hello)
|
44
|
+
|
45
|
+
In the environments/<%= singular_name %>_environment file, set the
|
46
|
+
configuration variable :default_language to either 'en' or 'sp.' That's it!
|
47
|
+
|
48
|
+
== Changelog
|
49
|
+
|
50
|
+
1.0.1 Fixed a few problems, including with test suite
|
51
|
+
1.0.0 First gem release
|
data/templates/lang.yaml
ADDED
data/templates/localization.rb
CHANGED
@@ -19,10 +19,15 @@ module <%= class_name %>
|
|
19
19
|
arguments = symbol
|
20
20
|
symbol = arguments.shift
|
21
21
|
end
|
22
|
-
|
23
|
-
|
24
|
-
LOCALIZED_STRINGS[
|
25
|
-
|
22
|
+
|
23
|
+
begin
|
24
|
+
translation = (LOCALIZED_STRINGS[language][symbol] or
|
25
|
+
LOCALIZED_STRINGS['en'][symbol] or
|
26
|
+
symbol.to_s)
|
27
|
+
rescue
|
28
|
+
translation = symbol.to_s
|
29
|
+
end
|
30
|
+
|
26
31
|
begin
|
27
32
|
return translation % arguments
|
28
33
|
rescue => e
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
4
|
+
|
5
|
+
class <%= class_name %>Test < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include <%= class_name %>
|
8
|
+
|
9
|
+
# TODO: shouldn't use production data for tests
|
10
|
+
# require File.dirname(__FILE__) + '/../test_helper'
|
11
|
+
TRANSLATION_TABLE = LOCALIZED_STRINGS['<%= plural_name %>']
|
12
|
+
CONFIG[:default_language] = '<%= plural_name %>'
|
13
|
+
|
14
|
+
def test_normal_translation
|
15
|
+
assert_equal TRANSLATION_TABLE[:available_forums], l(:available_forums)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_unknown_string
|
19
|
+
assert_equal 'test_symbol_that_has_no_traslation', l(:test_symbol_that_has_no_traslation)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_with_arguments
|
23
|
+
assert_match /an argument/, l(:registration_email_sent, 'an argument')
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_missing_an_expected_argument
|
27
|
+
assert_raise (ArgumentError) { l(:registration_email_sent) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_arguments_as_array
|
31
|
+
assert_match /an argument/, l([:registration_email_sent, 'an argument'])
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_arguments_empty_array
|
35
|
+
assert_raise(ArgumentError) { l([]) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_array_and_arguments
|
39
|
+
assert_raise(ArgumentError) { l([:registration_email_sent, 'an argument'], 'some more arguments') }
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_LString_to_s
|
43
|
+
# was a silly bug
|
44
|
+
assert_nothing_raised { LString.new(:registration_email_sent, 'an argument').to_s }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.8
|
|
3
3
|
specification_version: 1
|
4
4
|
name: localization_generator
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
6
|
+
version: 1.0.1
|
7
7
|
date: 2005-04-19
|
8
8
|
summary: "[Rails] Localization generator."
|
9
9
|
require_paths:
|
@@ -29,9 +29,12 @@ authors:
|
|
29
29
|
- Joe Hosteny
|
30
30
|
files:
|
31
31
|
- USAGE
|
32
|
+
- templates/README
|
32
33
|
- localization_generator.rb
|
33
34
|
- templates/localization.rb
|
34
35
|
- templates/localization_environment.rb
|
36
|
+
- templates/localization_test.rb
|
37
|
+
- templates/lang.yaml
|
35
38
|
test_files: []
|
36
39
|
rdoc_options: []
|
37
40
|
extra_rdoc_files: []
|