How to add translations from the server that show up on the client


  • Global Moderator

    Okay, so I have some json files that are in the following structure:

    • translations/
      • en_US/
        • calendar.json
        • calendar_admin.json
      • en_GB/
        • calendar.json
        • calendar_admin.json

    I have this code, which scans the translations folder and adds each file under each folder to the translator:

    var rootDir = __dirname+"/public/translations", filePath, dirs = [], lang;
    async.waterfall([
      async.apply(fs.readdir, rootDir),
      function(files, next){
        async.each(files, function(file, nxt){
          if(file[0] !== '.'){
            filePath = rootDir + '/' + file;
            fs.stat(filePath, function(err, stat){
              if(err){
                return nxt(err);
              }
              if(stat.isDirectory()){
                dirs.push(file);
              }
              nxt();
            });
          }
        }, function(err){
          console.log("ln564: ", dirs);
          next(err, dirs);
        });
      },
      function(dirs, next){
        console.log("ln569: ", dirs);
        async.each(dirs, function(dir, nxt){
          fs.readdir(rootDir + '/' + dir, function(err, files){
            if(err){
              return nxt(err);
            }
            files.forEach(function(file){
              if(file[0] !== '.'){
                filePath = rootDir + '/' + dir + '/' + file;
                console.log("ln579: ", dir);
                translator.addTranslation(dir, file.replace(".json", ""), require(filePath));
              }
            });
            nxt();
          });
        }, next);
      }
    ], function(err){
      if(err){
        return "whatever";
      }
    
      translator.getTranslations("en_GB", "calendar", function(data){
        console.log("ln590: ", data);
      });
      translator.getTranslations("en_GB", "calendar_admin", function(data){
        console.log("ln593: ", data);
      });
    });
    

    The console prints out the contents of both files. However, if I do the same translator.getTranslations call on the client, it shows nothing.

    What am I doing wrong?

    Looking at the code, this module doesn't appear to be made for use with plugins.

    Also, on the page, when I refresh, it doesn't appear to be translating at all, as the [[global:save_changes]] button isn't translated. But, when the page is loaded via ajaxify, it appears to be trying to translate the calendar stuff, but isn't finding it. The save changes button shows the correct string.


  • Global Moderator

    @psychobunny created the templating system. Maybe he made the translations thingy too?


  • Admin

    Yep, he wrote both, although I am positive translation strings should work. They worked for the Rocket theme that was in-progress, so I can't imagine plugins wouldn't have the same benefit...

    Check this line out: https://github.com/NodeBB/nodebb-theme-rocket/blob/master/plugin.json#L24

    Add that, and then mkdir -p languages/en_GB, add your language files there?


  • Global Moderator

    @julian okay, that worked for ajaxify, but not for a direct load. Do I need to include some sort of middleware to translate it?

    btw, this is an ACP plugin page

    The templating system is working, just not translations.


  • Global Moderator

    @julian @psychobunny

    Still haven't fixed this. I think the problem is that the translator is not called by middleware.admin.buildHeader which is a problem.


  • Global Moderator

    Fixed it in this commit


  • Admin

    Best commit message ever 😆

    Thanks @pitaj!


 

| |

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.