localite 0.3 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/Manifest +44 -0
  2. data/VERSION +1 -1
  3. data/bin/list-tr +10 -0
  4. data/lib/localite.rb +161 -43
  5. data/lib/localite/filter.rb +23 -64
  6. data/lib/localite/format.rb +36 -0
  7. data/lib/localite/node_filter.rb +197 -0
  8. data/lib/localite/scopes.rb +93 -0
  9. data/lib/localite/settings.rb +139 -17
  10. data/lib/localite/storage.rb +132 -0
  11. data/lib/localite/template.rb +29 -25
  12. data/lib/localite/tr.rb +292 -0
  13. data/lib/localite/translate.rb +46 -20
  14. data/localite.gemspec +9 -6
  15. data/localite.tmproj +3 -99
  16. data/tasks/echoe.rake +1 -1
  17. data/test/i18n/de.tr +5 -0
  18. data/test/i18n/en.tr +16 -0
  19. data/test/rails/Rakefile +10 -0
  20. data/test/rails/app/controllers/application_controller.rb +10 -0
  21. data/test/rails/app/controllers/localite_controller.rb +20 -0
  22. data/test/rails/app/views/localite/index.html.erb +19 -0
  23. data/test/rails/app/views/localite/template.de.html.erb +1 -0
  24. data/test/rails/app/views/localite/template.en.html.erb +1 -0
  25. data/test/rails/config/boot.rb +110 -0
  26. data/test/rails/config/environment.rb +50 -0
  27. data/test/rails/config/environments/development.rb +17 -0
  28. data/test/rails/config/environments/production.rb +28 -0
  29. data/test/rails/config/environments/test.rb +28 -0
  30. data/test/rails/config/initializers/new_rails_defaults.rb +21 -0
  31. data/test/rails/config/initializers/session_store.rb +15 -0
  32. data/test/rails/config/locales/de.yml +4 -0
  33. data/test/rails/config/locales/en.yml +4 -0
  34. data/test/rails/config/routes.rb +13 -0
  35. data/test/rails/script/console +3 -0
  36. data/test/rails/script/server +3 -0
  37. data/test/rails/test/functional/localite_controller_test.rb +56 -0
  38. data/test/rails/test/test_helper.rb +38 -0
  39. data/test/test.rb +4 -1
  40. metadata +51 -11
  41. data/lib/localite/scope.rb +0 -140
  42. data/test/i18n/de.yml +0 -15
@@ -1,6 +1,20 @@
1
1
  require "set"
2
2
 
3
3
  module Localite::Translate
4
+
5
+ private
6
+
7
+ def log_translation(str, locale, scope, value)
8
+ value = value[0,37] + "…" if value.length > 40
9
+
10
+ msg = "Resolved #{current_scope.first(str)}"
11
+ msg += " (as #{scope.inspect})" if current_scope.first(str) != scope
12
+ msg += " to #{value.inspect} [#{locale}]"
13
+ logger.warn msg
14
+ end
15
+
16
+ public
17
+
4
18
  #
5
19
  # translate a string
6
20
  #
@@ -9,31 +23,42 @@ module Localite::Translate
9
23
  # If still no translation is found, return nil
10
24
  #
11
25
  def translate(s, raise_mode)
12
- r = do_translate(locale, s)
13
- return r if r
14
-
15
- r = do_translate(base, s) if base != locale
16
- return r if r
26
+ old_i18n_locale = I18n.locale
17
27
 
18
- raise Missing, [locale, s] if raise_mode != :no_raise
19
- end
28
+ [ current_locale, base ].uniq.each do |locale|
29
+ current_scope.each(s) do |scope|
30
+ next unless value = translate_via_i18n(locale, scope)
20
31
 
21
- private
32
+ log_translation s, locale, scope, value
33
+
34
+ #
35
+ # reformat: if target format is html, convert the value into text.
36
+ return Localite::Format.send(current_format, value)
37
+ end
38
+ end
22
39
 
23
- def do_translate(locale, s)
24
- scopes.each(s) do |scoped_string|
25
- tr = translate_via_i18n locale, scoped_string
26
- return tr if tr
40
+ src = caller[1]
41
+ if src =~ /^([^:]+:[^:]+):/
42
+ src = $1
27
43
  end
44
+ logger.warn "[#{current_locale}] Could not translate #{current_scope.first(s).inspect}; from #{src}"
28
45
 
29
- record_missing locale, scopes.first(s)
30
- nil
46
+ record_missing current_locale, current_scope.first(s)
47
+ return if raise_mode == :no_raise
48
+
49
+ raise Missing, [current_locale, s]
50
+ ensure
51
+ I18n.locale = old_i18n_locale
31
52
  end
53
+
54
+ private
32
55
 
33
- def translate_via_i18n(locale, s)
34
- locale = base unless I18n.backend.available_locales.include?(locale)
56
+ def translate_via_i18n(locale, str)
35
57
  I18n.locale = locale
36
- I18n.translate(s, :raise => true)
58
+
59
+ r = I18n.translate(str, :raise => true)
60
+ return nil if r.is_a?(Hash)
61
+ r
37
62
  rescue I18n::MissingTranslationData
38
63
  nil
39
64
  end
@@ -51,15 +76,16 @@ module Localite::Translate
51
76
  public
52
77
 
53
78
  class Missing < RuntimeError
54
- attr :locale
55
- attr :string
79
+ attr_reader :locale, :string, :scope
56
80
 
57
81
  def initialize(opts)
58
82
  @locale, @string = *opts
83
+ @scope = Localite.current_scope.to_s
84
+ # dlog "--> scope", @scope.inspect
59
85
  end
60
86
 
61
87
  def to_s
62
- "Missing translation: [#{locale}] #{string.inspect}"
88
+ "Missing translation: [#{locale}] #{string.inspect} (in scope #{scope.inspect})"
63
89
  end
64
90
  end
65
91
  end
@@ -2,27 +2,30 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{localite}
5
- s.version = "0.3"
5
+ s.version = "0.5.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["pboy"]
9
- s.date = %q{2010-05-23}
9
+ s.date = %q{2010-06-18}
10
+ s.default_executable = %q{list-tr}
10
11
  s.description = %q{An easy to use localization gem.}
11
12
  s.email = %q{eno-pboy@open-lab.org}
12
- s.extra_rdoc_files = ["lib/localite.rb", "lib/localite/filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "tasks/echoe.rake"]
13
- s.files = ["Manifest", "Rakefile", "VERSION", "config/dependencies.rb", "config/gem.yml", "lib/localite.rb", "lib/localite/filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "localite.tmproj", "script/console", "script/rebuild", "tasks/echoe.rake", "test/i18n/de.yml", "test/initializers/fake_rails.rb", "test/test.rb", "localite.gemspec"]
13
+ s.executables = ["list-tr"]
14
+ s.extra_rdoc_files = ["bin/list-tr", "lib/localite.rb", "lib/localite/filter.rb", "lib/localite/format.rb", "lib/localite/node_filter.rb", "lib/localite/scopes.rb", "lib/localite/settings.rb", "lib/localite/storage.rb", "lib/localite/template.rb", "lib/localite/tr.rb", "lib/localite/translate.rb", "tasks/echoe.rake"]
15
+ s.files = ["Manifest", "Rakefile", "VERSION", "bin/list-tr", "config/dependencies.rb", "config/gem.yml", "lib/localite.rb", "lib/localite/filter.rb", "lib/localite/format.rb", "lib/localite/node_filter.rb", "lib/localite/scopes.rb", "lib/localite/settings.rb", "lib/localite/storage.rb", "lib/localite/template.rb", "lib/localite/tr.rb", "lib/localite/translate.rb", "localite.tmproj", "script/console", "script/rebuild", "tasks/echoe.rake", "test/i18n/de.tr", "test/i18n/en.tr", "test/initializers/fake_rails.rb", "test/rails/Rakefile", "test/rails/app/controllers/application_controller.rb", "test/rails/app/controllers/localite_controller.rb", "test/rails/app/views/localite/index.html.erb", "test/rails/app/views/localite/template.de.html.erb", "test/rails/app/views/localite/template.en.html.erb", "test/rails/config/boot.rb", "test/rails/config/environment.rb", "test/rails/config/environments/development.rb", "test/rails/config/environments/production.rb", "test/rails/config/environments/test.rb", "test/rails/config/initializers/new_rails_defaults.rb", "test/rails/config/initializers/session_store.rb", "test/rails/config/locales/de.yml", "test/rails/config/locales/en.yml", "test/rails/config/routes.rb", "test/rails/script/console", "test/rails/script/server", "test/rails/test/functional/localite_controller_test.rb", "test/rails/test/test_helper.rb", "test/test.rb", "localite.gemspec"]
14
16
  s.homepage = %q{http://github.com/pboy/localite}
15
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Localite"]
16
18
  s.require_paths = ["lib"]
17
19
  s.rubyforge_project = %q{localite}
18
- s.rubygems_version = %q{1.3.6}
20
+ s.rubygems_version = %q{1.3.7}
19
21
  s.summary = %q{An easy to use localization gem.}
22
+ s.test_files = ["test/rails/test/functional/localite_controller_test.rb", "test/rails/test/test_helper.rb"]
20
23
 
21
24
  if s.respond_to? :specification_version then
22
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
26
  s.specification_version = 3
24
27
 
25
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
29
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
27
30
  else
28
31
  s.add_dependency(%q<i18n>, [">= 0"])
@@ -2,8 +2,6 @@
2
2
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
5
- <key>currentDocument</key>
6
- <string>lib/localite/filter.rb</string>
7
5
  <key>documents</key>
8
6
  <array>
9
7
  <dict>
@@ -12,7 +10,7 @@
12
10
  <key>name</key>
13
11
  <string>localite</string>
14
12
  <key>regexFolderFilter</key>
15
- <string>!.*/(\.[^/]*|CVS|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
13
+ <string>!.*/(\.[^/]*|CVS|_darcs|coverage|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
16
14
  <key>sourceDirectory</key>
17
15
  <string></string>
18
16
  </dict>
@@ -20,104 +18,10 @@
20
18
  <key>fileHierarchyDrawerWidth</key>
21
19
  <integer>339</integer>
22
20
  <key>metaData</key>
23
- <dict>
24
- <key>config/dependencies.rb</key>
25
- <dict>
26
- <key>caret</key>
27
- <dict>
28
- <key>column</key>
29
- <integer>0</integer>
30
- <key>line</key>
31
- <integer>4</integer>
32
- </dict>
33
- <key>firstVisibleColumn</key>
34
- <integer>0</integer>
35
- <key>firstVisibleLine</key>
36
- <integer>0</integer>
37
- </dict>
38
- <key>config/gem.yml</key>
39
- <dict>
40
- <key>caret</key>
41
- <dict>
42
- <key>column</key>
43
- <integer>0</integer>
44
- <key>line</key>
45
- <integer>7</integer>
46
- </dict>
47
- <key>firstVisibleColumn</key>
48
- <integer>0</integer>
49
- <key>firstVisibleLine</key>
50
- <integer>0</integer>
51
- </dict>
52
- <key>lib/localite.rb</key>
53
- <dict>
54
- <key>caret</key>
55
- <dict>
56
- <key>column</key>
57
- <integer>0</integer>
58
- <key>line</key>
59
- <integer>208</integer>
60
- </dict>
61
- <key>firstVisibleColumn</key>
62
- <integer>0</integer>
63
- <key>firstVisibleLine</key>
64
- <integer>172</integer>
65
- </dict>
66
- <key>lib/localite/filter.rb</key>
67
- <dict>
68
- <key>caret</key>
69
- <dict>
70
- <key>column</key>
71
- <integer>45</integer>
72
- <key>line</key>
73
- <integer>65</integer>
74
- </dict>
75
- <key>firstVisibleColumn</key>
76
- <integer>0</integer>
77
- <key>firstVisibleLine</key>
78
- <integer>56</integer>
79
- </dict>
80
- <key>lib/localite/template.rb</key>
81
- <dict>
82
- <key>caret</key>
83
- <dict>
84
- <key>column</key>
85
- <integer>0</integer>
86
- <key>line</key>
87
- <integer>54</integer>
88
- </dict>
89
- <key>firstVisibleColumn</key>
90
- <integer>0</integer>
91
- <key>firstVisibleLine</key>
92
- <integer>31</integer>
93
- </dict>
94
- <key>script/console</key>
95
- <dict>
96
- <key>caret</key>
97
- <dict>
98
- <key>column</key>
99
- <integer>47</integer>
100
- <key>line</key>
101
- <integer>16</integer>
102
- </dict>
103
- <key>firstVisibleColumn</key>
104
- <integer>0</integer>
105
- <key>firstVisibleLine</key>
106
- <integer>0</integer>
107
- </dict>
108
- </dict>
109
- <key>openDocuments</key>
110
- <array>
111
- <string>lib/localite.rb</string>
112
- <string>lib/localite/filter.rb</string>
113
- <string>script/console</string>
114
- <string>lib/localite/template.rb</string>
115
- <string>config/gem.yml</string>
116
- <string>config/dependencies.rb</string>
117
- </array>
21
+ <dict/>
118
22
  <key>showFileHierarchyDrawer</key>
119
23
  <true/>
120
24
  <key>windowFrame</key>
121
- <string>{{10, 12}, {924, 778}}</string>
25
+ <string>{{10, 0}, {924, 778}}</string>
122
26
  </dict>
123
27
  </plist>
@@ -38,7 +38,7 @@ if gem_config = YAML.load(File.read("#{GEM_ROOT}/config/gem.yml"))
38
38
  puts "============================================="
39
39
  puts "Installing gem..."
40
40
 
41
- system "gem install #{gem} > /dev/null 2>&1"
41
+ system "sudo gem install #{gem} > /dev/null 2>&1"
42
42
 
43
43
  puts ""
44
44
  puts "I built and installed the gem for you. To upload, run "
@@ -0,0 +1,5 @@
1
+ t: "de.t"
2
+ outer:
3
+ inner:
4
+ x1: "de/outer/inner/x1"
5
+ title: "Das ist Hypertext!"
@@ -0,0 +1,16 @@
1
+ #
2
+ # comment
3
+ param: "{* xxx *}"
4
+ base: "en_only"
5
+ t: "en.t"
6
+ #
7
+ # another comment
8
+ outer:
9
+ inner:
10
+ x1: "en/outer/inner/x1"
11
+ title: "This is hypertext"
12
+ title2: "This is <> hypertext"
13
+
14
+ outer: |
15
+ A multiline
16
+ entry
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,10 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+
8
+ # Scrub sensitive parameters from your log
9
+ # filter_parameter_logging :password
10
+ end
@@ -0,0 +1,20 @@
1
+ require "localite/filter"
2
+
3
+ class LocaliteController < ApplicationController
4
+ include Localite::Filter
5
+
6
+ def index
7
+ end
8
+
9
+ def current_locale
10
+ return "de" if action_name == "auto"
11
+ super
12
+ end
13
+
14
+ def auto
15
+ render :action => :index
16
+ end
17
+
18
+ def template
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ <h1>Localite test page</h1>
2
+
3
+ <div class="localite">
4
+ <div class="title"><%= :title.t %></div>
5
+ <div class="paragraph"><%= :paragraph.t %></div>
6
+ </div>
7
+
8
+ <div class="error">
9
+ <%=
10
+ err = begin
11
+ :error.t
12
+ rescue Localite::Translate::Missing
13
+ $!
14
+ end
15
+ %w(string locale).map do |key|
16
+ content_tag :div, key + err.send(key).inspect, :class => key
17
+ end.join("\n")
18
+ %>
19
+ </div>
@@ -0,0 +1 @@
1
+ das ist deutsch!
@@ -0,0 +1 @@
1
+ this is english!
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.2'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!