zlocalize 4.2.4 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b62af639480410b92ed5fdfa1ef387c2cc83cfc3
4
- data.tar.gz: 86e1d26113c5d63a297dc4272c592e50ce58f644
2
+ SHA256:
3
+ metadata.gz: c9a2c4bb51602f233b18d3804549d8499181462a88ed7b3ce31b3153d632aceb
4
+ data.tar.gz: a30beef5b16aca8fdef1687408b9d611c1c6c1336c5d319f850efb4c0d7be99f
5
5
  SHA512:
6
- metadata.gz: e5d11e0c6a144b59541592dbe12c0d548568a517242990cc59f0252532d0243796a245ce66ddb99d1c43e49317b02243f22eb49b6ba8fffc565e30497eb7f559
7
- data.tar.gz: 34a4f1907b5c43b12ed16660fbb9aff8a3d2d2862705ece9d07390ba01b96b735c516a6caa2f525feb91f80694ae863667374229a5378f6f134a97946627f0b2
6
+ metadata.gz: 34a8a4833c978c152a07ea806607904a8e7ae9aa78bd3812b0ed317e0ae4a02f5142a1060b59a50d8716baf9b1759fab2f6742a5389a3fccf7ba6fa986ed4b34
7
+ data.tar.gz: 3eaffd56213617be744124c261fe901dadbc6acb37b0bdc0978a53ad48a220bcc6fa1a338e0b6270c5bb7142caabcbf2e100dc302d9891aa8010768f12db4295
@@ -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,9 +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)
14
-
15
- require 'zlocalize/rails/default_locale_evaluator'
16
- ActiveRecord::Base.send(:include, ZLocalize::Translatable::DefaultLocaleEvaluator)
@@ -1,5 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
-
3
1
  #
4
2
  # === Translation of attributes values for ActiveRecord ===
5
3
  #
@@ -41,18 +39,14 @@ module ZLocalize
41
39
  end
42
40
 
43
41
  module AttachedTranslations #:nodoc:
44
-
45
42
  def self.included(base)
46
43
  base.extend(ClassMethods)
47
44
  end
48
45
 
49
46
  module ClassMethods
50
47
 
51
- def has_translations(options = {})
48
+ def has_translations
52
49
  has_many :translations, :as => :translated, :dependent => :destroy
53
-
54
- set_default_locale_for_translations(options[:default_locale])
55
-
56
50
  include ZLocalize::Translatable::AttachedTranslations::InstanceMethods
57
51
  end
58
52
 
@@ -60,18 +54,12 @@ module ZLocalize
60
54
 
61
55
  module InstanceMethods
62
56
 
63
- def translate(attr_name,locale = nil, fetch_default = true)
57
+ def translate(attr_name,locale = nil)
64
58
  locale ||= ZLocalize.locale
65
59
  if tr = find_translation(attr_name,locale)
66
- return tr.value
60
+ tr.value
67
61
  else
68
- unless (default_locale = evaluate_default_locale_for_translations).blank?
69
- if default_locale.to_s != locale.to_s
70
- if tr = find_translation(attr_name,default_locale)
71
- return tr.value
72
- end
73
- end
74
- end
62
+ ''
75
63
  end
76
64
  end
77
65
 
@@ -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,9 +36,7 @@ module ZLocalize
38
36
 
39
37
  module ClassMethods
40
38
 
41
- def translates_columns(column_names, options = {})
42
-
43
- set_default_locale_for_translations(options[:default_locale])
39
+ def translates_columns(*column_names)
44
40
 
45
41
  [column_names].flatten.each do |col_name|
46
42
  class_eval "def #{col_name}(options = {})
@@ -56,18 +52,12 @@ module ZLocalize
56
52
 
57
53
  def read_translated_column(col_name,locale,fetch_default = true)
58
54
  s = self.read_attribute("#{col_name}_#{locale}")
59
- if s.blank? && fetch_default
60
- unless (default_locale = evaluate_default_locale_for_translations).blank?
61
- if default_locale.to_s != locale.to_s
62
- attr_name = "#{col_name}_#{default_locale}"
63
- if self.respond_to?(attr_name)
64
- return self.read_attribute(attr_name)
65
- end
66
- end
67
- end
68
- else
55
+ if !s.nil?
69
56
  return s
57
+ elsif fetch_default == true
58
+ return self.read_attribute("#{col_name}_#{ZLocalize.default_locale}")
70
59
  end
60
+ nil
71
61
  end
72
62
 
73
63
  end # module InstanceMethods
@@ -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 += te.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.4
4
+ version: 6.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Bedard
@@ -9,78 +9,84 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-11-22 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
  - - ">="
68
71
  - !ruby/object:Gem::Version
69
- version: 0.5.0
72
+ version: '0.7'
73
+ - - "<"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
70
76
  - !ruby/object:Gem::Dependency
71
- name: rdoc
77
+ name: parser
72
78
  requirement: !ruby/object:Gem::Requirement
73
79
  requirements:
74
- - - "<"
80
+ - - "~>"
75
81
  - !ruby/object:Gem::Version
76
- version: '6.0'
82
+ version: 2.7.1
77
83
  type: :runtime
78
84
  prerelease: false
79
85
  version_requirements: !ruby/object:Gem::Requirement
80
86
  requirements:
81
- - - "<"
87
+ - - "~>"
82
88
  - !ruby/object:Gem::Version
83
- version: '6.0'
89
+ version: 2.7.1
84
90
  description:
85
91
  email:
86
92
  - zzeligg@gmail.com
@@ -96,21 +102,21 @@ files:
96
102
  - lib/zlocalize/rails/active_record.rb
97
103
  - lib/zlocalize/rails/attached_translations.rb
98
104
  - lib/zlocalize/rails/decimal_attributes.rb
99
- - lib/zlocalize/rails/default_locale_evaluator.rb
100
105
  - lib/zlocalize/rails/generators/initializer.rb
101
106
  - lib/zlocalize/rails/generators/templates/initializer_template.rb
102
107
  - lib/zlocalize/rails/generators/templates/translations_migration_template.rb
103
108
  - lib/zlocalize/rails/generators/translations_migration.rb
104
109
  - lib/zlocalize/rails/railtie.rb
105
110
  - lib/zlocalize/rails/tasks/harvest.rake
106
- - lib/zlocalize/rails/translated_attributes_serializer.rb
107
111
  - lib/zlocalize/rails/translated_columns.rb
108
112
  - lib/zlocalize/rails/translation.rb
109
113
  - lib/zlocalize/rails/translation_validator.rb
110
- - lib/zlocalize/source_parser.rb
114
+ - lib/zlocalize/rdoc_source_parser.rb
115
+ - lib/zlocalize/source_processor.rb
111
116
  - lib/zlocalize/translation_file.rb
112
- homepage:
113
- licenses: []
117
+ homepage: https://github.com/zzeligg/zlocalize
118
+ licenses:
119
+ - MIT
114
120
  metadata: {}
115
121
  post_install_message:
116
122
  rdoc_options: []
@@ -120,15 +126,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
126
  requirements:
121
127
  - - ">="
122
128
  - !ruby/object:Gem::Version
123
- version: 1.9.3
129
+ version: 2.5.0
124
130
  required_rubygems_version: !ruby/object:Gem::Requirement
125
131
  requirements:
126
132
  - - ">="
127
133
  - !ruby/object:Gem::Version
128
134
  version: '0'
129
135
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.6.11
136
+ rubygems_version: 3.0.3
132
137
  signing_key:
133
138
  specification_version: 4
134
139
  summary: Translation engine for Rails applications
@@ -1,44 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module ZLocalize
3
- module Translatable
4
- module DefaultLocaleEvaluator
5
-
6
- def self.included(base)
7
- base.extend ClassMethods
8
- end
9
-
10
- module ClassMethods
11
-
12
- cattr_reader :default_locale_for_translations
13
-
14
- def set_default_locale_for_translations(value)
15
- @@default_locale_for_translations =
16
- case value
17
- when nil then nil
18
- when Symbol, String then value.to_sym
19
- else
20
- raise(
21
- Zigotos::Translatable::TranslationError,
22
- "default_locale option must be either a String or a Symbol representing a method on this class (trying to set it to a #{value.class.name})"
23
- )
24
- end
25
-
26
- include ZLocalize::Translatable::DefaultLocaleEvaluator::InstanceMethods
27
- end
28
- end
29
-
30
- module InstanceMethods
31
-
32
- def evaluate_default_locale_for_translations
33
- unless self.class.default_locale_for_translations.blank?
34
- self.send(self.class.default_locale_for_translations)
35
- else
36
- nil
37
- end
38
- end
39
-
40
- end # module InstanceMethods
41
-
42
- end # module DefaultLocaleEvaluator
43
- end # module Translatable
44
- end # module ZLocalize
@@ -1,73 +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
- set_default_locale_for_translations(options[:default_locale])
19
-
20
- [attribute_names].flatten.each do |attr_name|
21
- class_eval "def #{attr_name}(options = {})
22
- read_translated_attribute('#{attr_name}',(options[:locale] || ZLocalize.locale).to_s, options[:fetch_default] == true)
23
- end"
24
- end
25
-
26
- class_eval "after_initialize do
27
- self.translated_attributes ||= HashWithIndifferentAccess.new
28
- end
29
-
30
- def translated_attributes=(value)
31
- self.translations = value
32
- end"
33
-
34
- include ZLocalize::Translatable::TranslatedAttributesSerializer::InstanceMethods
35
- end
36
-
37
- end
38
-
39
- module InstanceMethods
40
-
41
- def read_translated_attribute(attr_name,locale,fetch_default = true)
42
- s = self.translated_attributes[locale].try(:'[]',attr_name)
43
- if s.blank? && fetch_default
44
- unless (default_locale = evaluate_default_locale_for_translations).blank?
45
- if default_locale.to_s != locale.to_s
46
- return self.translated_attributes[default_locale].try(:'[]',attr_name)
47
- end
48
- end
49
- else
50
- return s
51
- end
52
- end
53
-
54
- alias :translate :read_translated_attribute
55
-
56
- def translations
57
- translated_attributes
58
- end
59
-
60
- def translations=(locales)
61
- locales.each do |locale,terms|
62
- self.translated_attributes[locale] ||= HashWithIndifferentAccess.new
63
- terms.each do |name,value|
64
- self.translated_attributes[locale][name] = value
65
- end
66
- end
67
- end
68
-
69
- end # module InstanceMethods
70
-
71
- end # module TranslatedAttributesSerializer
72
- end # module Translatable
73
- end # module ZLocalize