V
V
Vladislav2015-10-15 18:45:00
PHP
Vladislav, 2015-10-15 18:45:00

How to do localization through js/json/php files?

There is a block on the site, for example, with id="example"
in the file lang-ru.js/php/json there is a line example='Hi';
in the file lang-en.js/php/json there is a line example='Hello';
in language files, localization should be written in this form,
and in the HTML itself these blocks are empty, how to make them load words depending on the selected language
(such an example uses VK vk.com/js/lang0_0.js?6672)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Osher, 2015-10-15
@miraage

Suppose we have localization somewhere.
Then you need to put directives on all elements
I don't know what JS tools you use - let's make an example with jQuery.

$(function() {
  var file = '/locale/' + App.lang + '.json';
  
  // Тут предположим, что response = есть содержимое файла
  $.get(file).then(function (response) {
    // Идем по всем файлам и заменяем контент
    $('[data-translate]').each(function () {
      var $elem = $(this),
          locKey = $elem.data('translate');
        
      // Если ключ найден  
      if (response[locKey]) {
        $elem.html(response[locKey]);
      }
    });
  });
});

I
IceJOKER, 2015-10-15
@IceJOKER

What's difficult?
Upload the json file, then go through each element of the json file and change it on the go, something like this:
better not id, but use data-lang="example" , or like on - developer.android.com/intl/ru/training/ index.html

X
xmoonlight, 2015-10-15
@xmoonlight

Language (code) - take it from the browser header Accept-Language or from the session (if the user has changed) and immediately load the desired file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question