Module: Middleman::Features::I18n::ClassMethods

Defined in:
lib/middleman-i18n/feature.rb

Instance Method Summary collapse

Instance Method Details

#localize(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/middleman-i18n/feature.rb', line 25

def localize(options={})
  langs = options[:langs] || begin
    Dir[File.join(settings.root, "locales", "*.yml")].map do |file|
      File.basename(file).gsub(".yml", "").to_sym
    end
  end
  
  lang_map      = options[:lang_map]      || {}
  path          = options[:path]          || "/:locale/"
  templates_dir = options[:templates_dir] || "localizable"
  mount_at_root = options.has_key?(:mount_at_root) ? options[:mount_at_root] : langs.first
  
  if !settings.views.include?(settings.root)
    settings.set :views, File.join(settings.root, settings.views)
  end
  
  files = Dir[File.join(settings.views, templates_dir, "**/*")]
  
  langs.each do |lang|
    # Set current locale
    ::I18n.locale = lang
    
    # Build lang path
    if mount_at_root == lang
      prefix = "/"
    else
      replacement = lang_map.has_key?(lang) ? lang_map[lang] : lang
      prefix = path.gsub(":locale", replacement.to_s)
    end

    files.each do |file|
      url = file.gsub(settings.views, "").split(".html").first + ".html"
      
      page_id = File.basename(url, File.extname(url))
      localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id)
      localized_url = File.join(prefix, url.gsub(templates_dir + "/", "")).gsub(page_id, localized_page_id)

      page localized_url, :proxy => url, :ignore => true do
        ::I18n.locale = lang
        @lang         = lang
        @page_id      = page_id
      end
    end
  end
  
  settings.after_configuration do
    if !settings.build?
      $stderr.puts "== Locales: #{langs.join(", ")}"
    end
  end
end