zlocalize 4.2.0 → 6.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dd7d0e00b29df94e177bcc0846a7baf7ad47b7e8
4
- data.tar.gz: 5ba24f8718b66e38f85816fdbc26fa5e6390bae9
2
+ SHA256:
3
+ metadata.gz: a47c635f843a5cc385159f1fa491346ba12a2503a08f17e995e25788a09f914f
4
+ data.tar.gz: e3f2502d435a73b291b20a637b2d824816f6057f451f2f3ae5f9d0364459018c
5
5
  SHA512:
6
- metadata.gz: 9e608ee25898b040885ee9fd9bfd238613ab354fe555211e86b5376bee673089824e3d84ce72ca54e8c37ec7a6e5c0921528532bb2ad90b72dc6f75bdac78abe
7
- data.tar.gz: 40836d8344fc7172bcbc7d023d934235b1a1fd75b869593b13770212e4ea3154e673fd0994329b239d8d191e67496643f27a1c677af6d73e1b6c2b963399ebe2
6
+ metadata.gz: dff3e7f314e2a344879dac586d001493464acd6c5cafa8aef784a4c4355e318a4655c5195d062a91bc02e1acceab257908e1cae63b77b1ae1b27a5db5ec10ad6
7
+ data.tar.gz: 2d28dc426ab248f8d368e7f393211f9e5dbd4f61ce15b5ab196472bcd1b3d73d873db71b6a97d7a0cb41894729040422d2fe69b702f0c8f9a244c957cd5ba2d2
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  zlocalize_path = File.expand_path(File.dirname(__FILE__))
4
2
  $:.unshift(zlocalize_path) if File.directory?(zlocalize_path) && !$:.include?(zlocalize_path)
5
3
 
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  require 'yaml'
4
2
  require 'zlocalize/translation_file'
5
3
  require 'zlocalize/config'
@@ -151,7 +149,7 @@ module ZLocalize
151
149
  end
152
150
 
153
151
  def reload!(force = false)
154
- if force || !@initialized || (self.config.reload_per_request[Rails.env.to_sym] == true)
152
+ if force || !@initialized
155
153
  @initialized = false
156
154
  @translations = nil
157
155
  @translation_procs = nil
@@ -1,31 +1,28 @@
1
- # -*- encoding : utf-8 -*-
2
1
  module ZLocalize
3
2
 
4
3
  class Config
5
4
 
6
- attr_reader :reload_per_request
7
5
  attr_reader :return_source_on_missing
8
- attr_reader :reload_per_request
9
6
  attr_reader :locales
10
7
  attr_reader :define_gettext_methods
8
+ attr_reader :harvest_paths
11
9
 
12
10
  def initialize
13
- @reload_per_request = { :development => true, :test => false, :production => false, :staging => false }
14
11
  @return_source_on_missing = { :development => true, :test => false, :production => false, :staging => false }
12
+ @harvest_paths = [
13
+ "app/channels/**/*.rb",
14
+ "app/controllers/**/*.rb",
15
+ "app/helpers/**/*.rb",
16
+ "app/models/**/*.rb",
17
+ "app/views/**/*.erb",
18
+ "app/mailers/**/*.rb",
19
+ "app/jobs/**/*.rb",
20
+ "lib/**/*.rb"
21
+ ]
15
22
  @locales = {}
16
23
  @use_global_gettext_methods = true
17
24
  end
18
25
 
19
- def reload_per_request=(value)
20
- if value === true || value === false
21
- [:development,:test,:production,:staging].each do |env|
22
- @reload_per_request[env] = value
23
- end
24
- elsif value.is_a?(Hash)
25
- @reload_per_request.merge!(value)
26
- end
27
- end
28
-
29
26
  def return_source_on_missing=(value)
30
27
  if value === true || value === false
31
28
  [:development,:test,:production,:staging].each do |env|
@@ -1,122 +1,101 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  require 'rubygems'
4
- require 'erb'
5
- require File.join(File.dirname(__FILE__),'source_parser')
2
+ require File.join(File.dirname(__FILE__),'source_processor')
6
3
  require File.join(File.dirname(__FILE__),'translation_file')
7
4
 
8
5
  module ZLocalize
9
6
 
10
- INDENT_STEP = 3
11
-
12
- class HarvesterError < StandardError
13
- end
14
-
15
- # Harvester will parse and extract all calls to translation methods in "app/models/",
16
- # "app/controllers/" and "app/views/"
17
- class Harvester
18
-
19
- attr_accessor :rails_root
20
-
21
- DEFAULT_HARVEST_OPTIONS = { :models => true, :controllers => true,
22
- :views => true, :helpers => true,
23
- :output => 'config/locales/app-strings.yml',
24
- :overwrite_existing => false,
25
- :add_paths => [],
26
- :silent => false,
27
- :purge => false }
28
-
29
- HARVEST_SECTIONS = { :models => [ "app/models/**/*.rb" ],
30
- :controllers => [ "app/controllers/**/*.rb" ],
31
- :helpers => [ "app/helpers/**/*.rb" ],
32
- :views => [ "app/views/**/*.erb", "app/views/**/*.rhtml" ],
33
- :lib => [ "lib/**/*.rb"]
34
- }
35
-
36
- def initialize(rails_root, options = {})
37
- @rails_root = rails_root
38
- @options = DEFAULT_HARVEST_OPTIONS.merge(options)
39
- end
40
-
41
- def progress(msg)
42
- print msg unless @options[:silent]
43
- end
44
-
45
- def harvest_section(section,filespecs)
46
- progress("Harvesting localizable strings from #{section}:\n")
47
- filespecs.each do |spec|
48
- progress("#{spec}: ")
49
- Dir.glob(File.join(@rails_root,spec)) do |f|
50
- progress('.')
51
- collect_entries(f,@rails_root,is_erb?(f))
52
- end
53
- progress("\n")
54
- end
55
- end
56
-
57
- def is_erb?(filename)
58
- ['.erb','.rhtml'].include?(File.extname(filename))
59
- end
60
-
61
- def harvest
62
- @new_entries = TranslationEntryCollection.new
63
- HARVEST_SECTIONS.each_pair do |section,paths|
64
- harvest_section(section,paths)
65
- progress("\n")
66
- end
67
- if @options[:add_paths].is_a?(Array) && @options.size > 0
68
- harvest_section("additional paths",@options[:add_paths])
69
- progress("\n")
70
- end
71
-
72
- @translation_file = ZLocalize::TranslationFile.new
73
- unless @options[:overwrite_existing]
74
- progress("Merging existing translations from #{@options[:output]}...\n")
75
- begin
76
- @translation_file.load(File.join(@rails_root,@options[:output]))
77
- rescue
78
- end
7
+ INDENT_STEP = 3
8
+
9
+ class HarvesterError < StandardError
10
+ end
11
+
12
+ # Harvester will parse and extract all calls to translation methods in "app/models/",
13
+ # "app/controllers/" and "app/views/"
14
+ class Harvester
15
+
16
+ attr_accessor :rails_root
17
+
18
+ DEFAULT_HARVEST_OPTIONS = { :output => 'config/locales/app-strings.yml',
19
+ :overwrite_existing => false,
20
+ :add_paths => [],
21
+ :silent => false,
22
+ :purge => false }
23
+
24
+ def initialize(rails_root, options = {})
25
+ @rails_root = rails_root
26
+ @options = { paths: ZLocalize.config.harvest_paths }.merge(DEFAULT_HARVEST_OPTIONS).merge(options).with_indifferent_access
27
+ end
28
+
29
+ def progress(msg)
30
+ print msg unless @options[:silent]
31
+ end
32
+
33
+ def harvest_path(filespec)
34
+ progress("Harvesting localizable strings from #{filespec}:\n")
35
+ Dir.glob(File.join(@rails_root,filespec)) do |f|
36
+ progress('.')
37
+ collect_entries(f,@rails_root,is_erb?(f))
38
+ end
39
+ progress("\n")
40
+ end
41
+
42
+ def is_erb?(filename)
43
+ ['.erb','.rhtml'].include?(File.extname(filename))
44
+ end
45
+
46
+ def harvest
47
+ @new_entries = TranslationEntryCollection.new
48
+ @options[:paths].each do |path|
49
+ harvest_path(path)
50
+ progress("\n")
51
+ end
52
+
53
+ @translation_file = ZLocalize::TranslationFile.new
54
+ unless @options[:overwrite_existing]
55
+ progress("Merging existing translations from #{@options[:output]}...\n")
56
+ begin
57
+ @translation_file.load(File.join(@rails_root,@options[:output]))
58
+ rescue
79
59
  end
80
-
81
- @translation_file.entries.synchronize_with(@new_entries,@options[:purge])
82
-
83
- progress("Writing new translation file...\n")
84
- File.open(File.join(@rails_root,@options[:output]),"w") do |f|
85
- f.write(@translation_file.to_yaml)
86
- end
87
-
88
- progress("Done!\n\n")
89
- end
90
-
91
- def harvest_file(filename)
92
- @new_entries = TranslationEntryCollection.new
93
- collect_entries(filename,@rails_root,false)
94
-
95
- @translation_file = ZLocalize::TranslationFile.new
96
- # merge
97
- @translation_file.load(File.join(@rails_root,@options[:output]))
98
- @translation_file.entries.synchronize_with(@new_entries,@options[:purge])
99
- File.open(File.join(@rails_root,@options[:output]),"w") do |f|
100
- f.write(@translation_file.to_yaml)
101
- end
102
- end
103
-
104
- # collect entries in a source file
105
- def collect_entries(filename,root,is_erb = false)
106
- sp = ZLocalize::SourceParser.new(filename,root,is_erb)
107
- sp.parse
108
- sp.translation_entries.each do |key,te|
109
- if @new_entries[key]
110
- te.references.each do |ref|
111
- @new_entries[key].add_reference(ref)
112
- end
113
- else
114
- @new_entries[key] = te.dup
60
+ end
61
+
62
+ @translation_file.entries.synchronize_with(@new_entries,@options[:purge])
63
+
64
+ progress("Writing new translation file...\n")
65
+ File.open(File.join(@rails_root,@options[:output]),"w") do |f|
66
+ f.write(@translation_file.to_yaml)
67
+ end
68
+ progress("Done!\n\n")
69
+ end
70
+
71
+ def harvest_file(filename)
72
+ @new_entries = TranslationEntryCollection.new
73
+ collect_entries(filename,@rails_root,false)
74
+
75
+ @translation_file = ZLocalize::TranslationFile.new
76
+ # merge
77
+ @translation_file.load(File.join(@rails_root,@options[:output]))
78
+ @translation_file.entries.synchronize_with(@new_entries,@options[:purge])
79
+ File.open(File.join(@rails_root,@options[:output]),"w") do |f|
80
+ f.write(@translation_file.to_yaml)
81
+ end
82
+ end
83
+
84
+ # collect entries in a source file
85
+ def collect_entries(filename,root,is_erb = false)
86
+ sp = ZLocalize::SourceProcessor.new(filename,root,is_erb)
87
+ sp.translation_entries.each do |key,te|
88
+ if @new_entries[key]
89
+ te.references.each do |ref|
90
+ @new_entries[key].add_reference(ref)
115
91
  end
92
+ else
93
+ @new_entries[key] = te.dup
116
94
  end
117
- end
95
+ end
96
+ end
118
97
 
119
- end
98
+ end
120
99
 
121
100
  end # module Harvester
122
101
 
@@ -1,4 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
1
 
3
2
  require 'zlocalize/rails/attached_translations'
4
3
  ActiveRecord::Base.send(:include, ZLocalize::Translatable::AttachedTranslations)
@@ -8,6 +7,3 @@ ActiveRecord::Base.send(:include, ZLocalize::Translatable::TranslatedColumns)
8
7
 
9
8
  require 'zlocalize/rails/decimal_attributes'
10
9
  ActiveRecord::Base.send(:include, ZLocalize::Translatable::LocalizedDecimalAttributes)
11
-
12
- require 'zlocalize/rails/translated_attributes_serializer'
13
- ActiveRecord::Base.send(:include, ZLocalize::Translatable::TranslatedAttributesSerializer)
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  #
4
2
  # === Translation of attributes values for ActiveRecord ===
5
3
  #
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  # == Localized Decimal Attributes ==
4
2
  #
5
3
  # Provides a way to ensure Float attributes on ActiveRecord are converted to the internal format
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  require 'rails/generators/base'
4
2
 
5
3
  # module here is deliberately named Zlocalize (as opposed to ZLocalize), for consistency
@@ -1,24 +1,20 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  # ZLocalize configuration.
4
2
 
5
3
  # ZLocalize is bound to use I18n.locale, I18n.default_locale, as to remain consistent
6
4
  # with the locale used by the Rails framework itself
7
- I18n.default_locale = :fr
5
+ I18n.default_locale = :en
8
6
 
9
7
  # define_gettext_methods will add _ and n_ methods to the Object class (so that they are globally accessible)
10
8
  # if you do not define the gettext methods, you will need to use ZLocalize.translate and ZLocalize.pluralize
11
9
  ZLocalize.config.define_gettext_methods = true
12
10
 
13
- # specify which Rails environments will reload the translation files on each request
14
- ZLocalize.config.reload_per_request = { :development => false, :test => false,
15
- :production => false, :staging => false }
11
+ # specify which Rails environments will return the source (untranslated) string when no translation is found.
12
+ # Defaults to false for all environments, meaning a missing translation will raise a ZLocalize::TranslationMissing error
13
+ ZLocalize.config.return_source_on_missing = { development: false, test: false,
14
+ production: false, staging: false }
16
15
 
17
- # specify which Rails environments will return the source string when a translation is not found
18
- # when false. Defaults to false for all environments, meaning a missing translation will raise
19
- # a ZLocalize::TranslationMissing error
20
- ZLocalize.config.return_source_on_missing = { :development => true, :test => true,
21
- :production => true, :staging => true }
16
+ # If you have additional paths to scan for translation calls, add them here.
17
+ # ZLocalize.config.harvest_paths << 'app/pdf_producers/**/*.rb'
22
18
 
23
19
  # +ZLocalize.config.locales+ is the configuration of locales supported by your application.
24
20
  # each locale must be a Hash with the configuration values for that locale.
@@ -37,16 +33,16 @@ ZLocalize.config.return_source_on_missing = { :development => true, :test => tru
37
33
  # simply return the string itself.
38
34
 
39
35
  ZLocalize.config.locales = {
40
- :en => {
41
- :plural_select => lambda { |n| n <= 0 ? 0 : (n > 1 ? 2 : 1) },
42
- :translations => File.join(Rails.root,'config/locales/en.strings.yml'),
43
- :convert_float => lambda { |s| s.to_s.gsub(',','') }
36
+ en: {
37
+ plural_select: -> (n) { n <= 0 ? 0 : (n > 1 ? 2 : 1) },
38
+ translations: File.join(Rails.root,'config/locales/en.strings.yml'),
39
+ convert_float: -> (s) { s.to_s.gsub(',','') }
44
40
  },
45
- # :fr => {
46
- # :plural_select => lambda { |n| n <= 0 ? 0 : (n > 1 ? 2 : 1) },
47
- # :translations => File.join(Rails.root,'config/locales/fr.strings.yml'),
48
- # :titleize => lambda { |s| s.capitalize.to_s },
49
- # :convert_float => lambda { |s| s.to_s.gsub(' ','').gsub(',','.') }
41
+ # fr: {
42
+ # plural_select: -> (n) { n <= 0 ? 0 : (n > 1 ? 2 : 1) },
43
+ # translations: File.join(Rails.root,'config/locales/fr.strings.yml'),
44
+ # titleize: -> (s) { s.capitalize.to_s },
45
+ # convert_float: -> (s) { s.to_s.gsub(' ','').gsub(',','.') }
50
46
  # }
51
47
  }
52
48
 
@@ -1,7 +1,6 @@
1
- # -*- encoding : utf-8 -*-
1
+ class CreateTranslationsTable < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
2
 
3
- class CreateZLocalizeTranslationsTable < ActiveRecord::Migration
4
- def self.up
3
+ def change
5
4
  create_table :translations do |t|
6
5
  t.string :translated_type
7
6
  t.integer :translated_id
@@ -11,11 +10,8 @@ class CreateZLocalizeTranslationsTable < ActiveRecord::Migration
11
10
 
12
11
  t.timestamps
13
12
  end
14
- add_index 'translations', ['translated_type','translated_id'], :name => 'index_on_translated'
15
- add_index 'translations', ['name','locale'], :name => 'index_for_lookup'
13
+ add_index 'translations', ['translated_type','translated_id'], name: 'index_on_translated'
14
+ add_index 'translations', ['name','locale'], name: 'index_for_lookup'
16
15
  end
17
16
 
18
- def self.down
19
- drop_table :translations
20
- end
21
17
  end
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  require 'rails/generators/base'
4
2
  require 'rails/generators/migration'
5
3
 
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  require 'zlocalize'
4
2
  require 'rails'
5
3
  require 'active_record'
@@ -1,6 +1,4 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
- require 'zlocalize/harvester'
1
+ require 'zlocalize/harvester'
4
2
 
5
3
  namespace :zlocalize do
6
4
 
@@ -12,18 +10,18 @@ namespace :zlocalize do
12
10
  " clear=true|false Clearing the existing translations in output file (if it exists). (default: false)\n" +
13
11
  " silent=true|false If true, do not report progress. (default: false)\n" +
14
12
  " add_paths Comma-separated list of path specs (relative to Rails.root) to harvest in addition to default paths"
15
-
13
+
16
14
  task :harvest => :environment do
17
15
  options = { :clear => ['1','true'].include?(ENV['clear']),
18
16
  :purge => ['1','true'].include?(ENV['purge']),
19
17
  :output => ENV['output'].to_s.empty? ? 'config/locales/app-strings.yml' : ENV['output'],
20
- :add_paths => ENV['add_paths'].to_s.empty? ? [] : ENV['add_paths'].split(','),
18
+ :add_paths => ENV['add_paths'].to_s.empty? ? [] : ENV['add_paths'].split(','),
21
19
  :silent => ['1','true'].include?(ENV['silent']) }
22
-
20
+
23
21
  h = ZLocalize::Harvester.new(File.expand_path(Rails.root), options)
24
22
  h.harvest
25
23
  end
26
-
24
+
27
25
  desc "Display which entries are not translated in given file\n\n" +
28
26
  "Usage: rake zlocalize:check_untranslated file=FILE_PATH\n\n" +
29
27
  "where FILE_PATH is the path to the YAML translation file relative to Rails.Root\n"
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  #
4
2
  # === Translated columns ====
5
3
  #
@@ -38,7 +36,7 @@ module ZLocalize
38
36
 
39
37
  module ClassMethods
40
38
 
41
- def translates_columns(column_names)
39
+ def translates_columns(*column_names)
42
40
 
43
41
  [column_names].flatten.each do |col_name|
44
42
  class_eval "def #{col_name}(options = {})
@@ -1,10 +1,8 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  class Translation < ActiveRecord::Base
4
2
  belongs_to :translated, :polymorphic => true
5
-
3
+
6
4
  def to_s
7
5
  return self.value
8
6
  end
9
-
7
+
10
8
  end
@@ -1,7 +1,13 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  class TranslationValidator < ActiveModel::EachValidator
4
2
 
3
+ def initialize(options = {})
4
+ # create a virtual attribute accessor for the expected translations
5
+ options[:attributes].each do |attr_name|
6
+ options[:class].attr_accessor attr_name
7
+ end
8
+ super
9
+ end
10
+
5
11
  def evaluate_required_locales(locales,record,attr_name,value)
6
12
  case locales
7
13
  when Symbol then record.send(locales)
@@ -9,7 +15,8 @@ class TranslationValidator < ActiveModel::EachValidator
9
15
  when String then [locales]
10
16
  else
11
17
  if locales.respond_to?("call")
12
- locales.call(record,attr_name,value)
18
+ args = [record,attr_name,value].slice(0,locales.arity)
19
+ locales.call(*args)
13
20
  else
14
21
  raise(
15
22
  ActiveRecord::ActiveRecordError,
@@ -21,7 +28,7 @@ class TranslationValidator < ActiveModel::EachValidator
21
28
  end
22
29
 
23
30
  def validate_each(record,attr_name,value)
24
- configuration = { :message => 'is missing its translation in {{locales}}',
31
+ configuration = { :message => :missing_translations,
25
32
  :required_locales => record.respond_to?(:get_required_locales) ? :get_required_locales : [] }
26
33
  configuration.update(options)
27
34
  locales = evaluate_required_locales(configuration[:required_locales],record,attr_name,value)
@@ -29,7 +36,7 @@ class TranslationValidator < ActiveModel::EachValidator
29
36
  if locales.is_a?(Array)
30
37
  locales.each do |loc|
31
38
  begin
32
- s = record.translate(attr_name,loc)
39
+ s = record.translate(attr_name.to_sym,loc)
33
40
  rescue ZLocalize::Translatable::TranslationError
34
41
  s = nil
35
42
  end
@@ -37,6 +44,7 @@ class TranslationValidator < ActiveModel::EachValidator
37
44
  end
38
45
  end
39
46
  if missing_locales.size > 0
47
+ # m = configuration[:message].to_s.gsub()
40
48
  record.errors.add(attr_name, configuration[:message], { :locales => missing_locales.to_sentence })
41
49
  end
42
50
  end
@@ -1,5 +1,9 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
+ # This file is no longer used starting from Ruby 2.5, since rdoc does not use ruby_lex anymore.
4
+ # see ./source_processor.rb which replaces this parser with the `parser` gem.
5
+ #
6
+
3
7
  require 'rdoc'
4
8
  require 'rdoc/options'
5
9
  require 'rdoc/ruby_lex'
@@ -0,0 +1,120 @@
1
+ require 'parser/all'
2
+ require 'action_view/template/handlers/erb'
3
+ require File.join(File.dirname(__FILE__),'translation_file')
4
+ require File.join(File.dirname(__FILE__),'harvester')
5
+
6
+ module ZLocalize
7
+
8
+ class SourceProcessor
9
+
10
+ def initialize(filename, root, is_erb = false)
11
+ @in_hash = 0
12
+ @translate_calls = []
13
+ @root = File.join(File.expand_path(root).downcase,'/') # add a trailing /
14
+ @filename = File.expand_path(filename).downcase
15
+ @relative_filename = @filename.gsub(@root,'')
16
+ content = File.open(filename, "r") { |f| f.read }
17
+ if is_erb
18
+ content = ActionView::Template::Handlers::ERB::Erubi.new(content, escape: true, trim: true).src
19
+ end
20
+ @parser = create_parser_for_ruby_version
21
+ @stree = @parser.parse(content)
22
+ process(@stree)
23
+ end
24
+
25
+ def create_parser_for_ruby_version
26
+ md = RUBY_VERSION.match(/^(\d)\.(\d)/)
27
+ begin
28
+ kls = Object.const_get('Parser').const_get("Ruby#{md[1]}#{md[2]}")
29
+ rescue
30
+ raise "Unsupported Ruby version #{RUBY_VERSION}"
31
+ end
32
+ end
33
+
34
+ def process(node)
35
+ return unless node.is_a?(AST::Node)
36
+ if node.type == :send
37
+ if node.children[0] == nil
38
+ if node.children[1] == :_
39
+ process_translate_call(node) and return
40
+ elsif node.children[1] == :n_
41
+ process_pluralize_call(node) and return
42
+ end
43
+ elsif is_zlocalize_const?(node)
44
+ if node.children[1] == :translate
45
+ process_translate_call(node) and return
46
+ elsif node.children[1] == :pluralize
47
+ process_pluralize_call(node) and return
48
+ end
49
+ end
50
+ end
51
+ node.children.each do |n|
52
+ process(n)
53
+ end
54
+ end
55
+
56
+ def is_zlocalize_const?(node)
57
+ return node.is_a?(AST::Node) && node.type == :const && node.children[1] == :ZLocalize
58
+ end
59
+
60
+ def get_string_node_value(node)
61
+ unless node.is_a?(AST::Node) && node.type == :str
62
+ raise ArgumentError.new("String Expected but got: #{node.inspect}")
63
+ end
64
+ return node.children[0]
65
+ end
66
+
67
+ def get_string_array_node_value(node)
68
+ unless node.is_a?(AST::Node) || node.type != :array
69
+ raise ArgumentError.new("Array expected but got: #{node.inspect}")
70
+ end
71
+ a = []
72
+ for i in 0..node.children.size - 1
73
+ a << get_string_node_value(node.children[i])
74
+ end
75
+ a
76
+ end
77
+
78
+ def process_translate_call(node)
79
+ @translate_calls << { name: node.children[1].to_s,
80
+ line_no: node.loc.selector.line,
81
+ char_no: node.loc.selector.column+1,
82
+ parameter: get_string_node_value(node.children[2]) }
83
+ for i in 3..node.children.size-1
84
+ process(node.children[i])
85
+ end
86
+ end
87
+
88
+ def process_pluralize_call(node)
89
+ @translate_calls << { name: node.children[1].to_s,
90
+ line_no: node.loc.selector.line,
91
+ char_no: node.loc.selector.column+1,
92
+ parameter: get_string_array_node_value(node.children[2]) }
93
+ for i in 3..node.children.size-1
94
+ process(node.children[i])
95
+ end
96
+ end
97
+
98
+ def make_translation_entry(h)
99
+ TranslationEntry.new('plural' => h[:name] == 'n_' || h[:name] == 'pluralize',
100
+ 'source' => h[:parameter],
101
+ 'references' => [ "#{@relative_filename}:#{h[:line_no]}" ])
102
+ end
103
+
104
+ # return a Hash of all translation entries we collected
105
+ def translation_entries
106
+ entries = {}
107
+ @translate_calls.each do |c|
108
+ e = make_translation_entry(c)
109
+ if entries[e.source]
110
+ entries[e.source].references += e.references
111
+ else
112
+ entries[e.source] = e
113
+ end
114
+ end
115
+ entries
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  # Memory representation of a list of translation strings in a given language
4
2
  # Reads and writes each entry with a list of references to occurences in source files.
5
3
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zlocalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 6.0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Bedard
@@ -9,64 +9,84 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-02 00:00:00.000000000 Z
12
+ date: 2020-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - '>='
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 4.1.0
20
+ version: '6.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - '>='
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: 4.1.0
27
+ version: '6.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activesupport
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 4.1.0
34
+ version: '6.0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 4.1.0
41
+ version: '6.0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: actionpack
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - '>='
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: 4.1.0
48
+ version: '6.0'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - '>='
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: 4.1.0
55
+ version: '6.0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: i18n
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 0.5.0
62
+ version: '0.7'
63
+ - - "<"
64
+ - !ruby/object:Gem::Version
65
+ version: '2'
63
66
  type: :runtime
64
67
  prerelease: false
65
68
  version_requirements: !ruby/object:Gem::Requirement
66
69
  requirements:
67
- - - '>='
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0.7'
73
+ - - "<"
68
74
  - !ruby/object:Gem::Version
69
- version: 0.5.0
75
+ version: '2'
76
+ - !ruby/object:Gem::Dependency
77
+ name: parser
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.1
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.7.1
70
90
  description:
71
91
  email:
72
92
  - zzeligg@gmail.com
@@ -75,6 +95,7 @@ executables: []
75
95
  extensions: []
76
96
  extra_rdoc_files: []
77
97
  files:
98
+ - lib/zlocalize.rb
78
99
  - lib/zlocalize/backend.rb
79
100
  - lib/zlocalize/config.rb
80
101
  - lib/zlocalize/harvester.rb
@@ -87,15 +108,15 @@ files:
87
108
  - lib/zlocalize/rails/generators/translations_migration.rb
88
109
  - lib/zlocalize/rails/railtie.rb
89
110
  - lib/zlocalize/rails/tasks/harvest.rake
90
- - lib/zlocalize/rails/translated_attributes_serializer.rb
91
111
  - lib/zlocalize/rails/translated_columns.rb
92
112
  - lib/zlocalize/rails/translation.rb
93
113
  - lib/zlocalize/rails/translation_validator.rb
94
- - lib/zlocalize/source_parser.rb
114
+ - lib/zlocalize/rdoc_source_parser.rb
115
+ - lib/zlocalize/source_processor.rb
95
116
  - lib/zlocalize/translation_file.rb
96
- - lib/zlocalize.rb
97
- homepage:
98
- licenses: []
117
+ homepage: https://github.com/zzeligg/zlocalize
118
+ licenses:
119
+ - MIT
99
120
  metadata: {}
100
121
  post_install_message:
101
122
  rdoc_options: []
@@ -103,17 +124,16 @@ require_paths:
103
124
  - lib
104
125
  required_ruby_version: !ruby/object:Gem::Requirement
105
126
  requirements:
106
- - - '>='
127
+ - - ">="
107
128
  - !ruby/object:Gem::Version
108
- version: 1.9.3
129
+ version: 2.5.0
109
130
  required_rubygems_version: !ruby/object:Gem::Requirement
110
131
  requirements:
111
- - - '>='
132
+ - - ">="
112
133
  - !ruby/object:Gem::Version
113
134
  version: '0'
114
135
  requirements: []
115
- rubyforge_project:
116
- rubygems_version: 2.0.14
136
+ rubygems_version: 3.0.3
117
137
  signing_key:
118
138
  specification_version: 4
119
139
  summary: Translation engine for Rails applications
@@ -1,58 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
- module ZLocalize
4
- module Translatable #:nodoc:
5
-
6
- module TranslatedAttributesSerializer #:nodoc:
7
-
8
- def self.included(base)
9
- base.extend(ClassMethods)
10
- end
11
-
12
- module ClassMethods
13
-
14
- def serialize_translations(attribute_names,options = {})
15
-
16
- serialize :translated_attributes, HashWithIndifferentAccess
17
-
18
- [attribute_names].flatten.each do |attr_name|
19
- class_eval "def #{attr_name}(options = {})
20
- read_translated_attribute('#{attr_name}',(options[:locale] || ZLocalize.locale).to_s)
21
- end"
22
- end
23
-
24
- class_eval "after_initialize do
25
- self.translated_attributes ||= HashWithIndifferentAccess.new
26
- end
27
-
28
- def translated_attributes=(value)
29
- self.translations = value
30
- end"
31
-
32
- include ZLocalize::Translatable::TranslatedAttributesSerializer::InstanceMethods
33
- end
34
- end
35
-
36
- module InstanceMethods
37
-
38
- def read_translated_attribute(attr_name,locale)
39
- s = self.translated_attributes[locale]
40
- s.is_a?(Hash) ? s[attr_name] : nil
41
- end
42
-
43
- alias :translate :read_translated_attribute
44
-
45
- def translations=(locales)
46
- locales.each do |locale,terms|
47
- self.translated_attributes[locale] ||= HashWithIndifferentAccess.new
48
- terms.each do |name,value|
49
- self.translated_attributes[locale][name] = value
50
- end
51
- end
52
- end
53
-
54
- end # module InstanceMethods
55
-
56
- end # module TranslatedAttributesSerializer
57
- end # module Translatable
58
- end # module ZLocalize