Answer the question
In order to leave comments, you need to log in
Where and how to store language phrases for javascript in Bitrix?
Hello.
As far as I understand, when creating modules for Bitrix, language files lang/*/*.php must be stored in cp1251 encoding, and the rest in UTF.
But what about js files that contain language phrases?
If the site is in UTF and the file is in UTF, then there are no problems.
But if the site is in cp1251 and the file is in UTF, then everything is bad.
Does Bitrix have a mechanism for storing and loading language languages at once into a script?
Answer the question
In order to leave comments, you need to log in
See how it is implemented in standard components, BX.message is used for language phrases in js
One way or another, the task is to transfer the phrase to the JavaScript in the correct encoding.
This is either the inclusion of */lang_id/*.js file, within which linguistic phrases are defined as the value of variables, or inline initialization of variables with linguistic phrases broadcast via php.
... or loading via Ajax, but even there it comes down to one of two things.
File connection: prepare a file and connect it, for example, with the following code:
$doc_root= \Bitrix\Main\Application::getDocumentRoot();
$js= __DIR__ . '/lang/'.LANGUAGE_ID.'/js_lang.js';
\Bitrix\Main\Page\Asset::getInstance()
->addJs( $js . '?x=' . md5( filemtime( $doc_root . $js ) ), true );
<script ...>
lang_me= {
'MENU_TITLE' : 'php-код'
...
}
</script>
Everything turned out to be much easier.
We register scripts via CJSCore::RegisterExt, where you can also specify the localization file
I do this in the template:
<script>
BX.message({
'BTN_MESSAGE_BASKET_REDIRECT': '<?=GetMessageJS("BTN_MESSAGE_BASKET_REDIRECT")?>',
'BTN_MESSAGE_CLOSE_POPUP': '<?=GetMessageJS("BTN_MESSAGE_CLOSE_POPUP")?>',
'CART_CAPTION_ADD': '<?=GetMessageJS("CART_CAPTION_ADD")?>',
'CART_ADDED_TEMPLATE': '<?=GetMessageJS("CART_ADDED_TEMPLATE")?>'
});
console.log(BX.message("CART_CAPTION_ADD"));
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question