Project

General

Profile

« Previous | Next » 

Revision 13

View differences:

trunk/redmine/vendor/plugins/localization/lib/localization.rb
1
# Original Localization plugin for Rails can be found at:
2
# http://mir.aculo.us/articles/2005/10/03/ruby-on-rails-i18n-revisited
3
#
4
# Slightly edited by Jean-Philippe Lang
5
# - added @@langs and modified self.define method to maintain an array of available 
6
#   langages with labels, eg. { 'en' => 'English, 'fr' => 'French }
7
# - modified self.generate_l10n_file method to retrieve already translated strings
8
#
9

  
10
module Localization
11
  mattr_accessor :lang, :langs
12
  
13
  @@l10s = { :default => {} }
14
  @@lang = :default
15
  @@langs = {}
16
  
17
  def self._(string_to_localize, *args)
18
    translated = @@l10s[@@lang][string_to_localize] || string_to_localize
19
    return translated.call(*args).to_s  if translated.is_a? Proc
20
    if translated.is_a? Array
21
      translated = if translated.size == 3 
22
        translated[args[0]==0 ? 0 : (args[0]>1 ? 2 : 1)]
23
      else
24
        translated[args[0]>1 ? 1 : 0]
25
      end
26
    end
27
    sprintf translated, *args
28
  end
29
  
30
  def self.define(lang = :default, name = :default)
31
    @@l10s[lang] ||= {}
32
    @@langs[lang] = [ name ]
33
    yield @@l10s[lang]
34
  end
35
  
36
  def self.load
37
    Dir.glob("#{RAILS_ROOT}/lang/*.rb"){ |t| require t }
38
    Dir.glob("#{RAILS_ROOT}/lang/custom/*.rb"){ |t| require t }
39
  end
40
  
41
  # Generates a best-estimate l10n file from all views by
42
  # collecting calls to _() -- note: use the generated file only
43
  # as a start (this method is only guesstimating)
44
  def self.generate_l10n_file(lang)
45
    "Localization.define('en_US') do |l|\n" <<
46
    Dir.glob("#{RAILS_ROOT}/app/views/**/*.rhtml").collect do |f| 
47
      ["# #{f}"] << File.read(f).scan(/<%.*[^\w]_\s*[\(]+[\"\'](.*?)[\"\'][\)]+/)
48
    end.uniq.flatten.collect do |g|
49
      g.starts_with?('#') ? "\n  #{g}" : "  l.store '#{g}', '#{@@l10s[lang][g]}'"
50
    end.uniq.join("\n") << "\nend"
51
  end
52
  
53
end
54

  
55
class Object
56
  def _(*args); Localization._(*args); end
57
end
trunk/redmine/vendor/plugins/localization/README
1
= Localization Plugin for Rails
2

  
3
This plugin provides a simple, gettext-like method to
4
provide localizations.
5

  
6
== Features
7

  
8
 * Any number of languages or locales
9
 * Simple method to defines singluar/plural translations
10
 * Can use lambdas to provide Ruby-code based dynamic translations
11
 * Customizable for different instances of the application
12

  
13
== Usage
14

  
15
If the localization plugin is installed, it is used automatically.
16

  
17
You need to create a /lang dir in your RAILS_ROOT.
18

  
19
The recommended way to use it is to create files that are named
20
like the languages you define in them (but you can put everything in
21
one big file too.)
22

  
23
For instance-customizable strings, add overrides in files you
24
put in /lang/custom.
25

  
26
=== Simple example:
27

  
28
Create a file /lang/translations.rb:
29
  
30
  Localization.define('de') do |l|
31
    l.store 'yes', 'Ja'
32
    l.store 'no', 'Nein'
33
  end
34
  
35
  Localization.define('fr') do |l|
36
    l.store 'yes', 'oui'
37
    l.store 'no', 'non'
38
  end
39
  
40
In your controller or application.rb:
41

  
42
  Localization.lang = 'de' # or 'fr'
43
  
44
In your view:
45

  
46
  <%=_ 'yes' %>
47
  <%=_ 'no' %>
48
  
49
Because the _ method is simply an extension to Object, you
50
can use it anywhere (models/controllers/views/libs).
51
  
52
=== Extended example:
53

  
54
Create a file /lang/default.rb with following contents:
55
  
56
  Localization.define do |l|
57
    l.store '(time)', lambda { |t| t.strftime('%I:%M%p') }
58
  end
59

  
60
Create a file /lang/de_DE.rb with following contents:
61
  
62
  Localization.define('de_DE') do |l|
63
    l.store '%d entries', ['Ein Eintrag', '%d Einträge']
64
    l.store '(time)', lambda { |t| t.strftime('%H:%M') }
65
  end
66
  
67
In your controller or application.rb:
68
  
69
  Localization.lang = 'de_DE'
70

  
71
In your view:
72

  
73
  <%=_ '%d entries', 1 %>     # singular variant is chosen 
74
  <%=_ '%d entries', 4 %>     # plural variant is chosen
75
  <%=_ '(time)', Time.now %>  # call the block with a parameter
76
  
77
== Translation file guesstimation
78

  
79
You can generate a guesstimation of all strings needed to be
80
translated in your views by first adding the _('blah') syntax
81
everywhere and then calling:
82

  
83
  puts Localization.generate_l10n_file
84
  
85
in the Rails console.
trunk/redmine/vendor/plugins/localization/init.rb
1
require "#{directory}/lib/localization.rb"
2

  
3
Localization.load

Also available in: Unified diff