-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for XLIFF 1.2 file format
- Loading branch information
Showing
25 changed files
with
317 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Langue.Document do | ||
defstruct path: nil, header: nil, top_of_the_file_comment: nil, master_language: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Langue.Formatter.XLIFF12.Parser do | ||
@behaviour Langue.Formatter.Parser | ||
|
||
alias Langue.Entry | ||
alias Langue.Utils.Placeholders | ||
|
||
def parse(%{render: render}) do | ||
render | ||
|> :erlsom.simple_form() | ||
|> case do | ||
{:ok, {'file', _attributes, [{'body', _, body}]}, _} -> | ||
entries = | ||
body | ||
|> Enum.with_index(1) | ||
|> Enum.map(&parse_line/1) | ||
|> Enum.reject(&is_nil/1) | ||
|> Placeholders.parse(Langue.Formatter.XLIFF12.placeholder_regex()) | ||
|
||
%Langue.Formatter.ParserResult{entries: entries} | ||
|
||
_ -> | ||
Langue.Formatter.ParserResult.empty() | ||
end | ||
end | ||
|
||
defp parse_line({{'trans-unit', [{'id', key}], [{'source', [], [source]}, {'target', [], [value]}]}, index}) do | ||
key = IO.chardata_to_string(key) | ||
value = IO.chardata_to_string(value) | ||
source = IO.chardata_to_string(source) | ||
|
||
%Entry{ | ||
value: value, | ||
master_value: source, | ||
value_type: Langue.ValueType.parse(value), | ||
key: key, | ||
index: index | ||
} | ||
end | ||
|
||
defp parse_line(_), do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule Langue.Formatter.XLIFF12.Serializer do | ||
@behaviour Langue.Formatter.Serializer | ||
|
||
def serialize(%{entries: entries, language: %{slug: slug}, document: %{master_language: master_language, path: path}}) when slug === master_language do | ||
file_attributes = [ | ||
original: path, | ||
datatype: "plaintext", | ||
"source-language": master_language | ||
] | ||
|
||
body = [ | ||
{"body", [], Enum.map(entries, &parse_master/1)} | ||
] | ||
|
||
serialize_body(file_attributes, body) | ||
end | ||
|
||
def serialize(%{entries: entries, language: language, document: document}) do | ||
file_attributes = [ | ||
original: document.path, | ||
datatype: "plaintext", | ||
"source-language": document.master_language, | ||
"target-language": language.slug | ||
] | ||
|
||
body = [ | ||
{"body", [], Enum.map(entries, &parse_target/1)} | ||
] | ||
|
||
serialize_body(file_attributes, body) | ||
end | ||
|
||
defp serialize_body(file_attributes, body) do | ||
render = | ||
{"file", file_attributes, body} | ||
|> XmlBuilder.generate() | ||
|> Kernel.<>("\n") | ||
|
||
%Langue.Formatter.SerializerResult{render: render} | ||
end | ||
|
||
defp parse_target(%{key: key, master_value: master_value, value: value}) do | ||
{ | ||
"trans-unit", | ||
[id: key], | ||
[ | ||
{"source", [], master_value}, | ||
{"target", [], value} | ||
] | ||
} | ||
end | ||
|
||
defp parse_master(%{key: key, value: value}) do | ||
{ | ||
"trans-unit", | ||
[id: key], | ||
[ | ||
{"source", [], value}, | ||
{"target", [], ""} | ||
] | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Langue.Formatter.XLIFF12 do | ||
@behaviour Langue.Formatter | ||
|
||
alias Langue.Formatter.XLIFF12.{Parser, Serializer} | ||
|
||
def name, do: "xliff_1_2" | ||
def placeholder_regex, do: :not_supported | ||
|
||
defdelegate parse(map), to: Parser | ||
defdelegate serialize(map), to: Serializer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Langue.Language do | ||
defstruct slug: nil, plural_forms: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.