dolibarr 23.0.3
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 = 'ExperimentalUx';
44$experimentName = 'UxDolibarrContextLangsTool';
45
46$experimentAssetsPath = $documentation->baseUrl . '/experimental/experiments/dolibarr-context/assets/';
47$js = [
48 '/includes/ace/src/ace.js',
49 '/includes/ace/src/ext-statusbar.js',
50 '/includes/ace/src/ext-language_tools.js',
51 $experimentAssetsPath . '/dolibarr-context.umd.js',
52 $experimentAssetsPath . '/dolibarr-tool.langs.js',
53 $experimentAssetsPath . '/dolibarr-tool.seteventmessage.js',
54];
55$css = [];
56
57// Output html head + body - Param is Title
58$documentation->docHeader($langs->trans($experimentName, $group), $js, $css);
59
60// Set view for menu and breadcrumb
61$documentation->view = [$group, 'UxDolibarrContext', $experimentName];
62
63// Output sidebar
64$documentation->showSidebar(); ?>
65<script>
66 Dolibarr.setContextVars(<?php print json_encode([
67 'DOL_VERSION' => DOL_VERSION,
68 'MAIN_LANG_DEFAULT' => $langs->getDefaultLang(),
69 'DOL_LANG_INTERFACE_URL' => dol_buildpath('admin/tools/ui/experimental/experiments/dolibarr-context/langs-tool-interface.php', 1),
70 ]) ?>);
71</script>
72<div class="doc-wrapper">
73
74 <?php $documentation->showBreadCrumb(); ?>
75
76 <div class="doc-content-wrapper">
77
78 <h1 class="documentation-title"><?php echo $langs->trans($experimentName); ?></h1>
79
80 <?php $documentation->showSummary(); ?>
81
82 <div class="documentation-section">
83 <h2 id="titlesection-basicusage" class="documentation-title">Introduction</h2>
84
85 <p>
86 The Dolibarr Context Langs Tool is a powerful JavaScript utility to manage translations and locales dynamically.<br/>
87 It allows you to load translation domains, set the current language, clear cache, and retrieve translated strings in your scripts.
88 </p>
89
90 </div>
91
92 <div class="documentation-section">
93 <h2 id="titlesection-setup-contextvars" class="documentation-title">Setup Context Variables</h2>
94 <p>
95 Before using the tool, you should declare the necessary context variables on your page.<br/>
96 These variables allow the tool to know the current Dolibarr version, the default language, and the interface URL used to fetch translations.
97 </p>
98 <p>
99 However, like the setEventMessage tool, the Langs tool is a core tool and is always loaded by Dolibarr.<br/>
100 Therefore, in most cases, you do not need to set these variables manually, as they are already defined.
101 </p>
102 <div class="documentation-example">
103
104 <?php
105 $lines = array(
106 '<script nonce="'.getNonce().'" >',
107 'Dolibarr.setContextVars(<?php print json_encode([',
108 ' \'DOL_VERSION\' => DOL_VERSION,',
109 ' \'MAIN_LANG_DEFAULT\' => $langs->getDefaultLang(),',
110 ' \'DOL_LANG_INTERFACE_URL\' => dol_buildpath(\'admin/tools/ui/experimental/experiments/dolibarr-context/langs-tool-interface.php\',1),',
111 ']) ?>);',
112 '</script>',
113 );
114 echo $documentation->showCode($lines, 'php');
115 ?>
116 </div>
117 </div>
118
119 <div class="documentation-section">
120 <h2 id="titlesection-basic-usage" class="documentation-title">Basic Usage</h2>
121 <p>
122 The main features of the Langs tool are:
123 </p>
124 <ul>
125 <li>Load translations for a specific domain with caching</li>
126 <li>Set or change the current locale</li>
127 <li>Clear cached translations</li>
128 <li>Retrieve a translated string by key</li>
129 </ul>
130
131 <p>Example:</p>
132
133 <div class="documentation-example">
134 <?php
135 $lines = array(
136 '<script nonce="'.getNonce().'" >',
137 'document.addEventListener(\'Dolibarr:Ready\', async function(e) {',
138 '',
139 ' if(Dolibarr.checkToolExist(\'langs\')){ // not mandatory because langs tool will be a core tool',
140 '',
141 ' // Load langs',
142 ' Dolibarr.tools.langs.load(\'uxdocumentation\'); // will use cache but need to load lang in new local',
143 '',
144 ' // Clear cache',
145 ' document.getElementById(\'clearCache\').addEventListener(\'click\', async function(e) {',
146 ' await Dolibarr.tools.langs.clearCache();',
147 ' const txt = Dolibarr.tools.langs.trans(\'CacheCleared\');',
148 ' Dolibarr.tools.setEventMessage(txt);',
149 ' });',
150 '',
151 ' // SET lang in fr_FR',
152 ' document.getElementById(\'setlangFr\').addEventListener(\'click\', async function(e) {',
153 ' await Dolibarr.tools.langs.setLocale(\'fr_FR\');',
154 ' const txt = Dolibarr.tools.langs.trans(\'LangsLocalChangedTo\', \'fr_FR\');',
155 ' Dolibarr.tools.setEventMessage(txt);',
156 ' });',
157 '',
158 ' // SET lang in en_US',
159 ' document.getElementById(\'setlangEn\').addEventListener(\'click\', async function(e) {',
160 ' await Dolibarr.tools.langs.setLocale(\'en_US\');',
161 ' const txt = Dolibarr.tools.langs.trans(\'LangsLocalChangedTo\', \'en_US\');',
162 ' Dolibarr.tools.setEventMessage(txt);',
163 ' });',
164 '',
165 ' // pop a message in current lang',
166 ' document.getElementById(\'popmessage\').addEventListener(\'click\', async function(e) {',
167 ' const txt = Dolibarr.tools.langs.trans(\'ContextLangToolTest\');',
168 ' Dolibarr.tools.setEventMessage(txt);',
169 ' });',
170 ' }',
171 '});',
172 '</script>',
173 );
174 echo $documentation->showCode($lines, 'php');
175
176 print implode("\n", $lines);
177 ?>
178 <p>1. Set the current lang</p>
179 <div >
180 <button id="setlangFr" class="button">Set lang in french</button>
181 <button id="setlangEn" class="button">Set lang in english</button>
182 <button id="clearCache" class="button">Clear cache</button>
183 </div>
184
185 <p>2. Pop translated message</p>
186 <div>
187 <button id="popmessage" class="button">pop message</button>
188 </div>
189 </div>
190
191 </div>
192
193
194
195
196 </div>
197
198
199
200
201</div>
202
203</div>
204<?php
205// Output close body + html
206$documentation->docFooter();
207?>
Class Context.
Class to manage UI documentation.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getNonce()
Return a random string to be used as a nonce value for js.
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.