Permalink
Browse files

--Backporting for-1.2 features

  • Loading branch information...
1 parent 41614b5 commit 4cc24938ea2c8708fd95c8ea6bbe8f217134b198 @saimonmoore saimonmoore committed Mar 12, 2007
View
@@ -78,8 +78,8 @@ See the wiki (http://www.globalize-rails.org/) for more documentation.
From your rails app root directory:
-1. <tt>script/plugin install http://svn.globalize-rails.org/svn/globalize/globalize/trunk</tt>
-1. <tt>rake globalize:setup</tt> (might take a while, about a minute or so)
+1. <tt>script/plugin install http://svn.globalize-rails.org/svn/globalize/trunk</tt>
+2. <tt>rake globalize:setup</tt> (might take a while, about a minute or so)
...and you're globalized, dude!
@@ -1,10 +1,86 @@
+Usage: script/generate globalize [tiny|internal] [lang|lang1,lang2...]
+
Description:
- Generates a new migration that creates and populates the "globalize" db tables.
- All tables are prefixed with "globalize_", so they won't conflict with
- other application tables. Run 'rake migrate' afterwards to synch the db.
+ Generates globalize specific migrations. The default migration creates and
+ populates the "globalize" db tables.
+ All tables are prefixed with "globalize_", so they won't conflict with
+ other application tables. Run 'rake db:migrate' afterwards to synch the db.
+
+ You can also specify the extra argument "tiny" to generate a compact version
+ of the data files (major languages only).
+
+ Specify "internal" to generate a migration which adds the required
+ language-suffixed columns for those model attributes marked as translatable
+ when using the internal storage mechanism
+ (i.e. keep_translations_in_model is true.)
+
+
Example:
+
+ Firstly, the command:
+
./script/generate globalize
- This will create:
- db/migrate/xxx_globalize.rb
+ will create:
+ db/migrate/xxx_globalize_migration.rb
+
+ which contains the db schema commands necessary to create the globalize tables
+ preloaded with a large amount of language/country data and certain translations
+ used for the ActionView date helper.
+
+ Secondly, the command:
+
+ ./script/generate globalize tiny
+
+ will also create:
+ db/migrate/xxx_globalize_migration.rb
+
+ but with a reduced amount of language and country data.
+
+
+ Assuming you have these two models:
+
+ class Product
+ self.keep_translations_in_model = true
+ translates :title
+ end
+
+ class Client
+ set_table_name :customers
+ self.keep_translations_in_model = true
+ translates :name
+ end
+
+ The following command:
+
+ ./script/generate globalize internal es,ca,fr
+
+ will create:
+ db/migrate/xxx_globalize_add_translated_fields_for_products_clients.rb
+
+ class GlobalizeAddTranslatedFieldsForProductsClients < ActiveRecord::Migration
+ def self.up
+ add_column :products, :title_es, :string
+ add_column :products, :title_ca, :string
+ add_column :products, :title_fr, :string
+
+ add_column :customers, :name_es, :string
+ add_column :customers, :name_ca, :string
+ add_column :customers, :name_fr, :string
+ end
+
+ def self.down
+ remove_column :products, :title_es
+ remove_column :products, :title_ca
+ remove_column :products, :title_fr
+ remove_column :customers, :name_es
+ remove_column :customers, :name_ca
+ remove_column :customers, :name_fr
+ end
+ end
+
+ Note: It will only generate the language-suffixed columns if they currently
+ do not exist in the table (for the current environment).
+
+ Remember to run rake db:migrate to add these migrations to your database.
@@ -2,15 +2,35 @@
require 'pathname'
class GlobalizeGenerator < MigrationGenerator
+
+ attr_accessor :attributes_for_migrations
+
def initialize(runtime_args, runtime_options = {})
- arg = runtime_args.first
- @tiny = arg && arg.downcase == 'tiny'
+ arg = runtime_args.shift
+ @internal,@tiny = false,false
+ case arg.downcase
+ when 'tiny'
+ @tiny = true
+ when 'internal'
+ @attributes_for_migrations = generate_translated_model_migrations(runtime_args.pop)
+ raise %q(No models found using internal storage mechanism or all required columns exist in db.) and return if @attributes_for_migrations.empty?
+ @internal = true
+ @migration_file_name = "globalize_add_translated_fields_for_#{@attributes_for_migrations.keys.collect {|key| key.split('/').first.underscore}.join('_')}"
+ @migration_class_name = "GlobalizeAddTranslatedFieldsFor#{@attributes_for_migrations.keys.collect {|key| key.split('/').first}.join}"
+ else
+ @tiny = false
+ end if arg
+
super([ "globalize_migration" ] + runtime_args, runtime_options)
end
def banner
- "Usage: script/generate globalize [tiny]\n" +
- ' Specify "tiny" to generate a compact version of the data files (major languages only).'
+ %q(
+ Usage: script/generate globalize [tiny|internal] [lang|lang1,lang2...]
+ No arguments generates a migration for the globalize tables with all the data files (major languages only).
+ Specify "tiny" to generate a compact version of the data files (major languages only).
+ Specify "internal" to generate a migration of all model attributes marked as translatable (when keep_translations_in_model is true.)
+ )
end
def inflate_schema
@@ -31,12 +51,62 @@ def inflate_schema
end
end
+=begin
+ For each supplied langugage finds all attributes (in all models) marked as
+ translatable and creates a hash like:
+
+ {'ModelClassName' => [['attribute_es','string', nil], ['attribute_fr','string', nil]]}
+
+ where the value is an array whose entries are (in order):
+
+ * {attribute_name}_{lang_suffix},
+ * {attribute_column_type}
+ * {attribute_default_value}
+=end
+ def generate_translated_model_migrations(langs)
+ require "#{RAILS_ROOT}/config/environment"
+ raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
+
+ langs = langs ? langs.split(',') : []
+
+ raise %q(You must specify at least one non-base language as an extra argument.
+ You may also specify a comma-separated list of as many non-base languages as you need.
+
+ e.g. script/generate globalize model es,en,fr) if langs.empty?
+
+ attributes_for_migrations = {}
+
+ Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each do |f|
+ model = File.basename(f).gsub(File.extname(f),'').camelize.constantize rescue nil
+ if model && model.base_class.superclass == ActiveRecord::Base
+ if model.keep_translations_in_model || Globalize::DbTranslate.keep_translations_in_model
+ key = "#{model.name}/#{model.table_name}"
+ attributes_for_migrations[key] = []
+ langs.each do |lang|
+ model.globalize_facets.each do |facet|
+ localized_facet_name = "#{facet}_#{lang}"
+ unless (column = model.columns.find {|c| c.name == facet.to_s}) && model.column_names.include?(localized_facet_name)
+ attributes_for_migrations[key] << [localized_facet_name, column.type, column.default]
+ end
+ end
+ end
+ end
+ end
+ end
+
+ attributes_for_migrations
+ end
+
def manifest
record do |m|
m.directory 'db/migrate'
- m.inflate_schema
- m.migration_template 'migration.rb', 'db/migrate'
+ m.inflate_schema unless @internal
+ m.migration_template 'migration.rb', 'db/migrate' unless @internal
+ m.migration_template 'model_migration.rb', 'db/migrate', :migration_file_name => @migration_file_name,
+ :assigns => {
+ :attributes_for_migrations => @attributes_for_migrations,
+ :migration_class_name => @migration_class_name
+ } if @internal
end
end
-end
-
+end
Binary file not shown.
Binary file not shown.
View
@@ -1,6 +1,3 @@
-require 'jcode'
-$KCODE = 'u' # Always use UTF-8 internally!
-
require 'pathname'
require 'singleton'
View
@@ -0,0 +1,10 @@
+APP_ROOT = File.join(File.dirname(__FILE__), '../../../')
+begin
+ output = `rake -f #{APP_ROOT}Rakefile globalize:upgrade_schema_to_1_dot_2 && exit 1`
+ puts output
+ while $?.exitstatus == 1
+ exit
+ end
+rescue => e
+ puts e.message
+end
@@ -1,17 +1,17 @@
-# This module supplies a bunch of localization-related core extensions to ruby
+# This module supplies a bunch of localization-related core extensions to ruby
# built-in and standard classes.
module Globalize # :nodoc:
module CoreExtensions # :nodoc:
module String
- # Indicates direction of text (usually +ltr+ [left-to-right] or
+ # Indicates direction of text (usually +ltr+ [left-to-right] or
# +rtl+ [right-to-left].
attr_accessor :direction
- # Translates the string into the active language. If there is a
- # quantity involved, it can be set with the +arg+ parameter. In this case
+ # Translates the string into the active language. If there is a
+ # quantity involved, it can be set with the +arg+ parameter. In this case
# string should contain the code <tt>%d</tt>, which will be substituted with
# the supplied number.
#
@@ -20,11 +20,33 @@ module String
#
# If there is no translation available, +default+ will be returned, or
# if it's not supplied, the original string will be returned.
- def translate(default = nil, arg = nil)
- Locale.translate(self, default, arg)
+ def translate(default = nil, arg = nil, namespace = nil)
+ Locale.translate(self, default, arg, namespace)
end
alias :t :translate
+
+ # Translates the string into the active language using the supplied namespace.
+ #
+ # Example:
+ # <tt>"draw".t -> "dibujar"</tt>
+ # <tt>"draw".tn(:lottery) -> "seleccionar"</tt>
+ def translate_with_namespace(namespace, arg = nil, default = nil)
+ Locale.translate(self, default, arg, namespace.to_s)
+ end
+
+ alias :tn :translate_with_namespace
+
+ # Translates the string into the active language using the supplied namespace.
+ # This is equivalent to translate_with_namespace(arg).
+ #
+ # Example:
+ # <tt>"draw".t -> "dibujar"</tt>
+ # <tt>"draw" >> 'lottery' -> "seleccionar"</tt>
+ def >>(namespace)
+ translate_with_namespace(namespace, nil, nil)
+ end
+
# Translates the string into the active language. This is equivalent
# to translate(arg).
#
@@ -36,20 +58,20 @@ def /(arg)
end
module Symbol
- # Translates the symbol into the active language. Underscores are
+ # Translates the symbol into the active language. Underscores are
# converted to spaces.
#
# If there is no translation available, +default+ will be returned, or
# if it's not supplied, the original string will be returned.
- def translate(default = nil)
- Locale.translate(self, default)
+ def translate(default = nil, namespace = nil)
+ Locale.translate(self, default, namespace)
end
alias :t :translate
end
module Object
- # Translates the supplied string into the active language. If there is a
- # quantity involved, it can be set with the +arg+ parameter. In this case
+ # Translates the supplied string into the active language. If there is a
+ # quantity involved, it can be set with the +arg+ parameter. In this case
# string should contain the code <tt>%d</tt>, which will be substituted with
# the supplied number.
#
@@ -59,7 +81,7 @@ module Object
# <em>Note: This method is deprectated and is supplied for backward
# compatibility with other translation packages, notable gettext.</em>
def _(str, default = nil, arg = nil)
- Locale.translate(str, default, arg)
+ Locale.translate(str, default, arg)
end
end
@@ -77,7 +99,7 @@ def localize( base = 10 )
number_grouping_scheme ||= :western
number_grouping_scheme == :indian ?
str.gsub(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) { |match|
- match + delimiter } :
+ match + delimiter } :
str.gsub(/(\d)(?=(\d\d\d)+(?!\d))/) { |match| match + delimiter }
else
str
@@ -106,7 +128,7 @@ def localize
int, frac = str.split('.')
number_grouping_scheme == :indian ?
- int.gsub!(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) { |match|
+ int.gsub!(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) { |match|
match + delimiter} :
int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) { |match| match + delimiter }
int + decimal + frac
@@ -118,7 +140,7 @@ def localize
end
module Time
- # Acts the same as #strftime, but returns a localized version of the
+ # Acts the same as #strftime, but returns a localized version of the
# formatted date/time string.
def localize(format)
# unabashedly stole this snippet from Tadayoshi Funaba's Date class
@@ -130,19 +152,19 @@ def localize(format)
when '%a'; o << "#{::Date::ABBR_DAYNAMES[wday]} [abbreviated weekday]".t(::Date::ABBR_DAYNAMES[wday])
when '%B'; o << "#{::Date::MONTHNAMES[mon]} [month]".t(::Date::MONTHNAMES[mon])
when '%b'; o << "#{::Date::ABBR_MONTHNAMES[mon]} [abbreviated month]".t(::Date::ABBR_MONTHNAMES[mon])
- when '%c'; o << ((Locale.active? && !Locale.active.date_format.nil?) ?
+ when '%c'; o << ((Locale.active? && !Locale.active.date_format.nil?) ?
localize(Locale.active.date_format) : strftime('%Y-%m-%d'))
when '%p'; o << if hour < 12 then 'AM [Ante Meridiem]'.t("AM") else 'PM [Post Meridiem]'.t("PM") end
else; o << c
end
end
strftime(o)
- end
+ end
alias :loc :localize
end
-
+
module Date
- # Acts the same as #strftime, but returns a localized version of the
+ # Acts the same as #strftime, but returns a localized version of the
# formatted date/time string.
def localize(format)
# unabashedly stole this snippet from Tadayoshi Funaba's Date class
@@ -154,15 +176,15 @@ def localize(format)
when '%a'; o << "#{::Date::ABBR_DAYNAMES[wday]} [abbreviated weekday]".t(::Date::ABBR_DAYNAMES[wday])
when '%B'; o << "#{::Date::MONTHNAMES[mon]} [month]".t(::Date::MONTHNAMES[mon])
when '%b'; o << "#{::Date::ABBR_MONTHNAMES[mon]} [abbreviated month]".t(::Date::ABBR_MONTHNAMES[mon])
- when '%c'; o << ((Locale.active? && !Locale.active.date_format.nil?) ?
+ when '%c'; o << ((Locale.active? && !Locale.active.date_format.nil?) ?
localize(Locale.active.date_format) : strftime('%Y-%m-%d'))
when '%p'; o << if hour < 12 then 'AM [Ante Meridiem]'.t("am") else 'PM [Post Meridiem]'.t("am") end
when '%P'; o << if hour < 12 then 'AM [Ante Meridiem]'.t("AM") else 'PM [Post Meridiem]'.t("PM") end
else; o << c
end
end
strftime(o)
- end
+ end
alias :loc :localize
end
Oops, something went wrong.

0 comments on commit 4cc2493

Please sign in to comment.