Class: I18nPoTools::LocfileUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_po_tools/utils/locfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirname) ⇒ LocfileUtils

Returns a new instance of LocfileUtils.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n_po_tools/utils/locfile.rb', line 19

def initialize(dirname)
  @translations_dir = ENV["I18N_YML_TRANSLATIONS_DIR"]
  @verbose = true

  @dirname = File.expand_path(dirname)
  config = YAML.load_file(File.join(dirname, 'Locfile'))

  @project_name = config["project_name"]
  @template_locales = config["template_locales"]
  @locales = config["locales"]
  @locales_indexes = config["locales_indexes"]

  @translation_files = config["translation_files"]
  @src_filename_template = config["src_filename_template"]
  @pot_filename_path = config["pot_filename_path"].gsub('%{project_name}', project_name)
  @po_filename_path = config["po_filename_path"].gsub('%{project_name}', project_name)
end

Instance Attribute Details

#dirnameObject

Returns the value of attribute dirname.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def dirname
  @dirname
end

#localesObject

Returns the value of attribute locales.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def locales
  @locales
end

#locales_indexesObject

Returns the value of attribute locales_indexes.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def locales_indexes
  @locales_indexes
end

#po_filename_pathObject

Returns the value of attribute po_filename_path.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def po_filename_path
  @po_filename_path
end

#pot_filename_pathObject

Returns the value of attribute pot_filename_path.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def pot_filename_path
  @pot_filename_path
end

#project_nameObject

Returns the value of attribute project_name.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def project_name
  @project_name
end

#src_filename_templateObject

Returns the value of attribute src_filename_template.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def src_filename_template
  @src_filename_template
end

#template_localesObject

Returns the value of attribute template_locales.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def template_locales
  @template_locales
end

#translation_filesObject

Returns the value of attribute translation_files.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def translation_files
  @translation_files
end

#translations_dirObject

Returns the value of attribute translations_dir.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def translations_dir
  @translations_dir
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/i18n_po_tools/utils/locfile.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#convert_from_po_to_src(locale) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/i18n_po_tools/utils/locfile.rb', line 84

def convert_from_po_to_src(locale)
  src_path = File.join(dirname, src_filename_template_with_locale(locale))
  po_path = File.join(translations_dir, po_filename_path.gsub('%{locale}',locale))

  translation_files.each_pair do |src_name, pot_name|
    src_filename = src_path.gsub('%{src_filename}',src_name)
    po_filename = File.join(po_path, pot_name+".po")

    cmds = []
    cmds << "i18n-po "\
      "--output #{src_filename} "\
      "--input #{po_filename} "\
      "#{convert_from_po_to_src_extra(locale, src_filename, po_filename)}"
    my_command( cmds.join(';') )
  end
end

#convert_from_po_to_src_extra(locale, src_filename, po_filename) ⇒ Object



129
130
131
# File 'lib/i18n_po_tools/utils/locfile.rb', line 129

def convert_from_po_to_src_extra(locale, src_filename, po_filename)
  ''
end

#convert_from_src_to_po(locale, base_locale) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/i18n_po_tools/utils/locfile.rb', line 101

def convert_from_src_to_po(locale, base_locale)
  base_path = File.join(dirname, src_filename_template_with_locale(base_locale))
  src_path = File.join(dirname, src_filename_template_with_locale(locale))
  po_path = File.join(translations_dir, po_filename_path.gsub('%{locale}',locale))

  translation_files.each_pair do |src_name, pot_name|
    base_filename = base_path.gsub('%{src_filename}', src_name)
    src_filename = src_path.gsub('%{src_filename}', src_name)
    po_filename = File.join(po_path, pot_name+".po")

    cmds = []
    cmds << "i18n-po "\
      "--output #{po_filename} "\
      "--input #{src_filename} "\
      "--language #{locale} "\
      "--base #{base_filename} "\
      "#{convert_from_src_to_po_extra(locale, base_locale, base_filename, src_filename, po_filename)}"
    my_command( cmds.join(';') )
  end
end

#convert_from_src_to_po_extra(locale, base_locale, base_filename, src_filename, po_filename) ⇒ Object



132
133
134
# File 'lib/i18n_po_tools/utils/locfile.rb', line 132

def convert_from_src_to_po_extra(locale, base_locale, base_filename, src_filename, po_filename)
  ''
end

#create_dirsObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n_po_tools/utils/locfile.rb', line 42

def create_dirs()
  template_locales.each do |locale|
    name = File.join(translations_dir, pot_filename_path.gsub('%{locale}',locale))
    my_command("mkdir -p #{name}")
  end
  locales.each do |(locale, base_locale)|
    name = File.join(translations_dir, po_filename_path.gsub('%{locale}',locale))
    my_command("mkdir -p #{name}")
  end
end

#export_initial_translationsObject



159
160
161
162
163
# File 'lib/i18n_po_tools/utils/locfile.rb', line 159

def export_initial_translations()
  locales.each do |(locale, base_locale)|
    convert_from_src_to_po(locale, base_locale)
  end
end

#export_messagesObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/i18n_po_tools/utils/locfile.rb', line 136

def export_messages()
  create_dirs()
  template_locales.each do |locale|
    generate_pot(locale)
  end

  #merge new strigns to po files
  puts
  puts
  puts "WARNING:"
  puts "Ensure that currently translators do not update your project translations."
  puts
  puts "Press [Enter] key to start export new strings from POT to PO-files..."
  gets
  merge_pot_all()
end

#generate_pot(locale) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/i18n_po_tools/utils/locfile.rb', line 53

def generate_pot(locale)
  src_path = File.join(dirname, src_filename_template_with_locale(locale))
  pot_path = File.join(translations_dir, pot_filename_path.gsub('%{locale}',locale))

  translation_files.each_pair do |src_name, pot_name|
    src_filename = src_path.gsub('%{src_filename}',src_name)
    pot_filename = File.join(pot_path, pot_name+".pot")

    cmds = []
    cmds << "i18n-po "\
      "--input #{src_filename} "\
      "--output #{pot_filename} "\
      "#{generate_pot_extra(locale, src_filename, pot_filename)}"
    my_command( cmds.join(';') )
  end
end

#generate_pot_extra(locale, src_filename, pot_filename) ⇒ Object



126
127
128
# File 'lib/i18n_po_tools/utils/locfile.rb', line 126

def generate_pot_extra(locale, src_filename, pot_filename)
  ''
end

#import_translationsObject



153
154
155
156
157
# File 'lib/i18n_po_tools/utils/locfile.rb', line 153

def import_translations()
  locales.each do |(locale, base_locale)|
    convert_from_po_to_src(locale)
  end
end

#merge_pot(locale, base_locale, pot_name) ⇒ Object



70
71
72
73
74
# File 'lib/i18n_po_tools/utils/locfile.rb', line 70

def merge_pot(locale, base_locale, pot_name)
  pot_filename = File.join(translations_dir, pot_filename_path.gsub('%{locale}',base_locale),pot_name+".pot")
  po_filename = File.join(translations_dir, po_filename_path.gsub('%{locale}',locale),pot_name+".po")
  my_command("msgmerge -U #{po_filename} #{pot_filename}")
end

#merge_pot_allObject



76
77
78
79
80
81
82
# File 'lib/i18n_po_tools/utils/locfile.rb', line 76

def merge_pot_all()
  locales.each do |(locale, base_locale)|
    translation_files.each_pair do |src_name, pot_name|
      merge_pot(locale, base_locale, pot_name)
    end
  end
end

#my_command(cmd) ⇒ Object



37
38
39
40
# File 'lib/i18n_po_tools/utils/locfile.rb', line 37

def my_command(cmd)
  puts cmd if verbose
  system(cmd)
end

#src_filename_template_with_locale(locale) ⇒ Object



122
123
124
# File 'lib/i18n_po_tools/utils/locfile.rb', line 122

def src_filename_template_with_locale(locale)
  src_filename_template.gsub('%{locale}',locale)
end