File: locales_utils.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (167 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--
-- (C) 2014-22 - ntop.org
--

local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/i18n/?.lua;" .. package.path

i18n = require "i18n"

local locales = {}

-- ##############################################

local default_locale = "en"
local default_locale_path = dirs.installdir..'/scripts/locales/en.lua'
local available_locales = {}
local locales_initialized = false

local supported_locales = {
   {code = "en"},
   {code = "cn"},
   {code = "it"},
   {code = "de"},
   {code = "jp"},
   {code = "pt"},
   {code = "cz"}
}

local nedge_supported_locales = {
   ["jp"] = true,
   ["en"] = true,
}

-- ##############################################

local function loadfile_to_data(file_path)
  local data = {}
  -- `loadfile` opens the named `file_path`, parses it and returns the compiled chunk as a function.
  -- Does not execute it.
  local chunk = assert(loadfile(file_path))

  data = chunk()

  -- Execution can return nil when the parsed `file_path` does not end with a return statement
  if not data then
    traceError(TRACE_ERROR, TRACE_CONSOLE, string.format("Execution of loaded chunk returned nil for %s", file_path))
  end

  return data or {}
end

-- ##############################################

function locales.loadLocaleFile(path, locale)
  local data = loadfile_to_data(path)
  local os_utils = require("os_utils")
  local plugins_utils = require("plugins_utils")

  -- Check if plugin specific locales exist
  local plugins_locales = os_utils.fixPath(plugins_utils.getRuntimePath() .. "/locales/" .. locale .. ".lua")

  if ntop.exists(plugins_locales) then
     local plugins_data = loadfile_to_data(plugins_locales)

    -- Add the plugins localized strings
    for k, v in pairs(plugins_data) do
      data[k] = v
    end
  end
  
  i18n.load({[locale]=data})
end

-- ##############################################

local function lookupLocale(localename)
   local base_path = dirs.installdir..'/scripts/locales/'
   local locale_path = base_path .. localename .. ".lua"

   if ntop.exists(locale_path) then
      return locale_path
   end

   return nil
end

-- ##############################################

local function initLocales()
   if(locales_initialized) then
      -- Already initialized
      return
   end

   local is_nedge = ntop.isnEdge()

   -- Provides a fallback for not already localized strings
   locales.loadLocaleFile(default_locale_path, default_locale)

   locales.default_locale = default_locale

   -- language is a global variable set from C that corresponds to the user default language
   -- it may be null when lua_utils are imported from periodic scripts
   if language == nil then
      local admin_lang = ntop.getPref("ntopng.user.admin.language")
      language = ternary(isEmptyString(admin_lang), locales.default_locale, admin_lang)
   end

   i18n.setLocale(language)

   -- Note: en already loaded
   if ((not is_nedge) or nedge_supported_locales[language]) then
      local locale_path = lookupLocale(language)

      if locale_path then
         locales.loadLocaleFile(locale_path, language)
      end
   end

   -- use pairsByKeys to impose an order
   for _, locale in ipairs(supported_locales) do
      local localename = locale["code"]

      if ((not is_nedge) or nedge_supported_locales[localename]) and lookupLocale(localename) then
         available_locales[#available_locales + 1] = locale
      end
   end

   locales_initialized = true
end

-- ##############################################

function locales.getAvailableLocales()
   initLocales()

   return available_locales
end

-- ##############################################

function locales.readDefaultLocale()
  local data = loadfile_to_data(default_locale_path)

  return(data)
end

-- ##############################################

-- Locales lazy loading: only load them when a script calls i18n(...)
local orig_i18n_metatable = getmetatable(i18n)
local i18n_mt = {}

-- Replace the i18n(...) invocation
function i18n_mt.__call(...)
   -- ensure that the loacales are loaded
   initLocales()

   -- call the original i18n function
   return(orig_i18n_metatable.__call(...))
end

setmetatable(i18n, i18n_mt)

-- ##############################################

return locales