Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ tags
# tests
/tmp_home/
.luarc.json

./po/build/*
!./po/build/README.md
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test:
@mkdir -p tmp_home
@export XDG_DATA_HOME='./tmp_home' && \
export XDG_CONFIG_HOME='./tmp_home' && \
export NVIM_TEST=1 && \
bash ./scripts/test_runner.sh
@rm -rf tmp_home

Expand All @@ -21,6 +22,7 @@ testcov:
@mkdir -p ./tmp_home/config/nvim
@export XDG_DATA_HOME=$(realpath './tmp_home/data') && \
export XDG_CONFIG_HOME=$(realpath './tmp_home/config') && \
export NVIM_TEST=1 && \
export TEST_COV=true && \
bash ./scripts/test_runner.sh
@luacov-console lua/
Expand Down
2 changes: 1 addition & 1 deletion lua/lualine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ local function setup_theme()
elseif ok and (type(dynamic_theme) == 'table') then
return dynamic_theme
else
local error_message = 'Invalid theme type returned from function: ' .. type(dynamic_theme)
local error_message = Msgstr('Invalid theme type returned from function: %s') .. type(dynamic_theme)
notify_theme_error(error_message)
return dynamic_theme
end
Expand Down
3 changes: 2 additions & 1 deletion lua/lualine/components/buffers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- MIT license, see LICENSE for more details.
local require = require('lualine_require').require
local Buffer = require('lualine.components.buffers.buffer')
local Msgstr = require('lualine.langMSG').Msgstr
local M = require('lualine.component'):extend()
local highlight = require('lualine.highlight')

Expand Down Expand Up @@ -226,7 +227,7 @@ function M.buffer_jump(buf_pos, bang)
end
if buf_pos < 1 or buf_pos > #M.bufpos2nr then
if bang ~= '!' then
error('Error: Unable to jump buffer position out of range')
error(Msgstr('Error: Unable to jump buffer position out of range'))
else
return
end
Expand Down
5 changes: 3 additions & 2 deletions lua/lualine/components/filename.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
local Msgstr = require('lualine.langMSG').Msgstr

local modules = require('lualine_require').lazy_require {
utils = 'lualine.utils.utils',
Expand All @@ -10,8 +11,8 @@ local default_options = {
symbols = {
modified = '[+]',
readonly = '[-]',
unnamed = '[No Name]',
newfile = '[New]',
unnamed = Msgstr("[No Name]"),
newfile = Msgstr("[New]"),
},
file_status = true,
newfile_status = false,
Expand Down
6 changes: 4 additions & 2 deletions lua/lualine/components/progress.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local Msgstr = require('lualine.langMSG').Msgstr

local function progress()
local cur = vim.fn.line('.')
local total = vim.fn.line('$')
if cur == 1 then
return 'Top'
return Msgstr('Top')
elseif cur == total then
return 'Bot'
return Msgstr('Bot')
else
return string.format('%2d%%%%', math.floor(cur / total * 100))
end
Expand Down
33 changes: 17 additions & 16 deletions lua/lualine/highlight.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = {}
local Msgstr = require('lualine.langMSG').Msgstr
local lualine_require = require('lualine_require')
local require = lualine_require.require
local modules = lualine_require.lazy_require {
Expand All @@ -18,20 +19,20 @@ local loaded_highlights = {}

-- table to map mode to highlight suffixes
local mode_to_highlight = {
['VISUAL'] = '_visual',
['V-BLOCK'] = '_visual',
['V-LINE'] = '_visual',
['SELECT'] = '_visual',
['S-LINE'] = '_visual',
['S-BLOCK'] = '_visual',
['REPLACE'] = '_replace',
['V-REPLACE'] = '_replace',
['INSERT'] = '_insert',
['COMMAND'] = '_command',
['EX'] = '_command',
['MORE'] = '_command',
['CONFIRM'] = '_command',
['TERMINAL'] = '_terminal',
[Msgstr('VISUAL')] = '_visual',
[Msgstr('V-BLOCK')] = '_visual',
[Msgstr('V-LINE')] = '_visual',
[Msgstr('SELECT')] = '_visual',
[Msgstr('S-LINE')] = '_visual',
[Msgstr('S-BLOCK')] = '_visual',
[Msgstr('REPLACE')] = '_replace',
[Msgstr('V-REPLACE')] = '_replace',
[Msgstr('INSERT')] = '_insert',
[Msgstr('COMMAND')] = '_command',
[Msgstr('EX')] = '_command',
[Msgstr('MORE')] = '_command',
[Msgstr('CONFIRM')] = '_command',
[Msgstr('TERMINAL')] = '_terminal',
}

--- Get highlight suffix for current mode, or inactive if not focused
Expand Down Expand Up @@ -80,7 +81,7 @@ local function sanitize_color(color)
return modules.color_utils.color_name2rgb(color)
elseif type(color) == 'number' then
if color > 255 then
error("What's this it can't be higher then 255 and you've given " .. color)
error(Msgstr("What's this it can't be higher then 255 and you've given %s", {tostring(color)}))
end
return modules.color_utils.cterm2rgb(color)
end
Expand All @@ -92,7 +93,7 @@ end
local function sanitize_color_for_cterm(color)
if type(color) == 'number' then
if color > 255 then
error("What's this it can't be higher then 255 and you've given " .. color)
error(Msgstr("What's this it can't be higher then 255 and you've given %s", {tostring(color)}))
end
return color
end
Expand Down
88 changes: 88 additions & 0 deletions lua/lualine/langMSG.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-- Current Language
-- local lang = string.sub(vim.v.lang, 1, 2)

local lang = os.getenv("NVIM_TEST") and "en" or string.sub(vim.v.lang, 1, 2)
local M = {}
local info = debug.getinfo(1, "S")
local script_dir = info.source:sub(2):match("(.*/)")
local po_path = script_dir .. "/../../po/" .. lang .. ".po"

-- Basic PO file parser
local function parse_po(path)
local file = io.open(path, "r")
if not file then return {} end

local translations = {}
local mode = nil
local current_msgid = {}
local current_msgstr = {}

local function unescape(str)
return str:gsub('\\"', '"'):gsub("\\n", "\n")
end

for line in file:lines() do
if line:match('^msgid%s+"') then
mode = "msgid"
current_msgid = { line:match('^msgid%s+"(.*)"') }
current_msgstr = {}
elseif line:match('^msgstr%s+"') then
mode = "msgstr"
current_msgstr = { line:match('^msgstr%s+"(.*)"') }
elseif line:match('^"') then
local str = line:match('^"(.*)"')
if mode == "msgid" then
table.insert(current_msgid, str)
elseif mode == "msgstr" then
table.insert(current_msgstr, str)
end
elseif line == "" then
-- End of one entry
if #current_msgid > 0 and #current_msgstr > 0 then
local msgid_text = unescape(table.concat(current_msgid))
local msgstr_text = unescape(table.concat(current_msgstr))
translations[msgid_text] = msgstr_text
end
mode = nil
current_msgid = {}
current_msgstr = {}
end
end

-- Handle the last entry if file doesn't end with blank line
if #current_msgid > 0 and #current_msgstr > 0 then
local msgid_text = unescape(table.concat(current_msgid))
local msgstr_text = unescape(table.concat(current_msgstr))
translations[msgid_text] = msgstr_text
end

file:close()
return translations
end

-- Load translations
local translatedTable = parse_po(po_path)

--- This function returns the translation if available and not empty
--- @param description string Description to translate
--- @param values string[]? Optional list of variables
--- @return string
function M.Msgstr(description, values)
local translated = translatedTable[description]
if not translated or translated == "" then
translated = description
end

if values then
local unpack = table.unpack or unpack

local ok, formatted = pcall(string.format, translated, unpack(values))
if ok then
translated = formatted
end
end

return translated
end

return M
73 changes: 37 additions & 36 deletions lua/lualine/utils/mode.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local Mode = {}
local Msgstr = require('lualine.langMSG').Msgstr

-- stylua: ignore
Mode.map = {
['n'] = 'NORMAL',
['no'] = 'O-PENDING',
['nov'] = 'O-PENDING',
['noV'] = 'O-PENDING',
['no\22'] = 'O-PENDING',
['niI'] = 'NORMAL',
['niR'] = 'NORMAL',
['niV'] = 'NORMAL',
['nt'] = 'NORMAL',
['ntT'] = 'NORMAL',
['v'] = 'VISUAL',
['vs'] = 'VISUAL',
['V'] = 'V-LINE',
['Vs'] = 'V-LINE',
['\22'] = 'V-BLOCK',
['\22s'] = 'V-BLOCK',
['s'] = 'SELECT',
['S'] = 'S-LINE',
['\19'] = 'S-BLOCK',
['i'] = 'INSERT',
['ic'] = 'INSERT',
['ix'] = 'INSERT',
['R'] = 'REPLACE',
['Rc'] = 'REPLACE',
['Rx'] = 'REPLACE',
['Rv'] = 'V-REPLACE',
['Rvc'] = 'V-REPLACE',
['Rvx'] = 'V-REPLACE',
['c'] = 'COMMAND',
['cv'] = 'EX',
['ce'] = 'EX',
['r'] = 'REPLACE',
['rm'] = 'MORE',
['r?'] = 'CONFIRM',
['!'] = 'SHELL',
['t'] = 'TERMINAL',
['n'] = Msgstr('NORMAL'),
['no'] = Msgstr('O-PENDING'),
['nov'] = Msgstr('O-PENDING'),
['noV'] = Msgstr('O-PENDING'),
['no\22'] = Msgstr('O-PENDING'),
['niI'] = Msgstr('NORMAL'),
['niR'] = Msgstr('NORMAL'),
['niV'] = Msgstr('NORMAL'),
['nt'] = Msgstr('NORMAL'),
['ntT'] = Msgstr('NORMAL'),
['v'] = Msgstr('VISUAL'),
['vs'] = Msgstr('VISUAL'),
['V'] = Msgstr('V-LINE'),
['Vs'] = Msgstr('V-LINE'),
['\22'] = Msgstr('V-BLOCK'),
['\22s'] = Msgstr('V-BLOCK'),
['s'] = Msgstr('SELECT'),
['S'] = Msgstr('S-LINE'),
['\19'] = Msgstr('S-BLOCK'),
['i'] = Msgstr('INSERT'),
['ic'] = Msgstr('INSERT'),
['ix'] = Msgstr('INSERT'),
['R'] = Msgstr('REPLACE'),
['Rc'] = Msgstr('REPLACE'),
['Rx'] = Msgstr('REPLACE'),
['Rv'] = Msgstr('V-REPLACE'),
['Rvc'] = Msgstr('V-REPLACE'),
['Rvx'] = Msgstr('V-REPLACE'),
['c'] = Msgstr('COMMAND'),
['cv'] = Msgstr('EX'),
['ce'] = Msgstr('EX'),
['r'] = Msgstr('REPLACE'),
['rm'] = Msgstr('MORE'),
['r?'] = Msgstr('CONFIRM'),
['!'] = Msgstr('SHELL'),
['t'] = Msgstr('TERMINAL')
}

---@return string current mode name
Expand Down
38 changes: 38 additions & 0 deletions po/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.31.6)
project(Internationalization)

set(NVIM_DIR "${CMAKE_SOURCE_DIR}/..")
set(TRANSLATION_DIR "${CMAKE_SOURCE_DIR}")
set(TEMPLATE_FILE "${TRANSLATION_DIR}/template.pot")
set(TRANSLATION_SCRIPT_TYPE "po")

set(LANGUAGES en ja)

file(MAKE_DIRECTORY ${TRANSLATION_DIR})
file(GLOB_RECURSE LUA_FILES "${NVIM_DIR}/*.lua")

add_custom_command(
OUTPUT ${TEMPLATE_FILE}
DEPENDS ${LUA_FILES}
COMMAND bash -c "mkdir -p '${TRANSLATION_DIR}' && cd '${NVIM_DIR}' && xgettext -L Lua --keyword=Msgstr --from-code=UTF-8 --directory=. -o '${TEMPLATE_FILE}' $(find . -name '*.lua')"
COMMENT "Generating template.pot from Lua source"
VERBATIM
)

foreach(LANGUAGE ${LANGUAGES})
set(PO_FILE "${TRANSLATION_DIR}/${LANGUAGE}.${TRANSLATION_SCRIPT_TYPE}")

add_custom_command(
OUTPUT ${PO_FILE}
DEPENDS ${TEMPLATE_FILE}
COMMAND bash -c "if [ -f '${PO_FILE}' ]; then msgmerge --update --backup=off '${PO_FILE}' '${TEMPLATE_FILE}'; else cp '${TEMPLATE_FILE}' '${PO_FILE}' && sed -i '' -e 's/charset=CHARSET/charset=UTF-8/' -e 's/^\\\"Language:.*\\\"$/\\\"Language: ${LANGUAGE}\\\\n\\\"/' '${PO_FILE}'; fi"
COMMENT "Merging or creating ${LANGUAGE}.po"
VERBATIM
)

add_custom_target(${LANGUAGE}_po ALL DEPENDS ${PO_FILE})
endforeach()

add_custom_target(update_translations ALL
DEPENDS ${TEMPLATE_FILE}
)
17 changes: 17 additions & 0 deletions po/build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Internationalization

To make your own translations do the following:

1. Add the language in the CMakeLists.txt in line `9`
```cmake
set(LANGUAGES en ja)
```
2. Go to `~/po/build/` and run

```
cmake ..
build
```

3. Now your new translation file should be in `../lang.po` with `lang` being the name of
the translaiton.
Loading