Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings stored in constant values do not change on different language selection #118

Open
ayush-shta opened this issue Apr 7, 2021 · 7 comments

Comments

@ayush-shta
Copy link

I have some stings that are stored in a separate constant files.

I am able to set and see those strings on my page when the language is set to initial value. However, after I update the selected language using: strings.setLanguage(language), the strings stored in the constants file are not changed for that respective language.

Do I have to do anything specific to get the behavior I want, or is it not possible with this library?

@stefalda
Copy link
Owner

stefalda commented Apr 7, 2021

Hi, you should provide some code to test your specific situation, otherwise I'm sorry but I'm unable to provide you with an answer...

@ayush-shta
Copy link
Author

ayush-shta commented Apr 7, 2021

Hello @stefalda ,

For example,

My util file for localization is like this:
lang.js

export const strings = new LocalizedStrings({
  en: english,
  fr: french,
  it: italian,
});

export function getString(key, ...args) {
  const str = strings.formatString(strings[key], ...args);
  return str;
}

export function updateLanguage(language) {
  strings.setLanguage(language);
}

I have a constant file like this:
pages.js

import { getString } from '../util/lang';

export const PageNames = {
  Home: getString('home'),
  Info: getString('info'),
  SelectTiming: getString('selectTiming'),
};

It's corresponding language file for English is something like this:
en.js

const language = {
  // PageNames
  home: 'Home,
  info: 'Info',
  selectTiming: 'Select Timing',
 ....
}

Similarly its French file for language is this:
fr.js

const language = {
  // PageNames
  home: 'fr Home fr,
  info: 'fr Info fr',
  selectTiming: 'fr Select Timing fr',
 ....
}

I use the pageName constant in my react component file as:

import { PageNames } from '../constants/pages';
...
<NavTab
    .....
    label={PageNames.Home}
      />

When I change my language from english to french using strings.setLanguage(language), the strings stored in the constants file do not reflect the change in the app for that respective language. It still shows the default language english.

However, the strings directly used in the react component files do work properly as intended.

@stefalda
Copy link
Owner

stefalda commented Apr 7, 2021

Have you tried to do setState ()?

@ayush-shta
Copy link
Author

Where do I need to setState to? I do not want to set my components to initial state. Also, I am tracking the state of a selected language

@stefalda
Copy link
Owner

stefalda commented Apr 7, 2021

You can just set a state without changing anything just to produce a redraw

@ayush-shta
Copy link
Author

Force redraw doesn't work for me. For now as a workaround, I'm setting the strings as functions and calling them as functions at the place of use. This does solve the problem but does not look optimal.

pages.js

import { getString } from '../util/lang';

export const PageNames = {
  Home: () => getString('home'),
  Info: () =>getString('info'),
  SelectTiming: ()=> getString('selectTiming'),
};

my component file

import { PageNames } from '../constants/pages';
...
<NavTab
    .....
    label={PageNames.Home()}
      />

@peterLam2
Copy link

hello,ayush-shta , I have the similar issue , had you figure this out or using the same approach as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants