dolibarr 24.0.0-beta
langs-tool.php
1<?php
2/*
3 * Copyright (C) 2025 Anthony Damhet <a.damhet@progiseize.fr>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20// Load Dolibarr environment
21require '../../../../main.inc.php';
22
30// Protection if external user
31if ($user->socid > 0) {
33}
34
35// Includes
36require_once DOL_DOCUMENT_ROOT . '/admin/tools/ui/class/documentation.class.php';
37
38// Load documentation translations
39$langs->load('uxdocumentation');
40
41//
42$documentation = new Documentation($db);
43$group = 'UxDolibarrContext';
44$experimentName = 'UxDolibarrContextLangsTool';
45
46$experimentAssetsPath = dolBuildUrl('/public/includes/dolibarr-js-context');
47$js = [
48 '/includes/ace/src/ace.js',
49 '/includes/ace/src/ext-statusbar.js',
50 '/includes/ace/src/ext-language_tools.js'
51];
52$css = [];
53
54// Output html head + body - Param is Title
55$documentation->docHeader($langs->trans($experimentName, $group), $js, $css);
56
57// Set view for menu and breadcrumb
58$documentation->view = [$group, 'UxDolibarrContext', $experimentName];
59
60// Output sidebar
61$documentation->showSidebar(); ?>
62
63<div class="doc-wrapper">
64
65 <?php $documentation->showBreadCrumb(); ?>
66
67 <div class="doc-content-wrapper">
68
69 <h1 class="documentation-title"><?php echo $langs->trans($experimentName); ?></h1>
70
71 <?php $documentation->showSummary(); ?>
72
73 <div class="documentation-section">
74 <h2 id="titlesection-basicusage" class="documentation-title">Introduction</h2>
75
76 <p>
77 The Dolibarr Context Langs Tool is a powerful JavaScript utility to manage translations and locales dynamically.<br/>
78 It allows you to load translation domains, set the current language, clear cache, and retrieve translated strings in your scripts.
79 </p>
80
81 </div>
82
83 <div class="documentation-section">
84 <h2 id="titlesection-setup-contextvars" class="documentation-title">Setup Context Variables</h2>
85 <p>
86 Before using the tool, you should declare the necessary context variables on your page.<br/>
87 These variables allow the tool to know the current Dolibarr version, the default language, and the interface URL used to fetch translations.
88 </p>
89 <p>
90 However, like the setEventMessage tool, the Langs tool is a core tool and is always loaded by Dolibarr.<br/>
91 Therefore, in most cases, you do not need to set these variables manually, as they are already defined.
92 </p>
93 <div class="documentation-example">
94
95 <?php
96 $lines = array(
97 '<script nonce="'.getNonce().'" >',
98 'Dolibarr.setContextVars(<?php print json_encode([',
99 ' \'DOL_VERSION\' => DOL_VERSION,',
100 ' \'MAIN_LANG_DEFAULT\' => $langs->getDefaultLang(),',
101 ' \'DOL_LANG_INTERFACE_URL\' => dol_buildpath(\'public/langs/langs-tool-interface.php\', 1),',
102 ']) ?>);',
103 '</script>',
104 );
105 $documentation->showCode($lines, 'php');
106 ?>
107 </div>
108 </div>
109
110 <div class="documentation-section">
111 <h2 id="titlesection-basic-usage" class="documentation-title">Basic Usage</h2>
112 <p>
113 The main features of the Langs tool are:
114 </p>
115 <ul>
116 <li>Load translations for a specific domain with caching</li>
117 <li>Set or change the current locale</li>
118 <li>Clear cached translations</li>
119 <li>Retrieve a translated string by key</li>
120 </ul>
121
122 <p>Example:</p>
123
124 <div class="documentation-example">
125 <?php
126 $lines = array(
127 '<script nonce="'.getNonce().'" >',
128 'document.addEventListener(\'Dolibarr:Ready\', async function(e) {',
129 '',
130 ' if(Dolibarr.checkToolExist(\'langs\')){ // not mandatory because langs tool will be a core tool',
131 '',
132 ' // Load langs',
133 ' Dolibarr.tools.langs.load(\'uxdocumentation\'); // will use cache but need to load lang in new local',
134 '',
135 ' // Clear cache',
136 ' document.getElementById(\'clearCache\').addEventListener(\'click\', async function(e) {',
137 ' await Dolibarr.tools.langs.clearCache();',
138 ' const txt = Dolibarr.tools.langs.trans(\'CacheCleared\');',
139 ' Dolibarr.tools.setEventMessage(txt);',
140 ' });',
141 '',
142 ' // SET lang in fr_FR',
143 ' document.getElementById(\'setlangFr\').addEventListener(\'click\', async function(e) {',
144 ' await Dolibarr.tools.langs.setLocale(\'fr_FR\');',
145 ' const txt = Dolibarr.tools.langs.trans(\'LangsLocalChangedTo\', \'fr_FR\');',
146 ' Dolibarr.tools.setEventMessage(txt);',
147 ' });',
148 '',
149 ' // SET lang in en_US',
150 ' document.getElementById(\'setlangEn\').addEventListener(\'click\', async function(e) {',
151 ' await Dolibarr.tools.langs.setLocale(\'en_US\');',
152 ' const txt = Dolibarr.tools.langs.trans(\'LangsLocalChangedTo\', \'en_US\');',
153 ' Dolibarr.tools.setEventMessage(txt);',
154 ' });',
155 '',
156 ' // pop a message in current lang',
157 ' document.getElementById(\'popmessage\').addEventListener(\'click\', async function(e) {',
158 ' const txt = Dolibarr.tools.langs.trans(\'ContextLangToolTest\');',
159 ' Dolibarr.tools.setEventMessage(txt);',
160 ' });',
161 ' }',
162 '});',
163 '</script>',
164 );
165 $documentation->showCode($lines, 'php');
166
167 print implode("\n", $lines);
168 ?>
169 <p>1. Set the current lang</p>
170 <div >
171 <button id="setlangFr" class="button">Set lang in french</button>
172 <button id="setlangEn" class="button">Set lang in english</button>
173 <button id="clearCache" class="button">Clear cache</button>
174 </div>
175
176 <p>2. Pop translated message</p>
177 <div>
178 <button id="popmessage" class="button">pop message</button>
179 </div>
180 </div>
181
182 </div>
183
184
185
186
187 </div>
188
189
190
191
192</div>
193
194</div>
195<?php
196// Output close body + html
197$documentation->docFooter();
198?>
Class Context.
Class to manage UI documentation.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
multi select button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.