0

I search a method to make my Multi Language system with MySQL or File. I need to make it in JavaScript, I thought with an object and langue name:

var lang = 
{
    fr:
    {
        welcome     : 'bonjour',
        good_bye    : 'à bientôt'
    },
    en:
    {
        welcome     : 'welcome',
        good_bye    : 'good bye'
    },
    es:
    {
        welcome     : 'holà',
        good_bye    : 'adios'
    }
}

Do you have an better idea?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
JonasGame
  • 25
  • 8

2 Answers2

1

have a look at http://i18next.com/ - quite a handy library, built in string replacement etc.

Joe Fitter
  • 1,309
  • 7
  • 11
1

A simple way to interrogate that JS object would be to write a simple function using the country and word as arguments:

function translate(country, word) {
   return lang[country][word]
}

And then use it like this, perhaps:

var name = 'Jonas';
var country = 'es';
translate(country, 'welcome') + ' ' + name; // "holà Jonas"

DEMO

UPDATE:

function doWelcome(country, name) {
   return lang[country].welcome + ', ' + name + '. Good game.';
}

And then use it like this, perhaps:

doWelcome('es', 'Jonas'); // "holà, Jonas. Good game"

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • Yeah. I know that, now I'm trying to make a system with args. – JonasGame Feb 23 '15 at 10:46
  • I don't know what a "system with args" is, sorry. – Andy Feb 23 '15 at 10:47
  • translate(country, 'welcome') + ' ' + name; // "holà Jonas" WORKS but, if it's "Welcome Jonas, good game", with your system I need to make 2 str: "Welcome", and "good game". Or I can to do: "Welcome %s, good game!". – JonasGame Feb 23 '15 at 10:50
  • I found https://gist.github.com/tbranyen/1049426, I'll try to understand it correctly but works perfectly. – JonasGame Feb 23 '15 at 11:59
  • Yes but I know this method, I'm asking if... I have my result: https://gist.github.com/tbranyen/1049426 – JonasGame Feb 23 '15 at 12:18
  • I think the best if just {p} like a "Params" or other "text" to inform it's a param. I don't think I need {1}, {2}, because rarely use many time the same arg. Just "{a} help {a}".format('Andy', 'Jonas'), no? – JonasGame Feb 23 '15 at 12:35
  • Your code return "Welcome Jonas, how are you? (It's Shaiming who talk to {0})" – JonasGame Feb 23 '15 at 12:37
  • That's not the way I would do it, no. and my code returns "Welcome Jonas, how are you? (It's Shaiming who talk to Jonas)" – Andy Feb 23 '15 at 12:39
  • Yes but I don't need to use {1}, {2}, etc. just argument: 'text number: {a} | other: {a} for example – JonasGame Feb 23 '15 at 14:46
  • But {a} is a example, I can use something else. – JonasGame Feb 23 '15 at 15:37
  • @JonasGame: Andy appears to have made a great effort on your behalf, even if was not what you wanted. Would you consider upvoting his answer, to thank him for his time? – halfer Apr 02 '15 at 19:43
  • @helfer: Sorry I'm new here and I forgotten, i'm going right now - "votes up require 15 reputation" – JonasGame Apr 02 '15 at 19:50