Skip to content

Commit

Permalink
fix: Occur error when can't detect language (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
nabekou29 authored Sep 16, 2024
1 parent 56ff140 commit 52209c5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/js-i18n/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function M.default_detect_language(path)
local abs_path = vim.fn.fnamemodify(path, ":p")
local split = vim.split(abs_path, "[/.]")

local lang = nil
local lang = "unknown"

for _, part in ipairs(vim.fn.reverse(split)) do
if LangSet[normalize_lang(part)] then
Expand Down
7 changes: 5 additions & 2 deletions lua/js-i18n/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ end
--- @return string|nil ライブラリの識別子
function M.detect_library(bufnr)
local root = M.get_workspace_root(bufnr)
local package_json = root .. "/package.json"

local package = vim.fn.json_decode(vim.fn.readfile(package_json))
local ok, package_json = pcall(vim.fn.readfile, root .. "/package.json")
if not ok then
return nil
end

local package = vim.fn.json_decode(package_json)
if package == nil then
return nil
end
Expand Down
4 changes: 2 additions & 2 deletions tests/js-i18n/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ describe("js-i18n.config", function()
local tests = {
{ path = "/path/to/locals/en/trans.json", expected = "en" },
{ path = "/path/to/locals/ja/trans.json", expected = "ja" },
{ path = "/path/to/locals/hoge/trans.json", expected = nil },
{ path = "/path/to/locals/hoge/trans.json", expected = "unknown" },

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

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

0 comments on commit 52209c5

Please sign in to comment.