josevalim-localized_templates 0.3.1 → 0.4

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/README CHANGED
@@ -1,10 +1,8 @@
1
- Copyright (c) 2008 José Valim (jose.valim@gmail.com)
2
- http://www.pagestacker.com/
3
- http://josevalim.blogspot.com/
1
+ LocalizedTemplates
4
2
  License: MIT
5
- Version: 0.3.1
3
+ Version: 0.4
6
4
 
7
- LocalizedTemplates
5
+ Description
8
6
  ==================
9
7
 
10
8
  This plugin uses Rails I18n (http://rails-i18n.org/) to allow templates localization.
@@ -21,16 +19,17 @@ And you can also localize rescue files:
21
19
  If you don't want to localize a file (like a xml file), simply don't move it and it will be
22
20
  rendered as default to all languages.
23
21
 
22
+ This is an alternative to Rails 2.3 templates localization schema. If you are
23
+ localizing almost all of your views, it definitely suits you better because
24
+ your views will be better organized.
25
+
24
26
  Installation
25
27
  =================
26
28
 
27
- Install LocalizedTemplates is very easy. It is stored in GitHub, so if you have
28
- never installed a gem via GitHub run the following:
29
+ Install LocalizedTemplates is very easy. If you are running run on Rails 2.3
30
+ just do the following:
29
31
 
30
32
  gem sources -a http://gems.github.com
31
-
32
- Then install the gem:
33
-
34
33
  sudo gem install josevalim-localized_templates
35
34
 
36
35
  In RAILS_ROOT/config/environment.rb:
@@ -39,33 +38,23 @@ In RAILS_ROOT/config/environment.rb:
39
38
 
40
39
  If you want it as plugin, just do:
41
40
 
42
- cd myapp
43
- git clone git://github.com/josevalim/localized_templates.git
44
- rm -rf vendor/plugins/localized_templates/.git
45
-
46
- Adding a new locale
47
- =================
48
-
49
- To add a new locale, simply create a new file in config/locales. You can make your life easier by copying an existing
50
- locale and basing your new locale on it.
51
-
52
- Check installed locales
53
- =================
54
-
55
- To display a list of all locales installed in config/locales, you can use the following rake command:
56
-
57
- rake locales
41
+ script/plugin install git://github.com/josevalim/localized_templates.git
58
42
 
59
- Changing the default locale
60
- =================
43
+ If you are running on Rails 2.2, you should use version 0.3.1:
61
44
 
62
- If you want to use a default locale other than en-US, you have to tell the i18n plugin the locale you want to use by
63
- default. Simply put the following line in an initializer in config/initializers or include it in your environment.rb:
45
+ cd myapp
46
+ git clone git://github.com/josevalim/localized_tempaltes.git
47
+ cd vendor/plugins/localized_templates
48
+ git checkout v0.3.1
49
+ rm -rf ./.git
64
50
 
65
- I18n.default_locale = 'de-AT'
66
51
 
67
52
  Bugs and Feedback
68
53
  =================
69
54
 
70
- If you discover any bugs, please send me an e-mail to jose.valim@gmail.com
71
- If you have positive feedback, that's fine too! =)
55
+ If you discover any bugs, please send an e-mail to jose.valim@gmail.com
56
+ If you just want to give some positive feedback or drop a line, that's fine too! =)
57
+
58
+ Copyright (c) 2009 José Valim
59
+ http://www.pagestacker.com/
60
+ http://josevalim.blogspot.com/
data/Rakefile CHANGED
@@ -2,17 +2,18 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
 
5
- desc 'Run tests.'
5
+ desc 'Run tests for Localized Templates.'
6
6
  Rake::TestTask.new(:test) do |t|
7
7
  t.pattern = 'test/**/*_test.rb'
8
8
  t.verbose = true
9
9
  end
10
10
 
11
- desc 'Generate documentation.'
11
+ desc 'Generate documentation for Localized Templates.'
12
12
  Rake::RDocTask.new(:rdoc) do |rdoc|
13
13
  rdoc.rdoc_dir = 'rdoc'
14
- rdoc.title = 'SimpleLocalization'
14
+ rdoc.title = 'Localized Templates'
15
15
  rdoc.options << '--line-numbers' << '--inline-source'
16
16
  rdoc.rdoc_files.include('README')
17
+ rdoc.rdoc_files.include('MIT-LICENSE')
17
18
  rdoc.rdoc_files.include('lib/**/*.rb')
18
- end
19
+ end
@@ -1,5 +1,2 @@
1
1
  require 'localized_rescue/actioncontroller/rescue'
2
- require 'localized_templates/actioncontroller/base'
3
- require 'localized_templates/actioncontroller/layout'
4
- require 'localized_templates/actionview/base'
5
- require 'localized_templates/actionview/partials'
2
+ require 'localized_templates/actionview/paths'
@@ -0,0 +1,29 @@
1
+ module ActionView #:nodoc:
2
+ class PathSet < Array #:nodoc:
3
+
4
+ def find_template(original_template_path, format = nil)
5
+ return original_template_path if original_template_path.respond_to?(:render)
6
+ template_path = original_template_path.sub(/^\//, '')
7
+
8
+ each do |load_path|
9
+ if format && (template = load_path["#{I18n.locale}/#{template_path}.#{format}"])
10
+ return template
11
+ elsif format && (template = load_path["#{template_path}.#{format}"])
12
+ return template
13
+ elsif template = load_path["#{I18n.locale}/#{template_path}"]
14
+ return template
15
+ elsif template = load_path[template_path]
16
+ return template
17
+ # Try to find html version if the format is javascript
18
+ elsif format == :js && template = load_path["#{I18n.locale}/#{template_path}.html"]
19
+ return template
20
+ elsif format == :js && template = load_path["#{template_path}.html"]
21
+ return template
22
+ end
23
+ end
24
+
25
+ Template.new(original_template_path, self)
26
+ end
27
+
28
+ end
29
+ end
@@ -1,5 +1,4 @@
1
1
  require File.dirname(__FILE__) + '/setup'
2
- require File.dirname(__FILE__) + '/../lib/localized_rescue.rb'
3
2
 
4
3
  class LocalizedRescueController < ActionController::Base
5
4
  def index
@@ -7,7 +6,7 @@ class LocalizedRescueController < ActionController::Base
7
6
  end
8
7
  end
9
8
 
10
- class LocalizedRescueTest < Test::Unit::TestCase
9
+ class LocalizedRescueTest < ActionController::TestCase
11
10
 
12
11
  def setup
13
12
  @controller = LocalizedRescueController.new
@@ -40,4 +39,4 @@ class LocalizedRescueTest < Test::Unit::TestCase
40
39
  assert_equal body, @response.body
41
40
  end
42
41
 
43
- end
42
+ end
@@ -1,5 +1,4 @@
1
1
  require File.dirname(__FILE__) + '/setup'
2
- require File.dirname(__FILE__) + '/../lib/localized_templates.rb'
3
2
 
4
3
  class ProjectsController < ActionController::Base
5
4
  layout 'default'
@@ -16,7 +15,7 @@ class ProjectsController < ActionController::Base
16
15
  end
17
16
  end
18
17
 
19
- class LocalizedTemplatesTest < Test::Unit::TestCase
18
+ class LocalizedTemplatesTest < ActionController::TestCase
20
19
 
21
20
  def setup
22
21
  @controller = ProjectsController.new
@@ -29,7 +28,7 @@ class LocalizedTemplatesTest < Test::Unit::TestCase
29
28
  get :index
30
29
 
31
30
  assert_response 200
32
- assert_equal "<none>\n none index view\n</none>", @response.body
31
+ assert_equal "<none>noneindexview</none>", @response.body.gsub(/\s/, '')
33
32
  end
34
33
 
35
34
  def test_render_rss_without_localization
@@ -38,7 +37,7 @@ class LocalizedTemplatesTest < Test::Unit::TestCase
38
37
  get :index
39
38
 
40
39
  assert_response 200
41
- assert_equal "<none>text</none>\n", @response.body
40
+ assert_equal "<none>text</none>", @response.body.gsub(/\s/, '')
42
41
  end
43
42
 
44
43
  def test_render_with_localization
@@ -46,7 +45,7 @@ class LocalizedTemplatesTest < Test::Unit::TestCase
46
45
  get :index
47
46
 
48
47
  assert_response 200
49
- assert_equal "<pt-BR>\n pt-BR index view\n</pt-BR>", @response.body
48
+ assert_equal "<pt-BR>pt-BRindexview</pt-BR>", @response.body.gsub(/\s/, '')
50
49
  end
51
50
 
52
51
  def test_render_rss_with_localization
@@ -55,7 +54,7 @@ class LocalizedTemplatesTest < Test::Unit::TestCase
55
54
  get :index
56
55
 
57
56
  assert_response 200
58
- assert_equal "<en_US>text</en_US>\n", @response.body
57
+ assert_equal "<en_US>text</en_US>", @response.body.gsub(/\s/, '')
59
58
  end
60
59
 
61
- end
60
+ end
data/test/setup.rb CHANGED
@@ -1,15 +1,29 @@
1
1
  # Those lines are plugin test settings
2
- ENV['RAILS_ENV'] = 'test'
2
+ require 'test/unit'
3
+ require 'rubygems'
3
4
 
4
- require File.dirname(__FILE__) + '/../../../../config/environment'
5
- require 'test_help'
5
+ ENV["RAILS_ENV"] = "test"
6
+
7
+ require 'active_support'
8
+ require 'action_controller'
9
+ require 'action_controller/test_case'
10
+ require 'action_controller/test_process'
11
+
12
+ require File.dirname(__FILE__) + '/../lib/localized_templates.rb'
6
13
 
7
14
  FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures')
8
15
  ActionController::Base.view_paths = FIXTURES_PATH
9
- Rails.public_path = "#{FIXTURES_PATH}/public"
16
+
17
+ # Mock Rails! :)
18
+ #
19
+ class Rails
20
+ def self.public_path
21
+ "#{FIXTURES_PATH}/public"
22
+ end
23
+ end
10
24
 
11
25
  ActionController::Routing::Routes.draw do |map|
12
26
  map.connect ':controller/:action/:id'
13
27
  end
14
28
 
15
- I18n.locale = 'en-US'
29
+ I18n.locale = 'en-US'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-localized_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -13,7 +13,7 @@ date: 2008-12-18 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: Templates localization for Rails 2.2
16
+ description: Another templates localization schema for Rails 2.3
17
17
  email: jose.valim@gmail.com
18
18
  executables: []
19
19
 
@@ -26,13 +26,8 @@ files:
26
26
  - README
27
27
  - Rakefile
28
28
  - lib/localized_templates.rb
29
- - lib/localized_rescue/README
30
29
  - lib/localized_rescue/actioncontroller/rescue.rb
31
- - lib/localized_templates/README
32
- - lib/localized_templates/actioncontroller/base.rb
33
- - lib/localized_templates/actioncontroller/layout.rb
34
- - lib/localized_templates/actionview/base.rb
35
- - lib/localized_templates/actionview/partials.rb
30
+ - lib/localized_templates/actionview/paths.rb
36
31
  has_rdoc: true
37
32
  homepage: http://github.com/josevalim/localized_templates
38
33
  post_install_message:
@@ -59,7 +54,7 @@ rubyforge_project:
59
54
  rubygems_version: 1.2.0
60
55
  signing_key:
61
56
  specification_version: 2
62
- summary: Templates localization for Rails 2.2
57
+ summary: Another templates localization schema for Rails 2.3
63
58
  test_files:
64
59
  - test/localized_rescue_test.rb
65
60
  - test/localized_templates_test.rb
@@ -1,10 +0,0 @@
1
- Localized rescue
2
- ----------------
3
-
4
- This feature extends Rails rescue handling and allows the use of localized
5
- rescues like <code>en-US/404.html</code> on the public dir. The plugin will
6
- then pick the rescue page matching the currently used locale
7
- (<code>I18n#locale</code>).
8
-
9
- If the localized rescue page isn't found, the plugin will render the
10
- <code>404.html</code> file if it exists.
@@ -1,24 +0,0 @@
1
- Localized templates
2
- -------------------
3
-
4
- This feature extends Rails template handling and allows the use of localized
5
- templates on the root of your views folder like <code>pt-BR/projects/index.html.erb</code>.
6
- The plugin will pick the template matching the currently used locale
7
- (<code>I18n#locale</code>).
8
-
9
- If the localized template isn't found, the plugin will try to render the default
10
- <code>projects/index.html.erb</code>. If <code>index.html.erb</code> doesn't
11
- exist, a 404 will be raised.
12
-
13
- If you have a template that is the same for all languages (like a xml file)
14
- just keep it without localization: <code>index.xml.builder</code>.
15
-
16
- Theoretically, we just need to overwrite _pick_template and _pick_partial_template
17
- methods in order to have LocalizedTemplates, but memoize doesn't help
18
- since it doesn't see locale changes and would memoize values for only one locale.
19
-
20
- We can fix this by passing the locale to _pick_template as argument or creating a
21
- localized memoize, that would memoize different values based on the locale.
22
-
23
- For now, we will use the first solution: pass the locale to _pick_template. As
24
- consequence, we need to overwrite all methods that call _pick_template.
@@ -1,12 +0,0 @@
1
- module ActionController
2
- class Base
3
-
4
- private
5
- def template_exists?(template_name = default_template_name)
6
- @template.send(:_pick_template, template_name, I18n.locale) ? true : false
7
- rescue ActionView::MissingTemplate
8
- false
9
- end
10
-
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module ActionController
2
- class Base
3
-
4
- private
5
- def layout_directory?(layout_name)
6
- @template.__send__(:_pick_template, "#{File.join('layouts', layout_name)}.#{@template.template_format}", I18n.locale) ? true : false
7
- rescue ActionView::MissingTemplate
8
- false
9
- end
10
-
11
- end
12
- end
@@ -1,93 +0,0 @@
1
- module ActionView
2
- class Base
3
- @@exempt_from_layout = Set.new([/\.rjs$/])
4
-
5
- # Renders the template present at <tt>template_path</tt> (relative to the view_paths array).
6
- # The hash in <tt>local_assigns</tt> is made available as local variables.
7
- def render(options = {}, local_assigns = {}, &block) #:nodoc:
8
- local_assigns ||= {}
9
-
10
- if options.is_a?(String)
11
- ActiveSupport::Deprecation.warn(
12
- "Calling render with a string will render a partial from Rails 2.3. " +
13
- "Change this call to render(:file => '#{options}', :locals => locals_hash)."
14
- )
15
-
16
- render(:file => options, :locals => local_assigns)
17
- elsif options == :update
18
- update_page(&block)
19
- elsif options.is_a?(Hash)
20
- options = options.reverse_merge(:locals => {})
21
- if options[:layout]
22
- _render_with_layout(options, local_assigns, &block)
23
- elsif options[:file]
24
- _pick_template(options[:file], I18n.locale).render_template(self, options[:locals])
25
- elsif options[:partial]
26
- render_partial(options)
27
- elsif options[:inline]
28
- InlineTemplate.new(options[:inline], options[:type]).render(self, options[:locals])
29
- elsif options[:text]
30
- options[:text]
31
- end
32
- end
33
- end
34
-
35
- private
36
- def _pick_template(template_path, locale = nil)
37
- return template_path if template_path.respond_to?(:render)
38
-
39
- path = template_path.sub(/^\//, '')
40
- if m = path.match(/(.*)\.(\w+)$/)
41
- template_file_name, template_file_extension = m[1], m[2]
42
- else
43
- template_file_name = path
44
- end
45
-
46
- # Try to render locale/controller/action
47
- if locale && template = _template_view_path_check("#{locale}/#{template_file_name}")
48
- template
49
- # Try to render controller/action
50
- elsif template = _template_view_path_check(template_file_name)
51
- template
52
- else
53
- template = Template.new(template_path, view_paths)
54
-
55
- if self.class.warn_cache_misses && logger
56
- logger.debug "[PERFORMANCE] Rendering a template that was " +
57
- "not found in view path. Templates outside the view path are " +
58
- "not cached and result in expensive disk operations. Move this " +
59
- "file into #{view_paths.join(':')} or add the folder to your " +
60
- "view path list"
61
- end
62
-
63
- template
64
- end
65
- end
66
- memoize :_pick_template
67
-
68
- def _template_view_path_check(template_file_name)
69
- # OPTIMIZE: Checks to lookup template in view path
70
- if template = self.view_paths["#{template_file_name}.#{template_format}"]
71
- template
72
- elsif template = self.view_paths[template_file_name]
73
- template
74
- elsif (first_render = @_render_stack.first) && first_render.respond_to?(:format_and_extension) &&
75
- (template = self.view_paths["#{template_file_name}.#{first_render.format_and_extension}"])
76
- template
77
- elsif template_format == :js && template = self.view_paths["#{template_file_name}.html"]
78
- @template_format = :html
79
- template
80
- else
81
- nil
82
- end
83
- end
84
-
85
- def _exempt_from_layout?(template_path) #:nodoc:
86
- template = _pick_template(template_path, I18n.locale).to_s
87
- @@exempt_from_layout.any? { |ext| template =~ ext }
88
- rescue ActionView::MissingTemplate
89
- return false
90
- end
91
-
92
- end
93
- end
@@ -1,65 +0,0 @@
1
- module ActionView
2
- class Base
3
-
4
- private
5
- def render_partial(options = {}) #:nodoc:
6
- local_assigns = options[:locals] || {}
7
-
8
- case partial_path = options[:partial]
9
- when String, Symbol, NilClass
10
- if options.has_key?(:collection)
11
- render_partial_collection(options)
12
- else
13
- _pick_partial_template(partial_path, I18n.locale).render_partial(self, options[:object], local_assigns)
14
- end
15
- when ActionView::Helpers::FormBuilder
16
- builder_partial_path = partial_path.class.to_s.demodulize.underscore.sub(/_builder$/, '')
17
- local_assigns.merge!(builder_partial_path.to_sym => partial_path)
18
- render_partial(:partial => builder_partial_path, :object => options[:object], :locals => local_assigns)
19
- when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope
20
- render_partial_collection(options.except(:partial).merge(:collection => partial_path))
21
- else
22
- object = partial_path
23
- render_partial(
24
- :partial => ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path),
25
- :object => object,
26
- :locals => local_assigns
27
- )
28
- end
29
- end
30
-
31
- def render_partial_collection(options = {}) #:nodoc:
32
- return nil if options[:collection].blank?
33
-
34
- partial = options[:partial]
35
- spacer = options[:spacer_template] ? render(:partial => options[:spacer_template]) : ''
36
- local_assigns = options[:locals] ? options[:locals].clone : {}
37
- as = options[:as]
38
-
39
- index = 0
40
- options[:collection].map do |object|
41
- _partial_path ||= partial ||
42
- ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path)
43
- template = _pick_partial_template(_partial_path, I18n.locale)
44
- local_assigns[template.counter_name] = index
45
- result = template.render_partial(self, object, local_assigns.dup, as)
46
- index += 1
47
- result
48
- end.join(spacer)
49
- end
50
-
51
- def _pick_partial_template(partial_path, locale = nil) #:nodoc:
52
- if partial_path.include?('/')
53
- path = File.join(File.dirname(partial_path), "_#{File.basename(partial_path)}")
54
- elsif controller
55
- path = "#{controller.class.controller_path}/_#{partial_path}"
56
- else
57
- path = "_#{partial_path}"
58
- end
59
-
60
- _pick_template(path, locale)
61
- end
62
- memoize :_pick_partial_template
63
-
64
- end
65
- end