Skip to content

Commit 947190a

Browse files
committed
Fix language detection crash when detected language_code is ""
1 parent 7c987f0 commit 947190a

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/desktop.ex

+18-12
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,25 @@ defmodule Desktop do
5757
# Preferring a full match 'xx_xx' == 'yy_yy'
5858
best_match = Enum.find(known_locales, fn l -> String.downcase(l) == language_code end)
5959

60-
if best_match != nil do
61-
put_default_locale(best_match)
62-
else
63-
# Looking for a prefix match 'xx' == 'yy'
64-
prefix = binary_part(language_code, 0, 2)
60+
cond do
61+
best_match != nil ->
62+
put_default_locale(best_match)
63+
64+
# We have seen windows return an empty string for language_code
65+
byte_size(language_code) >= 2 ->
66+
# Looking for a prefix match 'xx' == 'yy'
67+
prefix = binary_part(language_code, 0, 2)
68+
69+
prefix_match =
70+
Enum.find(known_locales, fn l -> String.starts_with?(String.downcase(l), prefix) end)
6571

66-
prefix_match =
67-
Enum.find(known_locales, fn l -> String.starts_with?(String.downcase(l), prefix) end)
72+
if prefix_match != nil do
73+
put_default_locale(prefix_match)
74+
end
6875

69-
if prefix_match != nil do
70-
put_default_locale(prefix_match)
71-
else
76+
true ->
7277
# we're giving up, not updating the default locale
73-
end
78+
nil
7479
end
7580
end
7681

@@ -89,7 +94,8 @@ defmodule Desktop do
8994
_ ->
9095
:wx.set_env(Desktop.Env.wx_env())
9196
locale = :wxLocale.new(:wxLocale.getSystemLanguage())
92-
# this is in the form "xx_XX"
97+
# This is in the form "xx_XX"
98+
# we have seen windows returns an empty string though...
9399
:wxLocale.getCanonicalName(locale) |> List.to_string() |> String.downcase()
94100
end
95101
end

0 commit comments

Comments
 (0)