Permalink
Browse files

added new code to forked plugin, new_file_handler, create_filepath, d…

…ump_yuml, new main structure and a lot of little changes
  • Loading branch information...
1 parent 7de10a5 commit 4b5261178e96ffd305b6d0ea59bc517d95cf27b0 @mattherick committed Jan 3, 2011
Showing with 335 additions and 208 deletions.
  1. +3 −0 .gitignore
  2. +11 −11 README
  3. +1 −2 Rakefile
  4. +1 −4 init.rb
  5. +123 −56 lib/base.rb
  6. +33 −15 lib/files.rb
  7. +36 −20 lib/gettext_i18n_convertor.rb
  8. +14 −21 lib/namespace.rb
  9. +2 −3 tasks/gettext_to_i18n_tasks.rake
  10. +87 −50 test/gettext_i18n_convertor_test.rb
  11. +24 −26 test/gettext_to_i18n_test.rb
View
3 .gitignore
@@ -0,0 +1,3 @@
+.loadpath
+.project
+*~
View
22 README
@@ -6,34 +6,34 @@ This plugin will convert your complete Rails project from Gettext calls to the n
Example
=======
- <%=_("Some string that will be translated, into %{lan}" % {:lan => 'english'}) %>
+ <%=_("Some string that will be translated, into %{lan}" % {:lan => 'english'}) %>
will be converted into:
- <%=t(:message_0, :lan => 'english', :scope => [:controller, :controller_name]) %>
+ <%=t(:message_0, :lan => 'english', :scope => [:controller, :controller_name]) %>
it will then place a entry:
-
- "Some string that will be translated, into {{lan}}"
-
+
+ "Some string that will be translated, into {{lan}}"
+
in the standard.yml locale file it generates.
About namespaces
========
By default the plugin walks all files in the app directory. It places the message id's into namespaces. The namespace are divided like this:
- Controllers: translation[:txt][:controller][:controller_name]
- Views: translation[:txt][:view][:view_colleciton_name]
- -> etc.
-
-
+ Controllers: translation[:txt][:controller][:controller_name]
+ Views: translation[:txt][:view][:view_colleciton_name]
+ -> etc.
+
+
Usage
=======
To convert all your views, controllers and models to the new format.
- rake gettext_to_i18n:transform
+ rake gettext_to_i18n:transform
Installation
View
3 Rakefile
@@ -23,5 +23,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
+end
View
5 init.rb
@@ -8,7 +8,4 @@
require File.dirname(__FILE__) + '/lib/namespace'
require File.dirname(__FILE__) + '/lib/gettext_i18n_convertor'
-require File.dirname(__FILE__) + '/lib/base'
-
-
-
+require File.dirname(__FILE__) + '/lib/base'
View
179 lib/base.rb
@@ -1,54 +1,86 @@
module GettextToI18n
class Base
- attr_reader :translations
LOCALE_DIR = RAILS_ROOT + '/config/locales/'
- STANDARD_LOCALE_FILE = LOCALE_DIR + 'template.yml'
- DEFAULT_LANGUAGE = 'some-LAN'
+
+ LANGUAGES = ['en','de']
+
+ TYPES = ['controllers','models','helpers','views','mailers','lib']
def initialize
- @translations = {}
- transform_files!(Files.controller_files, :controller)
- transform_files!(Files.model_files, :model)
- transform_files!(Files.view_files, :view)
- transform_files!(Files.helper_files, :helper)
- transform_files!(Files.lib_files, :lib)
+ TYPES.each do |type|
+ transform_files!(Files.type_files(type), type)
+ end
end
# Walks all files and converts them all to the new format
def transform_files!(files, type)
files.each do |file|
+ @file = file
+ @type = type
parsed = ""
- namespace = [DEFAULT_LANGUAGE, 'txt', type] + Base.get_namespace(file, type)
- puts "Converting: " + file + " into namespace: "
- puts namespace.map {|x| "[\"#{x}\"]"}.join("")
+ @dirnames = Base.get_namespace(file,type) # directories after the app/type/ directory
- n = Namespace.new(namespace)
+ namespaces = LANGUAGES.collect do |lang|
+ if type == 'views'
+ namespace = [lang] + @dirnames
+ else
+ namespace = [lang, type] + @dirnames
+ end
+ puts "Converting: " + file + " into namespace: "
+ puts namespace.map {|x| "[\"#{x}\"]"}.join("")
+
+ Namespace.new(namespace,lang)
+ end
+
contents = Base.get_file_as_string(file)
- parsed << GettextI18nConvertor.string_to_i18n(contents, n)
-
- #puts parsed
- # write the file
+ parsed << GettextI18nConvertor.string_to_i18n(contents, namespaces, type)
+ # write the app/type/file with new i18n format instead of gettext
File.open(file, 'w') { |file| file.write(parsed)}
-
-
- n.merge(@translations)
+ namespaces.each do |ns|
+ new_file_handler(ns)
+ end
end
end
- # Dumps the translation strings into config/locales/template.yml
- def dump_yaml!
- FileUtils.mkdir_p LOCALE_DIR
- File.open(STANDARD_LOCALE_FILE,'w+') { |f| YAML::dump(@translations, f) }
+ def new_file_handler(ns)
+ @yaml, @yaml2 = {}
+
+ # load old files if they exist
+ filepath = create_filepath(ns.locale)
+ @yaml = YAML.load_file(filepath) if File.exists?(filepath)
+
+ # new translations (ns) merges with existing file or with an empty hash
+ ns.merge(@yaml)
+ cleanup!(@yaml)
+
+ dump_yaml!(filepath,@yaml)
end
+ def create_filepath(language)
+ # directory will be created in config/locales/type/ in dump_yaml!
+ @dirpath = File.join(LOCALE_DIR + @type + '/' + @dirnames.first)
+ File.join(@dirpath + '/' + language + '.yml')
+ end
+
+ # Dumps the translation strings into yml files
+ def dump_yaml!(filepath,translations)
+ return if translations.blank?
+ FileUtils.mkdir_p(@dirpath) unless File.exists?(@dirpath)
+ File.open(filepath, 'w+') { |f| YAML::dump(translations, f) }
+ end
- private
-
+ def cleanup!(content)
+ content.each do |key, value|
+ cleanup!(value) if value.is_a? Hash
+ content.delete(key) if value.blank?
+ end
+ end
+ private
def self.get_file_as_string(filename)
data = ''
f = File.open(filename, "r")
@@ -57,38 +89,73 @@ def self.get_file_as_string(filename)
end
return data
end
+
# returns a name for a file
- # example:
- # Base.get_name('/controllers/apidoc_controller.rb', 'controller') => 'apidoc'
- def self.get_namespace(file, type)
- case type
+ # example: Base.get_name('/controllers/apidoc_controller.rb', 'controller') => 'apidoc'
+ def self.get_namespace(file,type)
+ case type
+ when 'controllers'
+ result = /controllers\/?([\_a-zA-Z0-9]+)?\/([\_a-zA-Z0-9]+).rb/.match(file)
+ if result[1].nil?
+ return [result[2]]
+ else
+ return [result[1],result[2]]
+ end
+ when 'helpers'
+ result = /helpers\/?([\_a-zA-Z0-9]+)?\/([\_a-zA-Z0-9]+)_helper.rb/.match(file)
+ if result[1].nil?
+ return [result[2]]
+ else
+ return [result[1],result[2]]
+ end
+ when 'models'
+ result = /models\/?([\_a-zA-Z0-9]+)?\/([\_a-zA-Z0-9]+).rb/.match(file)
+ if result[1].nil?
+ return [result[2]]
+ else
+ return [result[1],result[2]]
+ end
+ when 'views'
+ result = /views\/([\_a-zA-Z0-9]+)\/?([\_a-zA-Z0-9]+)?\/([\_a-zA-Z0-9]+).*\.([a-zA-Z0-9]+)/.match(file)
+
+ temp = result[3]
+ # if partial remove the first underscore
+ temp.slice!(0) if temp.first == '_'
- when :controller
- if result = /application\.rb/.match(file)
- return ['application']
- else
- result = /([a-zA-Z]+)_controller.rb/.match(file)
- return [result[1]]
- end
- return ""
- when :helper
- result = /([a-zA-Z]+)_helper.rb/.match(file)
- return [result[1]]
- when :model
- result = /([a-zA-Z]+).rb/.match(file)
- return [result[1]]
- when :view
- result = /views\/([\_a-zA-Z]+)\/([\_a-zA-Z]+).*\.([a-zA-Z]+)/.match(file)
- if result[3] != "erb"
- return [result[1], result[2], result[3]]
- else
- return [result[1], result[2]]
- end
- when :lib
- result = /([a-zA-Z]+).rb/.match(file)
+ if result[2].nil?
+ if result[4] != "erb" && result[4] != "haml"
+ return [result[1], temp, result[4]]
+ else
+ return [result[1], temp]
+ end
+ else
+ if result[4] != "erb" && result[4] != "haml"
+ return [result[1], result[2], temp, result[4]]
+ else
+ return [result[1], result[2], temp]
+ end
+ end
+ when 'mailers'
+ result = /mailers\/?([\_a-zA-Z0-9]+)?\/([\_a-zA-Z0-9]+)\.([a-zA-Z0-9]+)/.match(file)
+
+ if result[1].nil?
+ if result[3] != "erb" && result[3] != "haml" && result[3] != "rb"
+ return [result[2], result[3]]
+ else
+ return [result[2]]
+ end
+ else
+ if result[3] != "erb" && result[3] != "haml" && result[3] != "rb"
+ return [result[1], result[2], result[3]]
+ else
+ return [result[1], result[2]]
+ end
+ end
+ when 'lib'
+ result = /([\_a-zA-Z0-9]+).rb/.match(file)
return [result[1]]
- end
- end
-
+
+ end
+ end
end
end
View
48 lib/files.rb
@@ -4,46 +4,64 @@ class Files
# all files that contain some gettext methods
def self.all_files
- self.controller_files + self.view_files + self.helper_files + self.lib_files + self.model_files
+ self.controller_files + self.view_files + self.helper_files + self.model_files + self.mailer_files + self.lib_files
+ end
+
+ # All files with specific type
+ def self.type_files(type)
+ case type
+ when 'controllers'
+ return self.controller_files
+ when 'helpers'
+ return self.helper_files
+ when 'models'
+ return self.model_files
+ when 'views'
+ return self.view_files
+ when 'mailers'
+ return self.mailer_files
+ when 'lib'
+ return self.lib_files
+ end
end
# All controller files
def self.controller_files
- self.get_files('app/controllers', '*.rb')
+ self.get_files('app/controllers', '*.rb') + self.get_files('app/controllers', '**/*.rb')
end
# All view files
def self.view_files
- self.get_files('app/views', '**/*.{erb,builder}')
+ self.get_files('app/views', '**/*.{erb,builder,haml}')
end
- # All view files
+ # All lib files
def self.lib_files
- self.get_files('lib', '**/*.rb')
+ self.get_files('lib', '*.rb')
end
-
- # All view files
+ # All helper files
def self.helper_files
- self.get_files('app/helpers', '*.rb')
+ self.get_files('app/helpers', '*.rb') + self.get_files('app/helpers', '**/*.rb')
end
- # All view files
+ # All model files
def self.model_files
- self.get_files('app/models', '*.rb')
+ self.get_files('app/models', '*.rb') + self.get_files('app/models', '**/*.rb')
end
+ # All mailer files
+ def self.mailer_files
+ self.get_files('app/mailers', '*.rb') + self.get_files('app/mailers', '**/*.{erb,haml}')
+ end
- private
-
+ private
def self.chdir
-
-
Dir.chdir(RAILS_ROOT)
end
# All files we need to walk
- def self.get_files(filter = '**', types='*.{erb,rb}')
+ def self.get_files(filter = '**', types='*.{erb,rb,haml}')
self.chdir
Dir.glob("#{filter}/#{types}")
end
View
56 lib/gettext_i18n_convertor.rb
@@ -4,9 +4,10 @@ class GettextI18nConvertor
GETTEXT_VARIABLES = /\%\{(\w+)\}*/
- def initialize(text, namespace = nil)
+ def initialize(text, namespaces, type = 'views')
@text = text
- @namespace = namespace
+ @namespaces = namespaces
+ @type = type
end
# The contents of the method call
@@ -30,17 +31,15 @@ def contents_i18n
c = contents
unless c.nil?
c.gsub!(GETTEXT_VARIABLES, '{{\1}}')
+ c.gsub!(GETTEXT_VARIABLES, '%{\1}') # new
c.gsub!(/^(\"|\')/, '')
c.gsub!(/(\"|\')$/, '')
else
puts "No content: " + @text
-
end
c
end
-
-
# Returns the part after the method call,
# _('aaa' % :a => 'sdf', :b => 'agh')
# return :a => 'sdf', :b => 'agh'
@@ -86,39 +85,45 @@ def variables
return nil if vsplitted.nil?
vsplitted.map! { |v|
r = v.match(/\s*:(\w+)\s*=>\s*(.*)/)
- {:name => r[1], :value => GettextI18nConvertor.string_to_i18n(r[2], @namespace)}
+ {:name => r[1], :value => GettextI18nConvertor.string_to_i18n(r[2], @namespaces, @type)}
}
end
end
# After analyzing the variable part, the variables
# it is now time to construct the actual i18n call
def to_i18n
- id = @namespace.consume_id!
- @namespace.set_id(id, contents_i18n)
- output = "t(:#{id}"
+ id = ''
+ @namespaces.each do |ns|
+ id = ns.consume_id!(GettextI18nConvertor.create_key(contents_i18n))
+ ns.set_id(id, contents_i18n)
+ end
+ if @type == 'views'
+ output = "t('.#{id}'"
+ else
+ output = "I18n.t('#{@namespaces.first.to_i18n_scope + id}'"
+ end
if !self.variables.nil?
vars = self.variables.collect { |h| {:name => h[:name], :value => h[:value] }}
output += ", " + vars.collect {|h| ":#{h[:name]} => #{h[:value]}"}.join(", ")
end
- output += ", " + @namespace.to_i18n_scope
output += ")"
return output
end
# Takes the gettext calls out of a string and converts
# them to i18n calls
- def self.string_to_i18n(text, namespace)
+ def self.string_to_i18n(text, namespaces, type)
s = self.indexes_of(text, /_\(/)
e = self.indexes_of(text, /\)/)
- r = self.indexes_of(text, /\(/)
indent, indent_all,startindex, endinde, methods = 0, 0, -1, -1, []
output = ""
level = 0
gettext_blocks = []
text.length.times do |i|
+
token = text[i..i]
in_gettext_block = gettext_blocks.size % 2 == 1
@@ -144,22 +149,33 @@ def self.string_to_i18n(text, namespace)
e = gettext_blocks[i * 2 + 1]
to_convert = text[s..e]
- converted_block = GettextI18nConvertor.new(to_convert, namespace).to_i18n
+ converted_block = GettextI18nConvertor.new(to_convert, namespaces, type).to_i18n
g = output.index(to_convert) - 1
h = g + (e-s) + 2
output = output[0..g] + converted_block + output[h..output.length]
end
output
end
-
-
-
-
-
+
+ def self.create_key(s, max_words = 3)
+ s = 'message' if s.nil?
+ # all down
+ s.downcase!
+
+ # max words
+ words = s.split(/\s/)
+ if words.size >= max_words
+ s = words[0..(max_words-1)].join("_")
+ else
+ s = words.join("_")
+ end
+
+ # preserve alphanumerics, everything else becomes a separator
+ s.gsub(/[^a-z0-9_]/, '')
+ end
private
-
# Finds indexes of some pattern(regexp) in a string
def self.indexes_of(str, pattern)
indexes = []
@@ -169,6 +185,6 @@ def self.indexes_of(str, pattern)
end
indexes
end
-
+
end
end
View
35 lib/namespace.rb
@@ -1,27 +1,26 @@
module GettextToI18n
class Namespace
attr_reader :ids
+ attr_reader :locale
- def initialize(name)
+ def initialize(name,locale)
@ids = {}
@namespace = name
+ @locale = locale
end
# reserves a id
- def consume_id!(id = nil)
- id = "message_" + @ids.length.to_s if id.nil?
+ def consume_id!(key,id = nil)
+ id = "msg#{@ids.length.to_s}_#{key}" if id.nil?
raise "ID already in use" if @ids.keys.include?(id)
@ids[id] = ""
return id
end
-
-
def set_id(id, value)
- @ids[id] = value
+ @ids[id] = I18n.t(value, :default => value.to_s, :locale => @locale)
end
-
def to_s
o = "Namespace: {" + @namespace
o << @ids.to_yaml
@@ -31,29 +30,23 @@ def to_s
def i18n_namespace
@cached_i18n_namespace ||= begin
a = @namespace.dup
- #a.delete("txt")
- a.delete(Base::DEFAULT_LANGUAGE)
+ a.delete(@locale)
a
end
end
-
def to_i18n_scope
- @cached_i18n_scope ||= ":scope => [%s]" % i18n_namespace.collect {|x| ":#{x}"}.join(", ")
+ @cached_i18n_scope ||= i18n_namespace.collect {|x| "#{x}."}.join("")
end
-
+ # merges new translations with old yaml file
def merge(base)
- loc = ""
- @namespace.each do |v|
- loc << "[\"#{v}\"]"
- arr = 'base' + loc
- eval arr + ' = {} if ' + arr + '.nil?'
+ @namespace.each do |key|
+ base[key] ||= {}
+ base = base[key]
end
-
- eval 'base' + loc + ' = @ids'
-
+ base.merge!(@ids)
end
-
+
end
end
View
5 tasks/gettext_to_i18n_tasks.rake
@@ -8,9 +8,8 @@ require File.dirname(__FILE__) + '/../init'
namespace :gettext_to_i18n do
desc 'Transforms all of your files into the new I18n api format'
- task :transform do
- a = GettextToI18n::Base.new
- a.dump_yaml!
+ task :transform => %w(environment) do # w(environment) for multiple languages because i18n gem needed
+ GettextToI18n::Base.new
end
View
137 test/gettext_i18n_convertor_test.rb
@@ -1,91 +1,128 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../init.rb'
require 'YAML'
+require 'i18n'
module GettextToI18n
class GettextI18nTest < Test::Unit::TestCase
-
-
def test_other_string
- assert_equal 'a ', GettextI18nConvertor.new("_('a ')").contents
- #assert_equal 'a ', GettextI18nConvertor.new("_('a ' % {:a => 'sdasd'})").contents
- assert_equal ":a => 'sdasd'", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd'})").variable_part
+ assert_equal 'a ', GettextI18nConvertor.new("_('a ')", []).contents
+ assert_equal 'a ', GettextI18nConvertor.new("_('a ' % {:a => 'sdasd'})", []).contents
+ assert_equal 'a ', GettextI18nConvertor.new("_('a ') % {:a => 'sdasd'}", []).contents
+ assert_equal ":a => 'sdasd'", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd'})", []).variable_part
end
-
- def test_variables
- assert_equal ":a => 'sdasd', :b => 'sdasd'", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd', :b => 'sdasd'})").variable_part
- assert_equal [{:name => "a", :value => "'sdasd'"}, {:name => "b", :value => "'sdasd'"}], GettextI18nConvertor.new("_('a ' % {:a => 'sdasd', :b => 'sdasd'})").variables
- assert_equal "t(:message_0, :a => 'sdasd', :scope => [:somenamespace])", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd'})", Namespace.new("somenamespace")).to_i18n
- assert_equal "t(:message_0, :a => 'sdasd', :b => 'sd', :scope => [:somenamespace])", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd', :b => 'sd'})", Namespace.new("somenamespace")).to_i18n
- assert_equal ":a => 'sdf' + _(sdf)", GettextI18nConvertor.new("_('aaa' % {:a => 'sdf' + _(sdf)}) %>", Namespace.new("somenamespace")).variable_part
+ def test_views_variables
+ assert_equal ":a => 'sdasd', :b => 'sdasd'", GettextI18nConvertor.new("_('a ' % {:a => 'sdasd', :b => 'sdasd'})", []).variable_part
+ assert_equal [{:name => "a", :value => "'sdasd'"}, {:name => "b", :value => "'sdasd'"}], GettextI18nConvertor.new("_('a ' % {:a => 'sdasd', :b => 'sdasd'})", []).variables
+ assert_equal "t('.msg0_new_translation', :a => 'sdasd')", GettextI18nConvertor.new("_('new translation' % {:a => 'sdasd'})", [ Namespace.new("somenamespace", 'en') ]).to_i18n
+ assert_equal "t('.msg0_new_translation', :a => 'sdasd', :b => 'sd')", GettextI18nConvertor.new("_('new translation' % {:a => 'sdasd', :b => 'sd'})", [ Namespace.new("somenamespace", "en") ]).to_i18n
+ assert_equal ":a => 'sdf' + _(sdf)", GettextI18nConvertor.new("_('aaa' % {:a => 'sdf' + _(sdf)}) %>", [ Namespace.new("somenamespace", "en") ]).variable_part
end
- def test_multiple_variables
- assert_equal "<%=t(:message_0, :a => 'sdf', :b => 'agh', :scope => [:somenamespace]) %>", GettextI18nConvertor.string_to_i18n("<%=_('aaa' % {:a => 'sdf', :b => 'agh'}) %>", Namespace.new("somenamespace"))
+ def test_views_multiple_variables
+ assert_equal "<%=t('.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new("somenamespace", 'en') ], 'views')
end
+ def test_multiple_variables
+ assert_equal "<%=I18n.t('controllers.somenamespace.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new(["en","controllers","somenamespace"], 'en') ], 'controllers')
+ assert_equal "<%=I18n.t('models.somenamespace.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new(["en","models","somenamespace"], 'en') ], 'models')
+ assert_equal "<%=I18n.t('helpers.somenamespace.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new(["en","helpers","somenamespace"], 'en') ], 'helpers')
+ assert_equal "<%=I18n.t('mailers.somenamespace.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new(["en","mailers","somenamespace"], 'en') ], 'mailers')
+ assert_equal "<%=I18n.t('lib.somenamespace.msg0_new_trans', :a => 'sdf', :b => 'agh') %>", GettextI18nConvertor.string_to_i18n("<%=_('new trans' % {:a => 'sdf', :b => 'agh'}) %>", [ Namespace.new(["en","lib","somenamespace"], 'en') ], 'lib')
+ end
+
+ def test_create_key
+ assert_equal 'onewrd', GettextI18nConvertor.create_key('ONEwörd')
+ assert_equal 'first_level', GettextI18nConvertor.create_key('first level')
+ assert_equal 'one_two_three', GettextI18nConvertor.create_key('one two three')
+ assert_equal 'message', GettextI18nConvertor.create_key(blubb = nil)
+ assert_equal 'a', GettextI18nConvertor.create_key('"A"')
+ assert_equal 'eins_fnf_drei_vier', GettextI18nConvertor.create_key('eins fünf drei vier zwei sechs',4)
+ end
- def test_recursive_gettext
- t = GettextI18nConvertor.new("<%=_('aaa' % {:a => 'sdf' + _(sdfg) + _(sdfg), :b => '21'}) %>", Namespace.new("somenamespace"))
- assert_equal ":a => 'sdf' + _(sdfg) + _(sdfg), :b => '21'", t.variable_part
- assert_equal [{:value=>"'sdf' + t(:message_0, :scope => [:somenamespace]) + t(:message_1, :scope => [:somenamespace])", :name=>"a"}, {:value=>"'21'", :name=>"b"}], t.variables
+ def test_views_recursive_gettext
+ t = GettextI18nConvertor.new("<%=_('first level' % {:a => 'sdf' + _('second level') + _('second second level'), :b => '21'}) %>", [ Namespace.new("somenamespace", "en") ], 'views')
+ assert_equal ":a => 'sdf' + _('second level') + _('second second level'), :b => '21'", t.variable_part
+ assert_equal [{:value=>"'sdf' + t('.msg0_second_level') + t('.msg1_second_second_level')", :name=>"a"}, {:value=>"'21'", :name=>"b"}], t.variables
- assert_equal "<%=t(:message_0, :a => 'sdf' + t(:message_1, :scope => [:somenamespace]) + t(:message_2, :scope => [:somenamespace]), :scope => [:somenamespace]) %>", GettextI18nConvertor.string_to_i18n("<%=_('aaa' % {:a => 'sdf' + _(sdfg) + _(sdfg)}) %>", Namespace.new("somenamespace"))
+ expected = "<%=t('.msg0_first_level', :a => 'sdf' + t('.msg1_second_level') + t('.msg2_second_second_level')) %>"
+ actual = GettextI18nConvertor.string_to_i18n("<%=_('first level' % {:a => 'sdf' + _('second level') + _('second second level')}) %>", [ Namespace.new("somenamespace", "en") ], 'views')
+ assert_equal expected, actual
+ end
+ def test_conroller_recursive_gettext
+ expected = "<%=I18n.t('controllers.somenamespace.msg0_first_level', :a => 'sdf' + I18n.t('controllers.somenamespace.msg1_second_level') + I18n.t('controllers.somenamespace.msg2_second_second_level')) %>"
+ actual = GettextI18nConvertor.string_to_i18n("<%=_('first level' % {:a => 'sdf' + _('second level') + _('second second level')}) %>", [ Namespace.new(["de","controllers","somenamespace"], "de") ], 'controllers')
+ assert_equal expected, actual
end
- def test_variable_parts
- assert_equal "t(:message_0, :a => 'sdasddd', :b => 'sdasd' + t(:message_1, :scope => [:somenamespace]), :scope => [:somenamespace])" , GettextI18nConvertor.new("_('a ' % {:a => 'sdasddd', :b => 'sdasd' + _('sfd')})", Namespace.new("somenamespace")).to_i18n
+ def test_views_variable_parts
+ assert_equal "t('.msg0_a', :a => 'sdasddd', :b => 'sdasd' + t('.msg1_sfd'))" , GettextI18nConvertor.new("_('a ' % {:a => 'sdasddd', :b => 'sdasd' + _('sfd')})", [ Namespace.new("somenamespace", "en") ]).to_i18n
end
+ def test_controller_variable_parts
+ assert_equal "I18n.t('controllers.somenamespace.msg0_a', :a => 'sdasddd', :b => 'sdasd' + I18n.t('controllers.somenamespace.msg1_sfd'))" , GettextI18nConvertor.new("_('a ' % {:a => 'sdasddd', :b => 'sdasd' + _('sfd')})", [ Namespace.new(["en","controllers","somenamespace"], "en") ], 'controllers').to_i18n
+ end
- def test_exceptions
- str = "<%=_(\"Invoice: %{desc}\" % {:desc => @invoice.description}) %>"
- t = GettextI18nConvertor.new(str, Namespace.new("some"))
-
-
- str = "_(\"Information about advertising will soon follow, in the mean time, %{contact}.\" % {:contact => link_to(_(\"please contact us\"), contact_path)})"
- t = GettextI18nConvertor.new(str, Namespace.new("some"))
-
- assert_equal ":contact => link_to(_(\"please contact us\"), contact_path)", t.variable_part
-# assert_equal [{:value=>"link_to(t(:message_0, :scope => [:some]), contact_path)", :name=>"contact"}], t.variables
-
+ def test_controller_nested_scope
+ expected = "I18n.t('controllers.somenamespace.subdir.msg0_trans_will_be')"
+ actual = GettextI18nConvertor.new("_('trans will be translated')", [ Namespace.new(["en","controllers","somenamespace","subdir"], "en") ], 'controllers').to_i18n
+ assert_equal expected, actual
+ end
+
+ def test_model_nested_scope
+ expected = "I18n.t('models.somenamespace.subdir.msg0_trans_will_be')"
+ actual = GettextI18nConvertor.new("_('trans will be translated')", [ Namespace.new(["models","somenamespace","subdir"], "en") ], 'models').to_i18n
+ assert_equal expected, actual
end
+ def test_helper_nested_scope
+ expected = "I18n.t('helpers.somenamespace.subdir.msg0_trans_will_be')"
+ actual = GettextI18nConvertor.new("_('trans will be translated')", [ Namespace.new(["en","helpers","somenamespace","subdir"], "en") ], 'helpers').to_i18n
+ assert_equal expected, actual
+ end
- def test_greedyness
- str = "_(\"%{project_name} (copy)\") % {:project_name => @project.name}"
- t= GettextI18nConvertor.string_to_i18n(str, Namespace.new('d'))
- r = GettextI18nConvertor.new(str, Namespace.new('sdf'))
+ def test_mailer_nested_scope
+ expected = "I18n.t('mailers.somenamespace.subdir.msg0_trans_will_be')"
+ actual = GettextI18nConvertor.new("_('trans will be translated')", [ Namespace.new(["en","mailers","somenamespace","subdir"], "en") ], 'mailers').to_i18n
+ assert_equal expected, actual
end
+ def test_lib_nested_scope
+ expected = "I18n.t('lib.somenamespace.subdir.msg0_trans_will_be')"
+ actual = GettextI18nConvertor.new("_('trans will be translated')", [ Namespace.new(["en","lib","somenamespace","subdir"], "en") ], 'lib').to_i18n
+ assert_equal expected, actual
+ end
+
+ def test_views_exceptions
+ str = "<%=_(\"Invoice: %{desc}\" % {:desc => @invoice.description}) %>"
+ t = GettextI18nConvertor.new(str, [ Namespace.new("somenamespace", "en") ])
+ assert_equal ":desc => @invoice.description", t.variable_part
+
+ str = '_("Information about advertising will soon follow, in the mean time, %{contact}." % {:contact => link_to(_("please contact us"), contact_path)})'
+ t = GettextI18nConvertor.new(str, [ Namespace.new("somenamespace", "en") ])
+ assert_equal ":contact => link_to(_(\"please contact us\"), contact_path)", t.variable_part
+ assert_equal [{:value=>"link_to(t('.msg0_please_contact_us'), contact_path)", :name=>"contact"}], t.variables
+ end
- def test_some_other_string
+ def test_views_some_other_string
str = "_('For more information on large volume plans, customized solutions or (media) partnerships.')"
- #str = "_(\"%{project_name} (copy)\" % {:project_name => @project.name})"
- t = GettextI18nConvertor.string_to_i18n(str, Namespace.new('d'))
+ assert_equal "t('.msg0_for_more_information')", GettextI18nConvertor.new(str, [ Namespace.new("somenamespace", "en") ],'views').to_i18n
str = "o << content_tag(:div, _(\"Plan your own home with the free floorplanner\"), :class => \"content\")"
- t = GettextI18nConvertor.string_to_i18n(str, Namespace.new('d'))
- puts t
-
+ assert_equal "t('.msg0_plan_your_own')", GettextI18nConvertor.new(str, [ Namespace.new("somenamespace", "en") ],'views').to_i18n
end
def test_quotes
-
- n =GettextToI18n::Namespace.new("somenamespace")
+ n = GettextToI18n::Namespace.new("somenamespace", "en")
str = "_('The easiest way to <span class=\"highlight\">create</span> and<br /> <span class=\"highlight\">share</span> interactive floorplans')"
- t = GettextToI18n::GettextI18nConvertor.new(str, n)
+ t = GettextToI18n::GettextI18nConvertor.new(str, [n])
assert_equal 'The easiest way to <span class="highlight">create</span> and<br /> <span class="highlight">share</span> interactive floorplans', t.contents
- puts t.text
-
-
- t = GettextToI18n::GettextI18nConvertor.new("_(\"Save\")", n)
+ t = GettextToI18n::GettextI18nConvertor.new("_(\"Save\")", [n])
assert_equal "Save", t.contents
-
end
end
View
50 test/gettext_to_i18n_test.rb
@@ -3,30 +3,27 @@
require 'YAML'
class GettextToI18nTest < Test::Unit::TestCase
- def setup
- #@c = GettextToI18n::Convertor.new(nil,nil,nil,nil)
- end
def test_all_files
files = GettextToI18n::Files.all_files
assert_not_nil files, "no files available"
assert files.size > 0
end
-
-
+
def test_controller_files
files = GettextToI18n::Files.controller_files
assert_not_nil files, "no files available"
assert files.size > 0
+ files = GettextToI18n::Files.type_files('controllers')
+ assert_not_nil files, "no files available"
+ assert files.size > 0
end
-
-
+
def test_view_files
files = GettextToI18n::Files.view_files
assert_not_nil files, "no files available"
assert files.size > 0
end
-
def test_helper_files
files = GettextToI18n::Files.helper_files
assert_not_nil files, "no files available"
@@ -39,23 +36,24 @@ def test_lib_files
assert files.size > 0
end
-
- def test_get_filename
- assert_equal 'apidoc', GettextToI18n::Base.get_name('/controllers/apidoc_controller.rb', :controller)
- assert_equal 'application', GettextToI18n::Base.get_name('/controllers/application.rb', :controller)
- assert_equal 'page', GettextToI18n::Base.get_name('/views/page/index.html.erb', :view)
+ def test_mailer_files
+ files = GettextToI18n::Files.mailer_files
+ assert_not_nil files, "no files available"
+ assert files.size > 0
end
-
-
-
- def test_transform
- a = GettextToI18n::Base.new
- #puts a.dump_yaml
+
+ def test_get_filename
+ assert_equal ["admin", "admin"], GettextToI18n::Base.get_namespace('/controllers/admin/admin_controller.rb', 'controllers')
+ assert_equal ['application'], GettextToI18n::Base.get_namespace('/controllers/application_controller.rb', 'controllers')
+ assert_equal ["page", "index"], GettextToI18n::Base.get_namespace('/views/page/index.html.erb', 'views')
+ assert_equal ["page", "under", "index"], GettextToI18n::Base.get_namespace('/views/page/under/index.html.erb', 'views')
+ assert_equal ["comment"], GettextToI18n::Base.get_namespace('/models/comment.rb', 'models')
+ assert_equal ["admin","comment"], GettextToI18n::Base.get_namespace('/models/admin/comment.rb', 'models')
+ assert_equal ['feedback_mailer'], GettextToI18n::Base.get_namespace('/mailers/feedback_mailer.rb', 'mailers')
+ assert_equal ['notification_mailer','new_answer'], GettextToI18n::Base.get_namespace('/mailers/notification_mailer/new_answer.erb', 'mailers')
+ assert_equal ['notification_mailer','new_answer'], GettextToI18n::Base.get_namespace('/mailers/notification_mailer/new_answer.haml', 'mailers')
+ assert_equal ["blogs"], GettextToI18n::Base.get_namespace('/helpers/blogs_helper.rb', 'helpers')
+ assert_equal ["api","blogs"], GettextToI18n::Base.get_namespace('/helpers/api/blogs_helper.rb', 'helpers')
+ assert_equal ["cap_mailer"], GettextToI18n::Base.get_namespace('/lib/cap_mailer.rb', 'lib')
end
-
-
-
-
-
-
-end
+end

0 comments on commit 4b52611

Please sign in to comment.