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

Update LanguageDetectorAsyncModule #1879

Closed
wants to merge 1 commit into from

Conversation

daomtthuan
Copy link

Add return type Promise<void> for LanguageDetectorAsyncModule

Add return type `Promise<void>` for `LanguageDetectorAsyncModule`
@coveralls
Copy link

Coverage Status

Coverage remained the same at 91.436% when pulling 615194b on daomtthuan:master into 913190d on i18next:master.

index.d.ts Show resolved Hide resolved
index.d.ts Show resolved Hide resolved
index.d.ts Show resolved Hide resolved
@adrai adrai closed this Dec 10, 2022
@daomtthuan
Copy link
Author

daomtthuan commented Dec 10, 2022

@adrai
Why do init, detect and cacheUserLanguage not return promises in the module LanguageDetectorAsyncModule,
although it is async: true?

In this case
When use LanguageDetector with AsyncStorage

  import AsyncStorage from '@react-native-async-storage/async-storage'

  const LANGUAGE_KEY = 'i18n.language';

  const languageDetector: LanguageDetectorAsyncModule = {
    type: 'languageDetector',
    async: true,
    init: () => {
      // Do nothing.
    },
    detect: async (setLanguage) => {
      try {
        const language = await AsyncStorage.getItem(LANGUAGE_KEY);

        setLanguage(language ?? 'en');
      } catch (exception) {
        console.error(exception);
      }
    },
    cacheUserLanguage: async (language) => {
      try {
        await AsyncStorage.setItem(LANGUAGE_KEY, language);
      } catch (exception) {
        console.error(exception);
      }
    },
  };

because use async/await, LanguageDetector is LanguageDetectorAsyncModule, not LanguageDetectorModule.
That is the reason I add return type promise for LanguageDetectorAsyncModule

@adrai
Copy link
Member

adrai commented Dec 10, 2022

Because the interface looks like this: https://www.i18next.com/misc/creating-own-plugins#languagedetector

@daomtthuan
Copy link
Author

daomtthuan commented Dec 10, 2022

@adrai
So, can not use async/await for detect and cacheUserLanguage?
I know it can be like this:

  import AsyncStorage from '@react-native-async-storage/async-storage'

  const LANGUAGE_KEY = 'i18n.language';

  const languageDetector: LanguageDetectorAsyncModule = {
    type: 'languageDetector',
    async: true,
    init: () => {
      // Do nothing.
    },
    detect: (setLanguage) => {
      AsyncStorage.getItem(LANGUAGE_KEY)
        .then((language) => setLanguage(language ?? 'en'))
        .catch((exception) => console.error(exception));
    },
    cacheUserLanguage: (language) => {
      AsyncStorage.setItem(LANGUAGE_KEY, language).catch((exception) => console.error(exception));
    },
  };

I don't like to use then/catch with promise
Do you have any ideas for this?

@adrai
Copy link
Member

adrai commented Dec 10, 2022

This is sufficient:

import AsyncStorage from '@react-native-async-storage/async-storage'

  const LANGUAGE_KEY = 'i18n.language';

  const languageDetector: LanguageDetectorAsyncModule = {
    type: 'languageDetector',
    async: true,
    init: () => {},
    detect: (setLanguage) => {
      AsyncStorage.getItem(LANGUAGE_KEY)
        .then((language) => setLanguage(language ?? 'en'));
    },
    cacheUserLanguage: (language) => {
      AsyncStorage.setItem(LANGUAGE_KEY, language)
    }
  };

That's how the interface of i18next is defined since the beginning...

I'll check later, if at least we can accept a promise for the detect function.... will let you know.

@adrai
Copy link
Member

adrai commented Dec 10, 2022

ok, with v22.3.0 you can now do this:

  import AsyncStorage from '@react-native-async-storage/async-storage'

  const LANGUAGE_KEY = 'i18n.language';

  const languageDetector: LanguageDetectorAsyncModule = {
    type: 'languageDetector',
    async: true,
    detect: () => AsyncStorage.getItem(LANGUAGE_KEY),
    cacheUserLanguage: (language) => AsyncStorage.setItem(LANGUAGE_KEY, language)
  };

also updated the docs:
image

@daomtthuan
Copy link
Author

Thank you <3

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

Successfully merging this pull request may close these issues.

None yet

3 participants