aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Khramtsov <ekhramtsov@process-one.net>2019-07-11 00:04:32 +0300
committerEvgeny Khramtsov <ekhramtsov@process-one.net>2019-07-11 00:04:32 +0300
commit6fd736d496796505992f5ad521e504855d81c4a9 (patch)
tree863f9af92611858dc248864c3dd3bfa0e9c65d4e
parentFix formatting (diff)
Gracefully report invalid encoding of a translation file
-rw-r--r--src/translate.erl15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/translate.erl b/src/translate.erl
index 562df38d8..12d5b6c04 100644
--- a/src/translate.erl
+++ b/src/translate.erl
@@ -40,7 +40,8 @@
-define(ZERO_DATETIME, {{0,0,0}, {0,0,0}}).
-type error_reason() :: file:posix() | {integer(), module(), term()} |
- badarg | terminated | system_limit | bad_file.
+ badarg | terminated | system_limit | bad_file |
+ bad_encoding.
-record(state, {}).
@@ -146,9 +147,13 @@ load_file(File) ->
{ok, Lines} ->
lists:map(
fun({In, Out}) ->
- InB = iolist_to_binary(In),
- OutB = iolist_to_binary(Out),
- {{Lang, InB}, OutB};
+ try
+ InB = unicode:characters_to_binary(In, utf8),
+ OutB = unicode:characters_to_binary(Out, utf8),
+ {{Lang, InB}, OutB}
+ catch _:badarg ->
+ {error, File, bad_encoding}
+ end;
(_) ->
{error, File, bad_file}
end, Lines);
@@ -289,6 +294,8 @@ lang_of_file(FileName) ->
-spec format_error(error_reason()) -> string().
format_error(bad_file) ->
"corrupted or invalid translation file";
+format_error(bad_encoding) ->
+ "not an UTF-8 encoding";
format_error({_, _, _} = Reason) ->
"at line " ++ file:format_error(Reason);
format_error(Reason) ->