Skip to content

Commit 52209c5

Browse files
authored
fix: Occur error when can't detect language (#27)
1 parent 56ff140 commit 52209c5

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

lua/js-i18n/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function M.default_detect_language(path)
1818
local abs_path = vim.fn.fnamemodify(path, ":p")
1919
local split = vim.split(abs_path, "[/.]")
2020

21-
local lang = nil
21+
local lang = "unknown"
2222

2323
for _, part in ipairs(vim.fn.reverse(split)) do
2424
if LangSet[normalize_lang(part)] then

lua/js-i18n/utils.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ end
2323
--- @return string|nil ライブラリの識別子
2424
function M.detect_library(bufnr)
2525
local root = M.get_workspace_root(bufnr)
26-
local package_json = root .. "/package.json"
2726

28-
local package = vim.fn.json_decode(vim.fn.readfile(package_json))
27+
local ok, package_json = pcall(vim.fn.readfile, root .. "/package.json")
28+
if not ok then
29+
return nil
30+
end
2931

32+
local package = vim.fn.json_decode(package_json)
3033
if package == nil then
3134
return nil
3235
end

tests/js-i18n/config_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ describe("js-i18n.config", function()
55
local tests = {
66
{ path = "/path/to/locals/en/trans.json", expected = "en" },
77
{ path = "/path/to/locals/ja/trans.json", expected = "ja" },
8-
{ path = "/path/to/locals/hoge/trans.json", expected = nil },
8+
{ path = "/path/to/locals/hoge/trans.json", expected = "unknown" },
99

1010
-- Test cases to verify that it is sufficient for the languagee name to be included somewhere.
1111
{ path = "/path/to/locals/sub/en.json", expected = "en" },
1212
{ path = "/path/to/en/locals/trans.json", expected = "en" },
13-
{ path = "/path/to/locals/en-trans.json", expected = nil },
13+
{ path = "/path/to/locals/en-trans.json", expected = "unknown" },
1414

1515
-- Test cases for language names with any case and separating characters.
1616
{ path = "/path/to/locals/en-us/trans.json", expected = "en-us" },

0 commit comments

Comments
 (0)