Hemmelig.app icon indicating copy to clipboard operation
Hemmelig.app copied to clipboard

i18n support

Open luismanson opened this issue 1 year ago • 47 comments

Hi, I'm the guy who asked about translation support in Reddit. Would you consider a PR with some support? Frontend maybe.. I think I can add some basic implementation of react-i18n...

luismanson avatar Sep 03 '22 12:09 luismanson

👋

Please do. Hopefully the frontend code is not to spaghetti. I know strings are thrown around all the places. Even with ternary operators.

react-i18n is 👍 to me!

bjarneo avatar Sep 03 '22 17:09 bjarneo

Hey, What do you think? https://github.com/luismanson/Hemmelig.app/commit/563ffbf7cca9c49f56fd3cfdb1f9522cb0a6def8 it's not very complete, but the basic front strings.

luismanson avatar Sep 05 '22 22:09 luismanson

Hey @luismanson, I will look into it today :)

bjarneo avatar Sep 06 '22 06:09 bjarneo

@luismanson added a comment: https://github.com/luismanson/Hemmelig.app/commit/563ffbf7cca9c49f56fd3cfdb1f9522cb0a6def8#commitcomment-83183511

To me it looks great. You do not have to add the loader component for the Suspense, but if you do it would be cool. Feel free to open a PR :)

bjarneo avatar Sep 06 '22 10:09 bjarneo

PR: https://github.com/HemmeligOrg/Hemmelig.app/pull/82

bjarneo avatar Sep 06 '22 18:09 bjarneo

new PR: https://github.com/HemmeligOrg/Hemmelig.app/pull/83

Thank you for your contribution :)

bjarneo avatar Sep 06 '22 20:09 bjarneo

How would I go about adding more languages? I've created a new file public/locales/de/translation.json with the updated strings. Do I need to add this new language in src/i18n.js as well? And does the site itself already have some sort of system language detection or would that still be needed?

bytebone avatar Sep 08 '22 09:09 bytebone

Hello, you need to set de as the default language in src/i18n.js. Language switch/detection has not implemented yet.

PS: @bjarneo I'll submit my translation in the next day or so.

luismanson avatar Sep 08 '22 11:09 luismanson

If i can figure out at what point the strings are loaded, maybe I can whip up a language detection & automatic switching method. Wish me luck 🤞🏼

bytebone avatar Sep 08 '22 12:09 bytebone

check this doc: https://react.i18next.com/latest/using-with-hooks the example has some comments about language detection and file loading

luismanson avatar Sep 08 '22 12:09 luismanson

Reopening this issue.

One thing to consider here is to have an option to set the default language for the instance. This can be solved by adding an env var called SECRET_LANGUAGE. Then add than env var to the client config here: https://github.com/HemmeligOrg/Hemmelig.app/blob/main/config/default.cjs#L63

Which means you can fetch it like this: https://github.com/HemmeligOrg/Hemmelig.app/blob/main/src/client/routes/home/index.js#L51 One important note here is that the config that is imported and used in the frontend, is coming from this file: https://github.com/HemmeligOrg/Hemmelig.app/blob/main/src/client/config.js

bjarneo avatar Sep 08 '22 14:09 bjarneo

Btw, so cool that you guys want to contribute 🎉

bjarneo avatar Sep 08 '22 15:09 bjarneo

I have added you both @luismanson and @RainerZufahl as contributors to this repository. Let me know if something is not right about the settings. Should be allowed to create branches and PRs

bjarneo avatar Sep 08 '22 15:09 bjarneo

Thank you! I've already created a fork of the repo which I'm working in, should I scrap that and create a new branch in this repo?

bytebone avatar Sep 08 '22 16:09 bytebone

@rainerzufahl that is up to you, will work either way

bjarneo avatar Sep 08 '22 16:09 bjarneo

Wow, thanks! I'll test if works with the spanish json later today.

luismanson avatar Sep 08 '22 17:09 luismanson

automatic language detection & switching is now present via #88.

bytebone avatar Sep 09 '22 10:09 bytebone

How should we handle new strings? I think this has to be as transparent as possible. I noticed later that new strings could be written as t('some_string', 'Some String') but there are other options as Runtime Extraction and some plugins.

luismanson avatar Sep 10 '22 20:09 luismanson

Hm. Interesting. I have to look into this.

But, of course, using t('some.string', 'Default string') is a good start, because that means if a language does not have the translation, it will automatically fallback to the default english string?

bjarneo avatar Sep 11 '22 17:09 bjarneo

Yes, it should. If i understand correctly, this would even work with an empty default LANG setting.

luismanson avatar Sep 11 '22 20:09 luismanson

I think that would be the first approach for this. How would runtime extraction and plugins work with create-react-app?

bjarneo avatar Sep 12 '22 08:09 bjarneo

I'll have to look into it, i do not know tbh.

luismanson avatar Sep 12 '22 22:09 luismanson

But, of course, using t('some.string', 'Default string') is a good start, because that means if a language does not have the translation, it will automatically fallback to the default english string?

That would mean that the english translations are hardcoded into the program, instead of an external string file, correct? While practical in some ways, that would make adjusting the english strings later on more complicated again.

bytebone avatar Sep 13 '22 08:09 bytebone

Yes, that's right, it's a "good start" as @bjarneo called it and we can all agree that hardcoding stuff is a big no-no... However this would allow us to still translate the app without adding too much work at the default strings or new code, this is where some for of extraction/tool could come in handy.

luismanson avatar Sep 13 '22 12:09 luismanson

Continuing from #212

  • Currently, SECRET_FORCED_LANGUAGE=?? does not result in the language changing.
  • It's fine that there's an option to force the language, but imho this should not be the default behavior. Instead:
    1. Don't set SECRET_FORCED_LANGUAGE by default in docker-compose.yml. If set, use the defined locale.
    2. Use the browser's preferred language list to determine the language
    3. Fall back to English
  • I tried to force a language by changing the fallback language in i18n.js, but that doesn't work either. I think the entire i18next logic is faulty at this time.

ltguillaume avatar Sep 17 '23 16:09 ltguillaume

@bjarneo Any luck trying to find out what's causing this? It's pretty hard to onboard people if there's a language barrier (especially with the knowledge that the translation is in fact already present 😅).

ltguillaume avatar Oct 16 '23 11:10 ltguillaume

@ltguillaume this PR solves the SECRET_FORCED_LANGUAGE issue: https://github.com/HemmeligOrg/Hemmelig.app/commit/1404f0ac58e82f4e57b275aa0cbda57d89d6f26a

Test with German: Screenshot 2023-11-15 at 07 43 30

bjarneo avatar Nov 15 '23 06:11 bjarneo

Confirmed working. Next step, proper detection of the browser locale.

ltguillaume avatar Nov 15 '23 12:11 ltguillaume

Yeah, that would've been something

bjarneo avatar Nov 15 '23 14:11 bjarneo

Yeah, that would've been something

Well, when I looked at the code a while back, it seemed like the logic to do that is already there, but perhaps it doesn't actually get the values from request header to properly evaluate them against the available languages, thus defaulting to English (or SECRET_FORCED_LANGUAGE)?

ltguillaume avatar Nov 15 '23 14:11 ltguillaume