merb_r18n 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -19,12 +19,14 @@ information.
19
19
  pluralization (“1 comment”, “5 comments”) in your translation.
20
20
 
21
21
  == How To
22
- 1. Add merb_r18n to your merb application in <tt>config/init.rb</tt>:
22
+
23
+ === Merb App
24
+ 1. Add merb_r18n to your merb application in <tt>config/dependencies.rb</tt>:
23
25
 
24
26
  dependency 'merb_r18n'
25
27
 
26
- 2. Add route to set locale manually in your <tt>config/router.rb</tt>. For example set
27
- available translations:
28
+ 2. Add route to set locale manually in your <tt>config/router.rb</tt>.
29
+ For example set available translations:
28
30
 
29
31
  Merb::Router.prepare do
30
32
  match('(/:locale)', :locale => /(en|ru)/) do
@@ -34,7 +36,8 @@ information.
34
36
  end
35
37
 
36
38
  You may use another way, just set :locale param.
37
- 3. Create translations file in <tt>app/i18n/</tt>. For example <tt>app/i18n/en.yml</tt>:
39
+ 3. Create translations file in <tt>app/i18n/</tt>.
40
+ For example <tt>app/i18n/en.yml</tt>:
38
41
 
39
42
  post:
40
43
  add: Add post
@@ -65,6 +68,38 @@ information.
65
68
  <% end %>
66
69
  </ul>
67
70
 
71
+ === Merb Slice
72
+ 1. Add merb_r18n to your merb slice in <tt>lib/_YOUR_SLICE_.rb</tt>:
73
+
74
+ load_dependency 'merb_r18n'
75
+
76
+ 2. Create translations file in <tt>app/i18n/</tt>.
77
+ For example <tt>app/i18n/en.yml</tt>:
78
+
79
+ post:
80
+ add: Add post
81
+ edit: Edit %1
82
+
83
+ comments: !!pl
84
+ 0: No comments
85
+ 1: One comment
86
+ n: %1 comments
87
+
88
+ 3. Use translation messages in view. For example:
89
+
90
+ <h1><%= i18n.comments(@post.comments.size) %></h1>
91
+
92
+ 4. Print localized time and numbers. For example:
93
+
94
+ <%= i18n.l @post.created_at, :date %>
95
+
96
+ 5. In host application you can override translations. Just create files in
97
+ <tt>app/i18n/</tt> in _application_ root.
98
+ For example <tt>app/i18n/en.yml</tt>:
99
+
100
+ post:
101
+ add: Write your idea
102
+
68
103
  == License
69
104
  R18n is licensed under the GNU Lesser General Public License version 3.
70
105
  You can read it in LICENSE file or in http://www.gnu.org/licenses/lgpl.html.
data/lib/merb_r18n.rb CHANGED
@@ -18,36 +18,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
  =end
19
19
 
20
20
  require 'rubygems'
21
- gem 'r18n-core', '~>0.1'
21
+ gem 'r18n-core', '~>0.2'
22
22
  require 'r18n-core'
23
23
 
24
24
  # make sure we're running inside Merb
25
- if defined?(Merb::Plugins)
26
- Merb.push_path(:i18n, Merb.root / "app" / "i18n")
27
-
25
+ if defined? Merb::Plugins
28
26
  Merb::Plugins.config[:merb_r18n] = {
29
- :default_locale => 'en',
30
- :translations_dir => Merb.dir_for(:i18n)
27
+ :default_locale => 'en'
31
28
  }
32
-
29
+ unless Merb.load_paths.include? :i18n
30
+ Merb.push_path(:i18n, Merb.root / 'app' / 'i18n')
31
+ end
32
+
33
33
  module Merb
34
34
  class Controller
35
- # Parse +http_accept_language+ and load I18n object with locales
36
- # information and translations from translations_dir. If user set locale
37
- # manual put it as last argument.
38
- before do
39
- config = Merb::Plugins.config[:merb_r18n]
40
- R18n::I18n.default = config[:default_locale]
41
-
42
- locales = R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
43
- locales.insert(0, params[:locale]) if not params[:locale].nil?
44
- R18n.set R18n::I18n.new(locales, config[:translations_dir])
45
- end
46
-
47
- # Return tool fot i18n support. It will be R18n::I18n object, see it
35
+ protected
36
+ # Return tool for i18n support. It will be R18n::I18n object, see it
48
37
  # documentation for more information.
49
38
  def i18n
50
- R18n.get
39
+ unless @i18n
40
+ R18n::I18n.default = Merb::Plugins.config[:merb_r18n][:default_locale]
41
+
42
+ locales = R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
43
+ locales.insert(0, params[:locale]) if params[:locale]
44
+
45
+ @i18n = R18n::I18n.new(locales, self.i18n_dirs)
46
+ end
47
+ @i18n
48
+ end
49
+
50
+ # Dirs to load translations
51
+ def i18n_dirs
52
+ Merb.dir_for(:i18n)
53
+ end
54
+ end
55
+ end
56
+
57
+ Merb::BootLoader.after_app_loads do
58
+ if defined? Merb::Slices
59
+ Merb::Slices.each_slice do |slice|
60
+ unless slice.slice_paths.include? :i18n
61
+ slice.push_path :i18n, slice.root_path('app' / 'i18n')
62
+ end
63
+ end
64
+
65
+ module Merb::Slices::ControllerMixin::MixinMethods::InstanceMethods
66
+ protected
67
+ def i18n_dirs
68
+ [self.slice.dir_for(:i18n), Merb.dir_for(:i18n)]
69
+ end
51
70
  end
52
71
  end
53
72
  end
data/spec/app/i18n/en.yml CHANGED
@@ -4,3 +4,5 @@ created:
4
4
  at: Created at %1
5
5
 
6
6
  other: "Translations: %1"
7
+
8
+ app: APP
@@ -0,0 +1 @@
1
+ slice: SLICE
@@ -0,0 +1,8 @@
1
+ module Slice
2
+ class Main < Merb::Controller
3
+ controller_for_slice
4
+ def index
5
+ render i18n.slice + ' and ' + i18n.app
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ if defined? Merb::Plugins
2
+ load_dependency 'merb-slices'
3
+ Merb::Slices::register(__FILE__)
4
+
5
+ module Slice
6
+ self.description = 'Test i18n for slices'
7
+ self.version = '0.0.1'
8
+ self.author = 'Andrey A.I. Sitnik'
9
+
10
+ def self.loaded; end
11
+ def self.activate; end
12
+ def self.deactivate; end
13
+
14
+ def self.setup_router(scope)
15
+ scope.default_routes
16
+ end
17
+ end
18
+
19
+ require File.dirname(__FILE__) / '../application'
20
+ end
@@ -17,5 +17,9 @@ describe "merb_r18n" do
17
17
  c = dispatch_to(I18n, :code, {:locale => "en"}, "HTTP_ACCEPT_LANGUAGE" => "ru")
18
18
  c.body.should == "en\n"
19
19
  end
20
+
21
+ it "should provide i18n support in slice controller" do
22
+ dispatch_to(Slice::Main, :index).body.should == 'SLICE and APP'
23
+ end
20
24
 
21
25
  end
data/spec/spec_helper.rb CHANGED
@@ -2,12 +2,16 @@ $TESTING=true
2
2
 
3
3
  require 'rubygems'
4
4
  require 'merb-core'
5
+ require 'merb-slices'
5
6
 
6
7
  require File.dirname(__FILE__) / '../lib/merb_r18n'
7
8
  require File.dirname(__FILE__) / 'app/controllers/i18n'
8
9
 
9
- Merb::Plugins.config[:merb_r18n][:translations_dir] =
10
- File.dirname(__FILE__) / 'app/i18n'
10
+ Merb.push_path(:i18n, File.dirname(__FILE__) / 'app/i18n')
11
+
12
+ Merb::Plugins.config[:merb_slices][:auto_register] = true
13
+ Merb::Plugins.config[:merb_slices][:search_path] = File.dirname(__FILE__) / 'app/'
14
+ Merb::Router.prepare { add_slice(:slice) }
11
15
 
12
16
  Merb.start :environment => 'test'
13
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb_r18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "A.I." Sitnik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-23 00:00:00 +03:00
12
+ date: 2008-12-06 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - "="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.1
33
+ version: "0.2"
34
34
  version:
35
35
  description: A plugin for the Merb framework that provides i18n support to translate your site in several languages. It is just a wrap for R18n core library. It can format numbers and time to the rules of the user locale, has translation for common words, storage translation in YAML format with pluralization and procedures and has special support for countries with two official languages.
36
36
  email: andrey@sitnik.ru
@@ -78,6 +78,13 @@ test_files:
78
78
  - spec/app/i18n
79
79
  - spec/app/i18n/ru.yml
80
80
  - spec/app/i18n/en.yml
81
+ - spec/app/slice
82
+ - spec/app/slice/app
83
+ - spec/app/slice/app/i18n
84
+ - spec/app/slice/app/i18n/en.yml
85
+ - spec/app/slice/lib
86
+ - spec/app/slice/lib/slice.rb
87
+ - spec/app/slice/application.rb
81
88
  - spec/app/views
82
89
  - spec/app/views/i18n
83
90
  - spec/app/views/i18n/index.html.erb