aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/locale.lua
blob: f6bc32cb3d96ec93848bdc157ebfb4fc42ed50e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- Copyright 2007-2020 Mitchell mitchell.att.foicica.com. See LICENSE.

local M = {}

--[[ This comment is for LuaDoc.
---
-- Map of all messages used by Textadept to their localized form.
-- If the table does not contain the localized version of a given message, it
-- returns a string that starts with "No Localization:" via a metamethod.
-- Note: the terminal version ignores any "_" mnemonics the GUI version would
-- use.
module('_L')]]

local f = io.open(_USERHOME .. '/locale.conf', 'rb')
if not f then
  local lang = (os.getenv('LANG') or ''):match('^[^_.@]+') -- TODO: LC_MESSAGES?
  if lang then
    f = io.open(string.format('%s/core/locales/locale.%s.conf', _HOME, lang))
  end
end
if not f then f = io.open(_HOME .. '/core/locale.conf', 'rb') end
assert(f, '"core/locale.conf" not found')
for line in f:lines() do
  -- Any line that starts with a non-word character except '[' is considered a
  -- comment.
  if not line:find('^%s*[^%w_%[]') then
    local id, str = line:match('^(.-)%s*=%s*(.+)$')
    if id and str and assert(not M[id], 'duplicate locale key "%s"', id) then
      M[id] = not CURSES and str or str:gsub('_', '')
    end
  end
end
f:close()

setmetatable(M, {__index = function(_, k) return 'No Localization:' .. k end})
return M