A Neovim plugin that wraps translate-shell so you can translate text directly from Neovim.
Make sure you have translate-shell installed:
# macOS
brew install translate-shell
# Linux (Debian/Ubuntu)
sudo apt-get install translate-shell
# Or install via wget
wget git.io/trans
chmod +x ./transUsing lazy.nvim
{
"nicholasxjy/translator.nvim",
event = "VeryLazy",
opts = {
default_target_lang = "zh", -- Default target language
default_source_lang = nil, -- Default source language (nil = auto-detect)
window = {
width = 80, -- Popup window width
height = 20, -- Popup window max height
title = " Translation ", -- Popup window title
border = "rounded", -- Border style: "rounded", "single", "double", "solid", "shadow", "none"
title_pos = "center", -- Title position: "center", "left", "right"
},
},
}vim.pack is Neovim's built-in plugin manager and currently requires a recent
Neovim build with vim.pack support plus git on your PATH.
vim.pack.add({
{ src = "https://github.com/nicholasxjy/translator.nvim" },
}, { load = true })
require("translator").setup({
default_target_lang = "zh", -- Default target language
default_source_lang = nil, -- Default source language (nil = auto-detect)
window = {
width = 80, -- Popup window width
height = 20, -- Popup window max height
title = " Translation ", -- Popup window title
border = "rounded", -- Border style: "rounded", "single", "double", "solid", "shadow", "none"
title_pos = "center", -- Title position: "center", "left", "right"
},
})After adding the plugin, restart Neovim if needed. To update plugins managed by
vim.pack, run :lua vim.pack.update().
" Translate specific text to Chinese
:Trans text=hello to=zh
" Translate a sentence without quotes
:Trans hello world to=zh
" Translate a sentence with quotes
:Trans text="hello world" to=zh
" Translate visual selection to Chinese (select text first, then run command)
:'<,'>Trans to=zh
" Translate word under cursor to Chinese
:TransWord to=zh
" Translate word under cursor with source and target language
:TransWord from=en to=zh
" Translate word under cursor using default target language
:TransWord
" Translate with source and target language
:Trans text=你好 from=zh to=enYou can also call the translation functions directly from Lua code:
-- Translate current word under cursor
require('translator').transCurWord({ to = "zh" })
require('translator').transCurWord({ from = "en", to = "zh" })
-- Translate visual selection
require('translator').transVisualSel({ to = "en" })
require('translator').transVisualSel({ from = "zh", to = "en" })
-- Example: Custom keybinding using Lua API
vim.keymap.set('n', '<leader>tw', function()
require('translator').transCurWord({ to = "zh" })
end, { desc = "Translate word to Chinese" })
vim.keymap.set('v', '<leader>ts', function()
require('translator').transVisualSel({ to = "zh" })
end, { desc = "Translate selection to Chinese" }){
"nicholasxjy/translator.nvim",
event = "VeryLazy",
opts = {
default_target_lang = "zh",
window = {
width = 80,
height = 20,
title = " Translation ",
border = "rounded",
title_pos = "center",
},
},
keys = {
-- Translate visual selection to Chinese
{ "<leader>tc", ":'<,'>Trans to=zh<cr>", mode = "v", desc = "Translate to Chinese" },
-- Translate visual selection to English
{ "<leader>te", ":'<,'>Trans to=en<cr>", mode = "v", desc = "Translate to English" },
-- Translate visual selection to Japanese
{ "<leader>tj", ":'<,'>Trans to=ja<cr>", mode = "v", desc = "Translate to Japanese" },
-- Translate word under cursor to Chinese
{ "<leader>tw", "<cmd>TransWord to=zh<cr>", mode = "n", desc = "Translate word to Chinese" },
-- Translate word under cursor using default language
{ "<leader>tt", "<cmd>TransWord<cr>", mode = "n", desc = "Translate word" },
}
}- Popup Window: Translation results are displayed in a centered popup window with rounded borders
- Visual Selection: Select text and translate it directly
- Word Translation: Translate the word under cursor with
:TransWordcommand - Multi-word Command Input:
:Transsupports bare text and quotedtext=values - Full Translation Results: Display complete translation information including pronunciation, definitions, and examples
- Markdown Formatting: Translation results are displayed with markdown syntax highlighting
- Flexible Parameters: Support for
text=,to=, andfrom=parameters - Async Execution: Translation runs asynchronously to avoid blocking Neovim
- Error Handling: Shows clear notifications when translation fails or
transis missing - Easy Close: Press
qor<Esc>to close the popup window

