summaryrefslogtreecommitdiff
path: root/lib/tasks/i18n.rake
blob: daa18341cdd0c91fc4245f2812ee52b40555f7ff (plain)
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
require 'yaml'
require 'fileutils'

namespace :i18n do
  #
  # for coding, it helps to have the english strings in separate files.
  # for translating, it helps to have a single file. This action will combine
  # the small files into one big one.
  #
  # Also, transifex insists that the files be en_US and not just en.
  # grrr. transifex is configured to automatically update from the
  # file lib/en_US.yml.
  #
  desc "combine config/locales/*.en.yml to lib/en_US.yml"
  task :bundle do
    en_yml = File.join(Rails.root, 'lib', 'en_US.yml')
    Dir.chdir('config/locales/') do
      if File.exists?(en_yml)
        File.unlink en_yml
      end
      File.open(en_yml, 'w') do |output|
        output.write("en_US:\n\n"+
          "### Do NOT edit this file directly, as all changes will be overwritten by `rake i18:bundle`\n"+
          "### Instead, make changes in the appropriate file in config/locales.\n"+
          "### Source strings in transifex are automatically updated from this file (via github url)")

        Dir.glob('*.en.yml').sort.each do |file|
          ## print separator to see where another file begins
          output.write("\n\n" + '#' * 40 + "\n" + '### ' + file + "\n")
          output.write(
            # remove the toplevel "en" key
            YAML.load_file(file)['en'].
            to_yaml.
            # prefix all lines with two spaces (we're nested below "en:")
            lines.map {|line| "  #{line}"}[
              1..-1 # << skip the first line (it's "---" and freaks out the parser if it sees it multiple times in a file)
            ].join)
        end
      end
      puts "You can now find the bundled locale yml in: #{en_yml}"
    end
  end


  # elijah: this does not actually work, but I think could be a starting point
  # for something that will work.
  desc "pull translations from transifex"
  task :download do
    Conf.enabled_languages.each do |lang|
      next unless lang == 'en'
      `curl -L --user #{Conf.transifex_user}:#{Conf.transifex_password} -X GET https://www.transifex.net/api/2/project/crabgrass/resource/master/translation/#{lang}/?file > config/locales/#{lang}.yml`
    end
  end
end