Skip to content

Commit 7748127

Browse files
authored
feat: Support for i18next plurals functionality (#37)
1 parent b44847d commit 7748127

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

lua/js-i18n/analyzer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ end
7777
--- @param stop? integer 終了位置
7878
--- @return TSNode | nil, string | nil
7979
function M.get_node_for_key(bufnr, keys, start, stop)
80+
keys = { unpack(keys) }
8081
local key = table.concat(keys, c.config.key_separator)
8182
local ts = vim.treesitter
8283

lua/js-i18n/lsp/protocol/request/text_document_completion.lua

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@ local utils = require("js-i18n.utils")
44

55
--- 全ての翻訳を取得する関数
66
--- @param translation I18n.TranslationSource
7+
--- @param library? string ライブラリ
78
--- @param prefix? string
89
--- @param result? table<string, string>
910
--- @return table<string, string>
10-
local function get_all_translation(translation, prefix, result)
11+
local function get_all_translation(translation, library, prefix, result)
1112
result = result or {}
1213
for key, value in pairs(translation) do
1314
local full_key = prefix and prefix .. c.config.key_separator .. key or key
1415
if type(value) == "table" then
15-
get_all_translation(value, full_key, result)
16+
get_all_translation(value, library, full_key, result)
1617
else
17-
result[full_key] = value
18+
if library == utils.Library.I18Next then
19+
for _, suffix in ipairs(c.config.libraries.i18next.plural_suffixes) do
20+
-- 末尾がsuffixで終わる場合
21+
if full_key:sub(-#suffix) == suffix then
22+
local key_without_suffix = full_key:sub(1, -#suffix - 1)
23+
result[key_without_suffix] = result[key_without_suffix] or value
24+
goto continue
25+
end
26+
end
27+
end
28+
result[full_key] = result[full_key] or value
1829
end
30+
::continue::
1931
end
2032
return result
2133
end
@@ -28,12 +40,13 @@ end
2840
local function get_completion_items(client, bufnr, t_call)
2941
local lang = client:get_language(bufnr)
3042
local t_source = client.t_source_by_workspace[utils.get_workspace_root(bufnr)]
43+
local library = utils.detect_library(bufnr)
3144

3245
local key_prefix = t_call.key_prefix or ""
3346

3447
local translations = {}
3548
for _, source in pairs(t_source:get_translation_source_by_lang(lang)) do
36-
for key, value in pairs(get_all_translation(source)) do
49+
for key, value in pairs(get_all_translation(source, library)) do
3750
if key_prefix == "" then
3851
translations[key] = value
3952
else

lua/js-i18n/lsp/protocol/request/text_document_definition.lua

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,51 @@ local function handler(params, client)
2626
end
2727

2828
local key = t_call.key
29+
local library = utils.detect_library(bufnr)
2930

3031
for file, _ in pairs(t_source:get_translation_source_by_lang(lang)) do
3132
local bufnr = vim.api.nvim_create_buf(false, true)
3233
local content = Path:new(file):read()
3334
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(content, "\n"))
3435

36+
--- @type lsp.Location[]
37+
local result = {}
38+
3539
local keys = vim.split(key, c.config.key_separator, { plain = true })
3640
local node = analyzer.get_node_for_key(bufnr, keys)
37-
vim.api.nvim_buf_delete(bufnr, { force = true })
38-
3941
if node ~= nil then
4042
local row_start, col_start, row_end, col_end = node:range()
41-
return nil,
42-
{
43-
uri = vim.uri_from_fname(file),
44-
range = {
45-
start = { line = row_start, character = col_start },
46-
["end"] = { line = row_end, character = col_end },
47-
},
48-
}
43+
table.insert(result, {
44+
uri = vim.uri_from_fname(file),
45+
range = {
46+
start = { line = row_start, character = col_start },
47+
["end"] = { line = row_end, character = col_end },
48+
},
49+
})
50+
end
51+
52+
if library == utils.Library.I18Next then
53+
for _, suffix in ipairs(c.config.libraries.i18next.plural_suffixes) do
54+
local key_with_suffix = { unpack(keys) }
55+
key_with_suffix[#key_with_suffix] = key_with_suffix[#key_with_suffix] .. suffix
56+
57+
local node = analyzer.get_node_for_key(bufnr, key_with_suffix)
58+
if node ~= nil then
59+
local row_start, col_start, row_end, col_end = node:range()
60+
table.insert(result, {
61+
uri = vim.uri_from_fname(file),
62+
range = {
63+
start = { line = row_start, character = col_start },
64+
["end"] = { line = row_end, character = col_end },
65+
},
66+
})
67+
end
68+
end
69+
end
70+
71+
vim.api.nvim_buf_delete(bufnr, { force = true })
72+
if #result > 0 then
73+
return nil, result
4974
end
5075
end
5176

lua/js-i18n/lsp/protocol/request/text_document_hover.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ local function handler(params, client)
2424
local key = t_call.key
2525
local keys = vim.split(key, c.config.key_separator, { plain = true })
2626

27+
local library = utils.detect_library(bufnr)
28+
2729
-- 各言語の翻訳を表示
2830
--- @type string[]
2931
local contents = {}
3032
for _, lang in ipairs(t_source:get_available_languages()) do
31-
local translation = t_source:get_translation(lang, keys)
33+
local translation = t_source:get_translation(lang, keys, library)
3234
if translation then
3335
if type(translation) == "string" then
3436
table.insert(contents, lang .. ": `" .. translation .. "`")

0 commit comments

Comments
 (0)