File: tasks.rb

package info (click to toggle)
ruby-gettext-i18n-rails 1.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: ruby: 515; makefile: 6
file content (112 lines) | stat: -rw-r--r-- 3,881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require "gettext/tools/task"
gem "gettext", ">= 3.0.2"

namespace :gettext do
  def locale_path
    path = FastGettext.translation_repositories[text_domain].instance_variable_get(:@options)[:path] rescue nil
    path || File.join(Rails.root, "locale")
  end

  def text_domain
    # if your textdomain is not 'app': require the environment before calling e.g. gettext:find OR add TEXTDOMAIN=my_domain
    (FastGettext.text_domain rescue nil) || ENV['TEXTDOMAIN'] || "app"
  end

  # do not rename, gettext_i18n_rails_js overwrites this to inject coffee + js
  def files_to_translate
    Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim}")
  end

  def gettext_default_options
    config = (Rails.application.config.gettext_i18n_rails.default_options if defined?(Rails.application))
    config || %w[--sort-by-msgid --no-location --no-wrap]
  end

  def gettext_msgmerge_options
    config = (Rails.application.config.gettext_i18n_rails.msgmerge if defined?(Rails.application))
    config || gettext_default_options
  end

  def gettext_msgcat_options
    config = (Rails.application.config.gettext_i18n_rails.msgcat if defined?(Rails.application))
    config || gettext_default_options - %w[--location]
  end

  def gettext_xgettext_options
    config = (Rails.application.config.gettext_i18n_rails.xgettext if defined?(Rails.application))
    config || gettext_default_options
  end

  require "gettext_i18n_rails/haml_parser"
  require "gettext_i18n_rails/slim_parser"

  task :setup => [:environment] do
    GetText::Tools::Task.define do |task|
      task.package_name = text_domain
      task.package_version = "1.0.0"
      task.domain = text_domain
      task.po_base_directory = locale_path
      task.mo_base_directory = locale_path
      task.files = files_to_translate
      task.enable_description = false
      task.msgmerge_options = gettext_msgmerge_options
      task.msgcat_options = gettext_msgcat_options
      task.xgettext_options = gettext_xgettext_options
    end
  end

  desc "Create mo-files"
  task :pack => [:setup] do
    Rake::Task["gettext:mo:update"].invoke
  end

  desc "Update pot/po files"
  task :find => [:setup] do
    Rake::Task["gettext:po:update"].invoke
  end

  # This is more of an example, ignoring
  # the columns/tables that mostly do not need translation.
  # This can also be done with GetText::ActiveRecord
  # but this crashed too often for me, and
  # IMO which column should/should-not be translated does not
  # belong into the model
  #
  # You can get your translations from GetText::ActiveRecord
  # by adding this to you gettext:find task
  #
  # require 'active_record'
  # gem "gettext_activerecord", '>=0.1.0' #download and install from github
  # require 'gettext_activerecord/parser'
  desc "write the model attributes to <locale_path>/model_attributes.rb"
  task :store_model_attributes => :environment do
    FastGettext.silence_errors

    require 'gettext_i18n_rails/model_attributes_finder'
    require 'gettext_i18n_rails/active_record'

    storage_file = "#{locale_path}/model_attributes.rb"
    puts "writing model translations to: #{storage_file}"

    GettextI18nRails.store_model_attributes(
      :to => storage_file,
      :ignore_columns => [/_id$/, 'id', 'type', 'created_at', 'updated_at'],
      :ignore_tables => GettextI18nRails::IGNORE_TABLES
    )
  end

  desc "add a new language"
  task :add_language, [:language] => :environment do |_, args|
    language = args.language || ENV["LANGUAGE"]

    # Let's do some pre-verification of the environment.
    if language.nil?
      puts "You need to specify the language to add. Either 'LANGUAGE=eo rake gettext:add_language' or 'rake gettext:add_language[eo]'"
      next
    end

    language_path = File.join(locale_path, language)
    mkdir_p(language_path)
    Rake.application.lookup('gettext:find', _.scope).invoke
  end
end