-2

I am getting JSON fro backend, which I need to parse on UI. For all the keys from the JSON, I have to translate them and show on UI.

Eg: i18n.t('key') will give me translated value.

But for some keys like 'name', 'date' Eg: i18n.t('name') translation is giving following output "key 'translation:name (en-US)' returned a object instead of string."

Could you please help me how to deal with this scenerio.

aishwarya
  • 89
  • 1
  • 7
  • Please share a code sample. – Murat Gündeş Mar 23 '17 at 07:17
  • Handlebar helper: Handlebars.registerHelper('t', function() { var params = getParamsFromArgs(arguments).join(''); var result = params && i18n.t(params) || ''; return new Handlebars.SafeString(result); }); template in html:
    {{t 'name'}}
    or in Jquery $.t('name') its giving me error instead of replace values from messages.property
    – aishwarya Mar 23 '17 at 08:21
  • try i18n.t('key', { returnObjects: true }); and you will see name returns an object and not a string (per default without returnObjects option i18next allows only string values as valid translations) – jamuhl May 10 '17 at 11:03

1 Answers1

-1

If you have for example following JSON from your service


    {
       "i18n": {
          "name": "translation1",
          "name2": "translation2"
        }
    }

You can just use it as following


    var mytranslation = getTranslationsFromService();

    console.log(mytranslation.i18n.name)    //result: translation1
    console.log(mytranslation.i18n.name2)    //result: translation2

    var getTranslationsFromService = function() {
       //Get result from service, where the result looks like the JSON above.
    }

I hope I could help.

Kind regards.

Torben
  • 438
  • 1
  • 7
  • 22