I would like to suggest a small enhancement in App.tsx to ensure the <html>lang attribute updates dynamically whenever the language changes.
Here is my two cents:
I have a question about this function: getCurrentLang(). You put it into src/i18n.ts file but never implemented it. So how can I save user's choice of selected language? So user won't need to change language from default each time page reloads/switches to another?
export const setStorage = (key: string, value: any[] | string, ttl: number) => {
const now = new Date();
// `item` is an object which contains the original value
// as well as the time when it's supposed to expire
const item = {
value: value?.length > 0 ? value : [],
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
};
export const getStorage = (key: string) => {
const itemStr = localStorage.getItem(key);
if (!itemStr) {
return [];
}
const item = JSON.parse(itemStr);
const now = new Date();
// compare the expiry time of the item with the current time
if (now.getTime() > item.expiry) {
// If the item is expired, delete the item from storage
localStorage.removeItem(key);
return [];
}
return item.value;
};
Hi everyone, I have a problem here
All I found in this document is static and hardcode. I mean when we want to translate something english to arabic for example we have to define it in a json file.
Now I have a problem with my admin that he wanna upload new post or even update previous posts that I didn't define these new word in my json file.
To solve this problem what do you suggest?
You could make a simple cms app thats stores the translations and serves them through an endpoint (or serve the files directly) or just serve the files from a github page and give write access to your admin. repo: github.com/vaxevanis/i18nTranslations, translation file: vaxevanis.github.io/i18nTranslatio.... I hope that helps
Hey! I am a co-founder of an agency that develops Web3 projects. Also my favourite hobby is frontend development, which I have been doing for more than 5 years.
hi , thank you a lot for your post , i have a suggestion for changing layout
you can use document.body.dir = i18n.dir() to change the direction of layout.
Exactly what i need right now, thanks alot!!
this post is just what I need! thanks!
Thanks a lot for the tutorial! 🙌
I would like to suggest a small enhancement in
App.tsxto ensure the<html>langattribute updates dynamically whenever the language changes.Here is my two cents:
Great article, thanks for sharing!
I have a question about this function:
getCurrentLang(). You put it intosrc/i18n.tsfile but never implemented it. So how can I save user's choice of selected language? So user won't need to change language from default each time page reloads/switches to another?it would be nice to add the selected language to the local storage. Something like this perhaps:
// import from utils
Hi everyone, I have a problem here
All I found in this document is static and hardcode. I mean when we want to translate something english to arabic for example we have to define it in a json file.
Now I have a problem with my admin that he wanna upload new post or even update previous posts that I didn't define these new word in my json file.
To solve this problem what do you suggest?
You could make a simple cms app thats stores the translations and serves them through an endpoint (or serve the files directly) or just serve the files from a github page and give write access to your admin. repo: github.com/vaxevanis/i18nTranslations, translation file: vaxevanis.github.io/i18nTranslatio.... I hope that helps
Thank you for the comprehensive tutorial!!!
Amazing way to descripe the steps, really helps a lot 🙏
I just have a small enhancement to make the host dynamically fetched.
It makes the loadPath more flexible. Thanks
Great, thanks. Just change 18n to i18n.use(initReactI18next)...
hi , thank you a lot for your post , i have a suggestion for changing layout
you can use document.body.dir = i18n.dir() to change the direction of layout.
This is fantastic. Would be even better if you includes adding the language path to the URL