Skip to content

Instantly share code, notes, and snippets.

@evadne
Created October 13, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evadne/a42df72b9d385e8058dfcfc2f4852556 to your computer and use it in GitHub Desktop.
Save evadne/a42df72b9d385e8058dfcfc2f4852556 to your computer and use it in GitHub Desktop.
Sanitise Gettext (Elixir) output for version control
# For all .POT files
# Parse PO to internal format
# Remove comments
# Remove duplicate references
# Sort terms by key
# Dump out data
# Bodge data with regex
# Remove leading #: for long reference
# Write file
# For all .PO files
# Parse PO to internal format
# Remove header
# Remove comments
# Remove references
# Remove flags
# Sort terms by key
# Dump data
# Write file
template_pot = "priv/gettext/*.pot"
template_po = "priv/gettext/*/LC_MESSAGES/*.po"
for {_, app_path} <- Mix.Project.apps_paths() do
for path <- Path.wildcard(Path.join([app_path, template_pot])) do
{:ok, data} = File.read(path)
{:ok, po} = Gettext.PO.parse_string(data)
translation_fun = fn translation ->
references = Enum.sort(Enum.uniq(translation.references))
flags = MapSet.new()
%{translation | comments: [], extracted_comments: [], references: references, flags: flags}
end
translations = po.translations |> Enum.map(translation_fun) |> Enum.sort_by(& &1.msgid)
iodata = Gettext.PO.dump(%{po | translations: translations})
data = IO.iodata_to_binary(iodata)
data = data |> String.replace(~r/\n#:\n/, "\n")
:ok = File.write(path, data)
end
for path <- Path.wildcard(Path.join([app_path, template_po])) do
{:ok, data} = File.read(path)
{:ok, po} = Gettext.PO.parse_string(data)
translation_fun = fn translation ->
flags = MapSet.new()
%{translation | comments: [], extracted_comments: [], references: [], flags: flags}
end
translations = po.translations |> Enum.map(translation_fun) |> Enum.sort_by(& &1.msgid)
iodata = Gettext.PO.dump(%{po | headers: [], translations: translations})
:ok = File.write(path, iodata)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment