webmcp
view framework/env/__init.lua @ 324:1c3ba14bd679
Root __init.lua function must not set global variables without _G now; Documentation for "_" function added
author | jbe |
---|---|
date | Mon Mar 23 22:41:40 2015 +0100 (2015-03-23) |
parents | 1fbdccf4f8e9 |
children | 3384306aa7bc |
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 internationalization. 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 --]]--
16 function _G._(text, replacements)
17 local text = locale._get_translation_table()[text] or text
18 if replacements then
19 return (
20 string.gsub(
21 text,
22 "#{(.-)}",
23 function (placeholder)
24 return replacements[placeholder]
25 end
26 )
27 )
28 else
29 return text
30 end
31 end
32 --//--
34 --[[--
35 cloned_table = -- newly generated table
36 table.new(
37 table_or_nil -- keys of a given table will be copied to the new table
38 )
40 If a table is given, then a cloned table is returned.
41 If nil is given, then a new empty table is returned.
43 --]]--
44 function table.new(tbl)
45 local new_tbl = {}
46 if tbl then
47 for key, value in pairs(tbl) do
48 new_tbl[key] = value
49 end
50 end
51 return new_tbl
52 end
53 --//--
55 -- load libraries (except "multirand", which must be loaded after forking)
56 _G.extos = require 'extos'
57 _G.nihil = require 'nihil'
58 _G.mondelefant = require 'mondelefant'
59 _G.atom = require 'atom'
60 _G.json = require 'json'
61 require 'mondelefant_atom_connector'
62 -- NOTE: "multirand" library is loaded in mcp.lua after forking
64 -- setup mondelefant
65 mondelefant.connection_prototype.error_objects = true
66 function mondelefant.connection_prototype:sql_tracer(command)
67 if trace.is_disabled() then
68 return
69 end
70 local start_time = extos.monotonic_hires_time()
71 return function(error_info)
72 trace.sql{
73 command = command,
74 execution_time = extos.monotonic_hires_time() - start_time,
75 error_position = error_info and error_info.position or nil
76 }
77 end
78 end
80 --[[--
81 config -- table to store application configuration
83 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
84 --]]--
85 _G.config = {}
86 --//--