localizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +13 -0
  3. data/Rakefile +50 -0
  4. data/VERSION +1 -0
  5. data/init.rb +14 -0
  6. data/install.rb +1 -0
  7. data/lib/localizer.rb +6 -0
  8. data/lib/localizer/assets/flags/de.gif +0 -0
  9. data/lib/localizer/assets/flags/de_big.png +0 -0
  10. data/lib/localizer/assets/flags/de_medium.png +0 -0
  11. data/lib/localizer/assets/flags/en.gif +0 -0
  12. data/lib/localizer/assets/flags/en_big.png +0 -0
  13. data/lib/localizer/assets/flags/en_medium.png +0 -0
  14. data/lib/localizer/assets/flags/es.gif +0 -0
  15. data/lib/localizer/assets/flags/es_big.png +0 -0
  16. data/lib/localizer/assets/flags/es_medium.png +0 -0
  17. data/lib/localizer/assets/flags/fr.gif +0 -0
  18. data/lib/localizer/assets/flags/fr_big.png +0 -0
  19. data/lib/localizer/assets/flags/fr_medium.png +0 -0
  20. data/lib/localizer/assets/flags/gb.gif +0 -0
  21. data/lib/localizer/assets/flags/gb_big.png +0 -0
  22. data/lib/localizer/assets/flags/gb_medium.png +0 -0
  23. data/lib/localizer/assets/flags/it.gif +0 -0
  24. data/lib/localizer/assets/flags/it_big.png +0 -0
  25. data/lib/localizer/assets/flags/it_medium.png +0 -0
  26. data/lib/localizer/assets/flags/shadow.gif +0 -0
  27. data/lib/localizer/assets/flags/us.gif +0 -0
  28. data/lib/localizer/assets/flags/us_big.png +0 -0
  29. data/lib/localizer/assets/flags/us_medium.png +0 -0
  30. data/lib/localizer/config.rb +23 -0
  31. data/lib/localizer/controller.rb +21 -0
  32. data/lib/localizer/helper.rb +45 -0
  33. data/lib/localizer/routes.rb +46 -0
  34. data/tasks/localizer_tasks.rake +13 -0
  35. data/test/localizer_test.rb +8 -0
  36. data/test/test_helper.rb +3 -0
  37. data/uninstall.rb +1 -0
  38. metadata +101 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ Localizer
2
+ =========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "localizer"
8
+ gem.summary = %Q{Rails Plugin for easing simple sites localization.}
9
+ gem.description = %Q{Includes localization helpers for: routing, views and controllers.}
10
+ gem.email = "perlelia@gmail.com"
11
+ # gem.homepage = ""
12
+ gem.authors = ["Elia Schito"]
13
+
14
+ # gem.add_dependency "activesupport", "= 2.3.5"
15
+
16
+ gem.add_development_dependency "rspec", ">= 1.2.9"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ require 'rake/rdoctask'
41
+ desc 'Generate documentation for the localizer plugin.'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "Localizer #{version}"
47
+ rdoc.options << '--line-numbers' << '--inline-source'
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,14 @@
1
+ # Include hook code here
2
+ require 'localizer'
3
+
4
+ class ActionController::Base
5
+ include Localizer::Controller
6
+ end
7
+
8
+ class ActionController::Routing::RouteSet::Mapper
9
+ include Localizer::Routes
10
+ end
11
+
12
+ class ActionView::Base
13
+ include Localizer::Helper
14
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/localizer.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Localizer
2
+
3
+ require 'localizer/config'
4
+ require 'localizer/routes'
5
+ require 'localizer/controller'
6
+ require 'localizer/helper'
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,23 @@
1
+ module Localizer
2
+ class Config
3
+ attr_accessor :controllers, :locales
4
+ end
5
+
6
+ def self.config
7
+ unless @config
8
+ @config = Config.new
9
+ @config.controllers = {}
10
+ end
11
+
12
+ yield @config if block_given?
13
+ return @config
14
+ end
15
+
16
+ def self.localize! *pages
17
+ options = pages.last.kind_of?(Hash) ? pages.pop : {}
18
+ raise ArgumentError "Missing oprtion :from => controller" unless options[:from]
19
+ config.controllers[options[:from].to_s] = pages
20
+ Rails.logger.error "******" + @config.inspect
21
+ config.locales = options[:to]
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Localizer
2
+ module Controller
3
+ module InstanceMethods
4
+ private
5
+ def set_locale
6
+ I18n.locale = params[:locale].try(:to_sym) || I18n.default_locale
7
+ end
8
+
9
+ def locale
10
+ I18n.locale
11
+ end
12
+ end
13
+
14
+ def self.included(controller)
15
+ # controller.extend ClassMethods
16
+ controller.send :include, InstanceMethods
17
+ controller.send :before_filter, :set_locale
18
+ controller.helper_method :locale
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ module Localizer
2
+ module Helper
3
+ def locale
4
+ params[:locale] || 'it'
5
+ end
6
+
7
+ def localized_root
8
+ locale == 'it' ? root_path : local_root_path(locale)
9
+ end
10
+
11
+ def with_locale locale
12
+ locale_backup = I18n.locale
13
+ I18n.locale = locale
14
+ result = yield locale
15
+ I18n.locale = locale_backup
16
+ return result
17
+ end
18
+
19
+ def when_locale locale
20
+ locale_backup = I18n.locale
21
+ I18n.locale = locale
22
+ result = yield locale
23
+ I18n.locale = locale_backup
24
+ return result
25
+ end
26
+
27
+ def locales options = {}
28
+ locales = Localizer.config.locales
29
+ if options[:exclude]
30
+ locales - [ options[:exclude] ]
31
+ end
32
+ locales
33
+ end
34
+
35
+ def link_to_locale locale, name
36
+ active = (I18n.locale.to_s == locale.to_s)
37
+ link_to image_tag("flags/#{locale}.png", :title => name),
38
+ send( controller.action_name+'_path', locale ), :class => (active ? 'active' : '')
39
+ end
40
+
41
+ def localized_current_path locale
42
+ url_for(:action => nil, :locale => locale)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ module Localizer
2
+ module Routes
3
+ # Usage:
4
+ #
5
+ # include Localizer::Routes
6
+ # localize_paths map, :index, :about, :contact
7
+ # localize_path map, :home
8
+
9
+ def localize_path controller, action_name
10
+ self.with_options :controller => controller, :action => action_name do |site|
11
+ site.with_options :requirements => {:locale => /\w\w/} do |site_with_locale|
12
+ puts action_name
13
+ site_with_locale.connect ":locale" if action_name.to_s == 'index'
14
+ site_with_locale.connect ":locale/#{action_name}"
15
+ site_with_locale.connect ":locale/#{action_name}.htm"
16
+ site_with_locale.send action_name, ":locale/#{action_name}.html"
17
+ end
18
+ # site.with_options :locale => 'it' do |site|
19
+ site.connect "#{action_name}"
20
+ site.connect "#{action_name}.htm"
21
+ site.send "#{action_name}_default", "#{action_name}.html"
22
+ # end
23
+ end
24
+ end
25
+
26
+ def localize_paths_for controller, *action_names
27
+ for action_name in action_names
28
+ Rails.logger.error "******** #{controller} => #{action_name.to_s}"
29
+ localize_path controller, action_name.to_s
30
+ end
31
+ end
32
+
33
+ def include_localized_routes!
34
+ Localizer.config.controllers.each_pair do |controller, pages|
35
+ localize_paths_for controller, *pages
36
+ end
37
+ end
38
+
39
+ def localized_resource resource_name
40
+ resources resource_name, :path_prefix => ':locale',
41
+ :requirements => {:locale => Localizer.config.locales} do |resource|
42
+ yield resource if block_given?
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ # desc "Explaining what the task does"
2
+ # task :localizer do
3
+ # # Task goes here
4
+ # end
5
+
6
+ namespace :localizer do
7
+ desc "Install localizer assets in public folder."
8
+ task :assets do
9
+ require 'fileutils'
10
+ localizer_assets = File.join(File.dirname(__FILE__),'..','localizer','lib','assets')
11
+ File.cp_r File.join(localizer_assets,'flags'), File.join(Rails.public_path, 'images')
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class LocalizerTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elia Schito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-29 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: "Includes localization helpers for: routing, views and controllers."
26
+ email: perlelia@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - MIT-LICENSE
35
+ - README
36
+ - Rakefile
37
+ - VERSION
38
+ - init.rb
39
+ - install.rb
40
+ - lib/localizer.rb
41
+ - lib/localizer/assets/flags/de.gif
42
+ - lib/localizer/assets/flags/de_big.png
43
+ - lib/localizer/assets/flags/de_medium.png
44
+ - lib/localizer/assets/flags/en.gif
45
+ - lib/localizer/assets/flags/en_big.png
46
+ - lib/localizer/assets/flags/en_medium.png
47
+ - lib/localizer/assets/flags/es.gif
48
+ - lib/localizer/assets/flags/es_big.png
49
+ - lib/localizer/assets/flags/es_medium.png
50
+ - lib/localizer/assets/flags/fr.gif
51
+ - lib/localizer/assets/flags/fr_big.png
52
+ - lib/localizer/assets/flags/fr_medium.png
53
+ - lib/localizer/assets/flags/gb.gif
54
+ - lib/localizer/assets/flags/gb_big.png
55
+ - lib/localizer/assets/flags/gb_medium.png
56
+ - lib/localizer/assets/flags/it.gif
57
+ - lib/localizer/assets/flags/it_big.png
58
+ - lib/localizer/assets/flags/it_medium.png
59
+ - lib/localizer/assets/flags/shadow.gif
60
+ - lib/localizer/assets/flags/us.gif
61
+ - lib/localizer/assets/flags/us_big.png
62
+ - lib/localizer/assets/flags/us_medium.png
63
+ - lib/localizer/config.rb
64
+ - lib/localizer/controller.rb
65
+ - lib/localizer/helper.rb
66
+ - lib/localizer/routes.rb
67
+ - tasks/localizer_tasks.rake
68
+ - test/localizer_test.rb
69
+ - test/test_helper.rb
70
+ - uninstall.rb
71
+ has_rdoc: true
72
+ homepage:
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.5
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Rails Plugin for easing simple sites localization.
99
+ test_files:
100
+ - test/localizer_test.rb
101
+ - test/test_helper.rb