2

I have a flat json as follows

'action.skip': 'Skip this for now', 'action.start': 'Start'

I want to call it as

{I18n.t('action.start')}

currently it is not working because i18n trying to find "action" key and its child "start" instead of "action.start". I couldn't find any option to change the key separator. please help

Sebin Simon
  • 1,596
  • 2
  • 17
  • 28

1 Answers1

1

We can change the key seperator by adding following codes.

import I18n from 'i18n-js';

import { de, en } from '../locales';

I18n.defaultLocale = 'en';
I18n.defaultSeparator = '_';

i18n-js now only split the string with "_", so if add "abc.xyz" it will treat as a single key.

Sebin Simon
  • 1,596
  • 2
  • 17
  • 28
  • is it working ? for me it's not working. – Kshitij Feb 11 '21 at 09:28
  • did you used only I18n.defaultSeparator = '_', or you have done anything else also. Because for me defaultSeparator not working. – Kshitij Feb 12 '21 at 10:09
  • I have done only this. which character you using for the split? – Sebin Simon Feb 14 '21 at 07:00
  • I can use any character other than "." which is default, but it's not working for any character. – Kshitij Feb 15 '21 at 04:53
  • I didn't get you... could you please share your code ? – Sebin Simon Feb 15 '21 at 05:23
  • [ import I18n from 'i18n-js'; import EN from "../locales/EN"; import HIN from "../locales/HIN"; I18n.translations = { EN, HIN, }; I18n.defaultLocale = "EN"; // If the current locale in device is not en or hi I18n.locale = "EN"; // If we do not want the framework to use the phone's locale by default I18n.fallbacks = true I18n.missingBehaviour ='guess' I18n.defaultSeparator = "/"] – Kshitij Feb 15 '21 at 05:49
  • 1
    import I18n from 'i18n-js'; import { de, en } from '../locales'; I18n.defaultLocale = 'en'; I18n.defaultSeparator = '|'; I18n.fallbacks = true; I18n.translations = { en, de }; export default I18n; this is my code – Sebin Simon Feb 16 '21 at 04:28
  • 1
    Thanks for your help, I think there is some issue with the plugin so I changed it to I18next, – Kshitij Feb 16 '21 at 07:56