webmcp

view framework/env/__init.lua @ 498:e360b1933c78

Improve efficiency of table.insert in case of Lua 5.3 (do not use compatibility wrapper)
author jbe
date Sun Jul 23 03:43:49 2017 +0200 (2017-07-23)
parents 7eec5b604b00
children
line source
1 --[[--
2 translated_string = -- translated string
3 _(
4 string_to_translate, -- string to translate
5 {
6 placeholder_name1 = text1, -- replace all occurrences of "#{placeholder_name1}" with the string text1
7 placeholder_name2 = text2, -- replace all occurrences of "#{placeholder_name2}" with the string text2
8 ...
9 }
10 )
12 Translation function for localization. The "_" function translates a given string to the currently selected language (see locale.set{...}). If the translated string contains placeholders in the form #{name}, then those placeholders may be automatically replaced with a corresponding substring which is taken from the table passed as optional second argument.
14 Hint: Lua's syntax rules allow to write _"text" as a shortcut for _("text"), or _'text' instead of _('text') respectivly.
16 --]]--
18 function _G._(text, replacements)
19 local text = locale._get_translation_table()[text] or text
20 if replacements then
21 return (
22 string.gsub(
23 text,
24 "#{(.-)}",
25 function (placeholder)
26 return replacements[placeholder]
27 end
28 )
29 )
30 else
31 return text
32 end
33 end
34 --//--
36 --[[--
37 cloned_table = -- newly generated table
38 table.new(
39 table_or_nil -- keys of a given table will be copied to the new table
40 )
42 If a table is given, then a cloned table is returned.
43 If nil is given, then a new empty table is returned.
45 --]]--
46 function table.new(tbl)
47 local new_tbl = {}
48 if tbl then
49 for key, value in pairs(tbl) do
50 new_tbl[key] = value
51 end
52 end
53 return new_tbl
54 end
55 --//--
57 --[[--
58 table.insert(
59 t, -- table
60 index, -- optional index
61 value -- value
62 )
64 Custom implementation of Lua's table.insert(...) where table.insert(table, value) also respects metamethods in Lua 5.2 (this behavior is already supported by Lua 5.3).
66 --]]--
67 if _VERSION == "Lua 5.2" then
68 local old_insert = table.insert
69 function table.insert(...)
70 if select("#", ...) == 2 then
71 local t, value = ...
72 t[#t+1] = value
73 return
74 end
75 return old_insert(...)
76 end
77 end
78 --//--
80 -- load libraries
81 -- (except "extos", which has to be loaded earlier, and "multirand", which must be loaded after forking)
82 _G.nihil = require 'nihil'
83 _G.mondelefant = require 'mondelefant'
84 _G.atom = require 'atom'
85 _G.json = require 'json'
86 require 'mondelefant_atom_connector'
87 -- NOTE: "multirand" library is loaded in mcp.lua after forking
89 -- setup mondelefant
90 mondelefant.connection_prototype.error_objects = true
91 function mondelefant.connection_prototype:sql_tracer(command)
92 if trace.is_disabled() then
93 return
94 end
95 local start_time = extos.monotonic_hires_time()
96 return function(error_info)
97 trace.sql{
98 command = command,
99 execution_time = extos.monotonic_hires_time() - start_time,
100 error_position = error_info and error_info.position or nil
101 }
102 end
103 end
105 --[[--
106 config -- table to store application configuration
108 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
109 --]]--
110 _G.config = {}
111 --//--

Impressum / About Us