gettext_i18n_rails 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .idea
2
+ pkg/*.gem
@@ -4,7 +4,7 @@ Do all translations you want with FastGettext, use any other I18n backend as ext
4
4
 
5
5
  Rails does: `I18n.t('weir.rails.syntax.i.hate')`
6
6
  We do: `_('Just translate my damn text!')`
7
- To use I18n calls define a `weir.rails.syntax.i.hate` translation.
7
+ To use I18n calls define a `weir.rails.syntax.i.hate` translation.
8
8
 
9
9
  [See it working in the example application.](https://github.com/grosser/gettext_i18n_rails_example)
10
10
 
@@ -26,26 +26,45 @@ GetText 2.0 will render 1.93 unusable, so only install if you do not have apps t
26
26
  Copy default locales with dates/sentence-connectors/AR-errors you want from e.g.
27
27
  [rails i18n](http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/) into 'config/locales'
28
28
 
29
- #environment.rb
29
+ If you are not using bundler:
30
+
31
+ #config/environment.rb
30
32
  Rails::Initializer.run do |config|
31
33
  ...
32
34
  config.gem "fast_gettext", :version => '>=0.4.8'
33
35
  #only used for mo/po file generation in development, !do not load(:lib=>false), will needlessly eat ram!
34
36
  config.gem "gettext", :lib => false, :version => '>=1.9.3'
35
- # with bundler this would be
36
- # gem "fast_gettext", '>=0.4.8'
37
- # gem '>=1.9.3', "gettext", :require => false
38
37
  end
39
38
 
39
+ If you are using bundler:
40
+
41
+ #Gemfile
42
+ gem "fast_gettext", '>=0.4.8'
43
+ gem '>=1.9.3', "gettext", :require => false
44
+
45
+ If you installed it as a gem add to your Rakefile
46
+
47
+ #Rakefile
48
+ begin
49
+ require "gettext_i18n_rails/tasks"
50
+ rescue LoadError
51
+ puts "gettext_i18n_rails is not installed, you probably should run 'rake gems:install' or 'bundle install'."
52
+ end
53
+
54
+ To initialize:
55
+
40
56
  #config/initialisers/fast_gettext.rb
41
57
  FastGettext.add_text_domain 'app', :path => 'locale'
42
58
  FastGettext.default_available_locales = ['en','de'] #all you want to allow
43
59
  FastGettext.default_text_domain = 'app'
44
60
 
45
- #application_controller
61
+ And in your application:
62
+
63
+ #app/controllers/application_controller.rb
46
64
  class ApplicationController < ...
47
65
  before_filter :set_gettext_locale
48
66
 
67
+
49
68
  Translating
50
69
  ===========
51
70
  ###Getting started
@@ -141,4 +160,4 @@ Contributors
141
160
 
142
161
  [Michael Grosser](http://pragmatig.wordpress.com)
143
162
  grosser.michael@gmail.com
144
- Hereby placed under public domain, do what you want, just do not hold me accountable...
163
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
@@ -5,18 +5,16 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gettext_i18n_rails}
8
- s.version = "0.1.5"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
12
  s.date = %q{2010-06-19}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
- s.extra_rdoc_files = [
15
- "README.markdown"
16
- ]
17
14
  s.files = [
18
- "README.markdown",
15
+ ".gitignore",
19
16
  "Rakefile",
17
+ "Readme.md",
20
18
  "VERSION",
21
19
  "gettext_i18n_rails.gemspec",
22
20
  "init.rb",
@@ -28,6 +26,7 @@ Gem::Specification.new do |s|
28
26
  "lib/gettext_i18n_rails/i18n_hacks.rb",
29
27
  "lib/gettext_i18n_rails/model_attributes_finder.rb",
30
28
  "lib/gettext_i18n_rails/ruby_gettext_extractor.rb",
29
+ "lib/gettext_i18n_rails/tasks.rb",
31
30
  "lib/tasks/gettext_rails_i18n.rake",
32
31
  "spec/gettext_i18n_rails/action_controller_spec.rb",
33
32
  "spec/gettext_i18n_rails/active_record_spec.rb",
@@ -3,12 +3,22 @@ module I18n
3
3
 
4
4
  def locale=(new_locale)
5
5
  FastGettext.locale = new_locale
6
- if I18n.respond_to?(:config)
7
- I18n.config.locale = locale
8
- end
9
6
  end
10
7
 
11
8
  def locale
12
9
  FastGettext.locale.to_sym
13
10
  end
14
- end
11
+
12
+ # since Rails 2.3.8 a config object is used instead of just .locale
13
+ if defined? Config
14
+ class Config
15
+ def locale
16
+ FastGettext.locale.to_sym
17
+ end
18
+
19
+ def locale=(new_locale)
20
+ FastGettext.locale=(new_locale)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,111 @@
1
+ namespace :gettext do
2
+ def load_gettext
3
+ require 'gettext'
4
+ require 'gettext/utils'
5
+ end
6
+
7
+ desc "Create mo-files for L10n"
8
+ task :pack do
9
+ load_gettext
10
+ GetText.create_mofiles(true, "locale", "locale")
11
+ end
12
+
13
+ desc "Update pot/po files."
14
+ task :find do
15
+ load_gettext
16
+ $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib')
17
+ require 'gettext_i18n_rails/haml_parser'
18
+
19
+
20
+ if GetText.respond_to? :update_pofiles_org
21
+ GetText.update_pofiles_org(
22
+ text_domain(),
23
+ Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml}"),
24
+ "version 0.0.1",
25
+ :po_root => 'locale',
26
+ :msgmerge=>['--sort-output']
27
+ )
28
+ else #we are on a version < 2.0
29
+ puts "install new GetText with gettext:install to gain more features..."
30
+ #kill ar parser...
31
+ require 'gettext/parser/active_record'
32
+ module GetText
33
+ module ActiveRecordParser
34
+ module_function
35
+ def init(x);end
36
+ end
37
+ end
38
+
39
+ #parse files.. (models are simply parsed as ruby files)
40
+ GetText.update_pofiles(
41
+ textdomain,
42
+ Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml}"),
43
+ "version 0.0.1",
44
+ 'locale'
45
+ )
46
+ end
47
+ end
48
+
49
+ # This is more of an example, ignoring
50
+ # the columns/tables that mostly do not need translation.
51
+ # This can also be done with GetText::ActiveRecord
52
+ # but this crashed too often for me, and
53
+ # IMO which column should/should-not be translated does not
54
+ # belong into the model
55
+ #
56
+ # You can get your translations from GetText::ActiveRecord
57
+ # by adding this to you gettext:find task
58
+ #
59
+ # require 'active_record'
60
+ # gem "gettext_activerecord", '>=0.1.0' #download and install from github
61
+ # require 'gettext_activerecord/parser'
62
+ desc "write the locale/model_attributes.rb"
63
+ task :store_model_attributes => :environment do
64
+ FastGettext.silence_errors
65
+ require 'gettext_i18n_rails/model_attributes_finder'
66
+ storage_file = 'locale/model_attributes.rb'
67
+ puts "writing model translations to: #{storage_file}"
68
+ ignore_tables = [/^sitemap_/, /_versions$/, 'schema_migrations', 'sessions']
69
+ GettextI18nRails.store_model_attributes(
70
+ :to => storage_file,
71
+ :ignore_columns => [/_id$/, 'id', 'type', 'created_at', 'updated_at'],
72
+ :ignore_tables => ignore_tables
73
+ )
74
+ end
75
+
76
+ desc "add a new language"
77
+ task :add_language, [:language] => :environment do |_, args|
78
+ language = args.language || ENV["LANGUAGE"]
79
+
80
+ # Let's do some pre-verification of the environment.
81
+ if language.nil?
82
+ puts "You need to specify the language to add. Either 'LANGUAGE=eo rake gettext:add_languange' or 'rake gettext:add_languange[eo]'"
83
+ next
84
+ end
85
+ pot = File.join(locale_path, "#{text_domain}.pot")
86
+ if !File.exists? pot
87
+ puts "You don't have a pot file yet, you probably should run 'rake gettext:find' at least once. Tried '#{pot}'."
88
+ next
89
+ end
90
+
91
+ # Create the directory for the new language.
92
+ dir = File.join(locale_path, language)
93
+ puts "Creating directory #{dir}"
94
+ Dir.mkdir dir
95
+
96
+ # Create the po file for the new language.
97
+ new_po = File.join(locale_path, language, "#{text_domain}.po")
98
+ puts "Initializing #{new_po} from #{pot}."
99
+ system "msginit --locale=#{language} --input=#{pot} --output=#{new_po}"
100
+ end
101
+
102
+ def locale_path
103
+ FastGettext.translation_repositories[text_domain].instance_variable_get(:@options)[:path]
104
+ rescue
105
+ File.join(RAILS_ROOT, "locale")
106
+ end
107
+
108
+ def text_domain
109
+ ENV['TEXTDOMAIN'] || FastGettext.text_domain || "app"
110
+ end
111
+ end
@@ -1,77 +1 @@
1
- namespace :gettext do
2
- def load_gettext
3
- require 'gettext'
4
- require 'gettext/utils'
5
- end
6
-
7
- desc "Create mo-files for L10n"
8
- task :pack do
9
- load_gettext
10
- GetText.create_mofiles(true, "locale", "locale")
11
- end
12
-
13
- desc "Update pot/po files."
14
- task :find do
15
- load_gettext
16
- $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib')
17
- require 'gettext_i18n_rails/haml_parser'
18
-
19
- textdomain = (ENV['TEXTDOMAIN'] || "app")
20
-
21
-
22
- if GetText.respond_to? :update_pofiles_org
23
- GetText.update_pofiles_org(
24
- textdomain,
25
- Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml}"),
26
- "version 0.0.1",
27
- :po_root => 'locale',
28
- :msgmerge=>['--sort-output']
29
- )
30
- else #we are on a version < 2.0
31
- puts "install new GetText with gettext:install to gain more features..."
32
- #kill ar parser...
33
- require 'gettext/parser/active_record'
34
- module GetText
35
- module ActiveRecordParser
36
- module_function
37
- def init(x);end
38
- end
39
- end
40
-
41
- #parse files.. (models are simply parsed as ruby files)
42
- GetText.update_pofiles(
43
- textdomain,
44
- Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml}"),
45
- "version 0.0.1",
46
- 'locale'
47
- )
48
- end
49
- end
50
-
51
- # This is more of an example, ignoring
52
- # the columns/tables that mostly do not need translation.
53
- # This can also be done with GetText::ActiveRecord
54
- # but this crashed too often for me, and
55
- # IMO which column should/should-not be translated does not
56
- # belong into the model
57
- #
58
- # You can get your translations from GetText::ActiveRecord
59
- # by adding this to you gettext:find task
60
- #
61
- # require 'active_record'
62
- # gem "gettext_activerecord", '>=0.1.0' #download and install from github
63
- # require 'gettext_activerecord/parser'
64
- desc "write the locale/model_attributes.rb"
65
- task :store_model_attributes => :environment do
66
- FastGettext.silence_errors
67
- require 'gettext_i18n_rails/model_attributes_finder'
68
- storage_file = 'locale/model_attributes.rb'
69
- puts "writing model translations to: #{storage_file}"
70
- ignore_tables = [/^sitemap_/, /_versions$/, 'schema_migrations', 'sessions']
71
- GettextI18nRails.store_model_attributes(
72
- :to => storage_file,
73
- :ignore_columns => [/_id$/, 'id', 'type', 'created_at', 'updated_at'],
74
- :ignore_tables => ignore_tables
75
- )
76
- end
77
- end
1
+ require File.join(File.dirname(__FILE__), "/../gettext_i18n_rails/tasks")
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Grosser
@@ -35,11 +35,12 @@ executables: []
35
35
 
36
36
  extensions: []
37
37
 
38
- extra_rdoc_files:
39
- - README.markdown
38
+ extra_rdoc_files: []
39
+
40
40
  files:
41
- - README.markdown
41
+ - .gitignore
42
42
  - Rakefile
43
+ - Readme.md
43
44
  - VERSION
44
45
  - gettext_i18n_rails.gemspec
45
46
  - init.rb
@@ -51,6 +52,7 @@ files:
51
52
  - lib/gettext_i18n_rails/i18n_hacks.rb
52
53
  - lib/gettext_i18n_rails/model_attributes_finder.rb
53
54
  - lib/gettext_i18n_rails/ruby_gettext_extractor.rb
55
+ - lib/gettext_i18n_rails/tasks.rb
54
56
  - lib/tasks/gettext_rails_i18n.rake
55
57
  - spec/gettext_i18n_rails/action_controller_spec.rb
56
58
  - spec/gettext_i18n_rails/active_record_spec.rb