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

Is there a way to get t function outside functional component, or without hook ? #1437

Closed
kristijorgji opened this issue Sep 27, 2021 · 14 comments

Comments

@kristijorgji
Copy link

Hi,
I was looking for a way to get t function outside functional component context or without using hook
(as was described here #983) but at the moment could not find such.

I have a utils file utils.ts, and export there a dialog function, and need to add t func there to translate from common namespace

export const salert = new Alert(Swal, // need T function here);

How can I get t fn there

I tried this

import { i18n } from 'next-i18next';
i18n?.t as TFunction

but did not work

@isaachinman
Copy link
Contributor

but did not work

Can you explain more?

@kristijorgji
Copy link
Author

Yes I can explain

  1. I tested useTranslation hook, not inside a functional component but just in a normal ts file and got error "cannot use hook outside functional component context"
  2. I tested
import { i18n } from 'next-i18next';
i18n?.t as TFunction 

and i18n.t was undefined

  1. How can I fetch the t function from a normal ts/js file that is not using a react component ?

I have a normal ts file that needs to translate some strings.
This is imported by a next application, that is using of course appWithTranslations

@isaachinman
Copy link
Contributor

Check the value of i18n. It will be null until the global i18n instance initialises. You can only call i18n.t after your app has initialised.

@kristijorgji
Copy link
Author

kristijorgji commented Sep 27, 2021

Why is the app not initializing then to create i18n, app is the first thing running, then importing this utility file....

Will check once more, but flow is like this, a page is importing the util file, and the page is imported by _app.tsx so i18n should exist at that time

@kristijorgji
Copy link
Author

kristijorgji commented Oct 10, 2021

Check the value of i18n. It will be null until the global i18n instance initialises. You can only call i18n.t after your app has initialised.

I checked sometimes i18n is not null other times null.

Is there a way to listen for i18n status ? maybe by registering a callback event or something like that

I traced how my util file is called, which tries to access t function and looks ok



./src/c/single/index.ts | @ | _app.js?ts=1633892107712:797
-- | -- | --
  | options.factory | @ | webpack.js?ts=1633892107712:685
  | __webpack_require__ | @ | webpack.js?ts=1633892107712:37
  | fn | @ | webpack.js?ts=1633892107712:354
  | eval | @ | Header.tsx:12
  | ./src/c/components/organisms/Header/Header.tsx

So first below is _app.ts called, then Header component then this one is including single/index.ts which exports one alert function, like this export const alert = new AlertManager(i18n.t);

At this moment i18n is null, then after some seconds is not null anymore

@isaachinman
Copy link
Contributor

#918 (comment)

@bryanprimus
Copy link

still can't get this work done

@Spell28
Copy link

Spell28 commented Mar 17, 2022

@bryantobing12
I didn't understand how to do it the first time either, it ended up like this:

import { i18n } from 'next-i18next'; // Import
i18n?.init() // Initialize i18n
i18n?.t('translate_key') // get something from your translation json file using current Language

@mickdewald
Copy link

@bryantobing12 I didn't understand how to do it the first time either, it ended up like this:

import { i18n } from 'next-i18next'; // Import
i18n?.init() // Initialize i18n
i18n?.t('translate_key') // get something from your translation json file using current Language

This does not work.
i18n is always null

@AljazGornik
Copy link

@micktg did you manage to resolve this? we are facing the same issue

@mickdewald
Copy link

@micktg did you manage to resolve this? we are facing the same issue

I used https://github.com/JCQuintas/ni18n for this.

@asilliahmet
Copy link

for future reference, here is how i fixed it with async.
just await and use it: const t = await t_hook

`import i18next from "i18next";

const t_hook = (async ()=>{
if (i18next.isInitialized) {
return i18next.t;
}
await i18next.init();
return i18next.t;
})();

export default t_hook;`

@zjy365
Copy link

zjy365 commented Apr 28, 2024

I think we should reopen this issue. It seems that there is no good way to solve this problem.

@illicit-oblivion
Copy link

illicit-oblivion commented Jun 19, 2024

@micktg did you manage to resolve this? we are facing the same issue

I used https://github.com/JCQuintas/ni18n for this.

can you show how to use this particular library outside the component?

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

9 participants