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

Updating translations on the fly #21

Open
randohinn opened this issue Mar 11, 2019 · 5 comments
Open

Updating translations on the fly #21

randohinn opened this issue Mar 11, 2019 · 5 comments

Comments

@randohinn
Copy link
Contributor

Am I correct in believing, based on my test attempts, that this library does not yet support changing translations on the fly?

I'm currently testing with this:

import './stylesheets/Application.css';
import React, { useState, useEffect } from 'react';
import { Router, Link, Location, navigate, globalHistory} from '@reach/router';
import Menuscreen from './Menuscreen.js';
import Scanscreen from './Scanscreen.js';
import Picturescreen from './Picturescreen.js';
import ErrorBoundary from './ErrorBoundary.js';
import ApplicationContext from './ApplicationContext.js';
import { TranslatorProvider } from "react-translate"


export default function Application() {
    const [lang, setLanguage] = useState(localStorage.getItem('ahhaaLanguage') || 'et');
    const [qrCode, setQrCode] = useState(localStorage.getItem('ahhaaQR'));
    const [translations, setTranslations] = useState({locale:"et"});

    useEffect(() => {
        setTranslations({
          locale: "et",
          Scanscreen: {
            SIMPLE_KEY: "ABCDEF",
            KEY_WITH_PARAMS: "Hello {{name}}",
            KEY_WITH_PLURAL: [
              "You have {{n}} message",
              "You have {{n}} messages",
            ],
          },
        });
       console.log(translations);
});

    return (
        <ErrorBoundary>
            <ApplicationContext.Provider value={{ lang, qrCode, setLanguage, setQrCode, translations, setTranslations }}>
                <TranslatorProvider translations={translations}>
                    <Location>
                        <Router>
                                <Scanscreen path="/" default />
                                <Menuscreen path="/menu" />
                                <Picturescreen path="/pictures" />
                        </Router>
                    </Location>
                </TranslatorProvider>
            </ApplicationContext.Provider>
        </ErrorBoundary>
    );
}

And seeing that my translations set in the effect hook, are not rendered in my Scanscreen, as translated values, instead, I only see the key names, as if no translations existed for that key. What would be the minimum amount of work required to implement this?

@randohinn
Copy link
Contributor Author

@bloodyowl Any ideas?

@bloodyowl
Copy link
Owner

hum AFAIK it should work. don't you have some shouldComponentUpdate at some point in your app?

@randohinn
Copy link
Contributor Author

hum AFAIK it should work. don't you have some shouldComponentUpdate at some point in your app?

Absolutely none exist :/

@randohinn
Copy link
Contributor Author

I have hereby changed libraries for translation, but this should still be looked into, as this is one of the top hits on google for "react translations".

@bloodyowl
Copy link
Owner

isn't it simply because your locale stayed the same? the translator is only updated if this value is changed.
https://github.com/bloodyowl/react-translate/blob/master/src/translate.js#L16

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

2 participants