LiveSvelteGettext.Extractor (LiveSvelteGettext v0.2.0)

View Source

Extracts translation strings from Svelte component files.

Scans .svelte files for gettext() and ngettext() calls and extracts the translation strings with their file and line number metadata.

Supported Patterns

  • gettext("string") - Simple translation
  • gettext("string", {...}) - Translation with interpolation variables
  • gettext("string", vars) - Any expression as the bindings argument
  • ngettext("singular", "plural", count) - Plural forms
  • Both single (') and double (") quotes
  • Escaped quotes (\' and \")
  • Multiple calls per line
  • Multi-line calls, including trailing commas

Only the leading string literal(s) need to match; whatever follows the msgid is ignored. This means an unusual bindings argument can never cause a string to be silently skipped.

Ignored Content

Translation calls are not extracted from:

  • HTML/Svelte comments (<!-- ... -->)
  • Block comments (/* ... */)
  • Whole-line JavaScript comments (a line whose first non-whitespace characters are //)

Comments are blanked out rather than deleted so that reported line numbers stay correct for the rest of the file.

Examples

iex> content = ~s|<button>{gettext("Save")}</button>|
iex> LiveSvelteGettext.Extractor.extract_from_content(content, "button.svelte")
[%{
  msgid: "Save",
  type: :gettext,
  plural: nil,
  references: [{"button.svelte", 1}]
}]

Summary

Functions

Deduplicates extractions by merging references for identical msgid/type/plural combinations.

Extracts all translation strings from a list of Svelte files.

Extracts translation strings from Svelte file content.

Extracts translation strings from a single Svelte file.

Types

extraction()

@type extraction() :: %{
  msgid: String.t(),
  type: :gettext | :ngettext,
  plural: String.t() | nil,
  references: [{file :: String.t(), line :: integer()}]
}

Functions

deduplicate(extractions)

@spec deduplicate([extraction()]) :: [extraction()]

Deduplicates extractions by merging references for identical msgid/type/plural combinations.

Examples

iex> extractions = [
...>   %{msgid: "Save", type: :gettext, plural: nil, references: [{"a.svelte", 1}]},
...>   %{msgid: "Save", type: :gettext, plural: nil, references: [{"b.svelte", 5}]}
...> ]
iex> LiveSvelteGettext.Extractor.deduplicate(extractions)
[%{msgid: "Save", type: :gettext, plural: nil, references: [{"a.svelte", 1}, {"b.svelte", 5}]}]

extract_all(files)

@spec extract_all([Path.t()]) :: [extraction()]

Extracts all translation strings from a list of Svelte files.

Returns deduplicated extractions with all file:line references merged.

Examples

iex> files = ["component1.svelte", "component2.svelte"]
iex> LiveSvelteGettext.Extractor.extract_all(files)
[%{msgid: "Save", type: :gettext, plural: nil, references: [...]}, ...]

extract_from_content(content, file)

@spec extract_from_content(String.t(), String.t()) :: [extraction()]

Extracts translation strings from Svelte file content.

Examples

iex> content = ~s|{gettext("Save")}|
iex> LiveSvelteGettext.Extractor.extract_from_content(content, "test.svelte")
[%{msgid: "Save", type: :gettext, plural: nil, references: [{"test.svelte", 1}]}]

extract_from_file(file)

@spec extract_from_file(Path.t()) :: [extraction()]

Extracts translation strings from a single Svelte file.

Returns an empty list if the file cannot be read.

Examples

iex> LiveSvelteGettext.Extractor.extract_from_file("component.svelte")
[%{msgid: "Hello", type: :gettext, ...}]