Skip to content

Commit

Permalink
Fixed: Ensure translations are fetched before loading app
Browse files Browse the repository at this point in the history
  • Loading branch information
markus101 committed Jul 15, 2023
1 parent 3aa3ac9 commit ad2721d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions frontend/build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ module.exports = (env) => {
hints: false
},

experiments: {
topLevelAwait: true
},

plugins: [
new webpack.DefinePlugin({
__DEV__: !isProduction,
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import PageConnector from 'Components/Page/PageConnector';
import ApplyTheme from './ApplyTheme';
import AppRoutes from './AppRoutes';

function App({ store, history }) {
function App({ store, history, hasTranslationsError }) {
return (
<DocumentTitle title={window.Sonarr.instanceName}>
<Provider store={store}>
<ConnectedRouter history={history}>
<ApplyTheme>
<PageConnector>
<PageConnector hasTranslationsError={hasTranslationsError}>
<AppRoutes app={App} />
</PageConnector>
</ApplyTheme>
Expand All @@ -25,7 +25,8 @@ function App({ store, history }) {

App.propTypes = {
store: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
history: PropTypes.object.isRequired,
hasTranslationsError: PropTypes.bool.isRequired
};

export default App;
4 changes: 4 additions & 0 deletions frontend/src/Components/Page/ErrorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function ErrorPage(props) {
const {
version,
isLocalStorageSupported,
hasTranslationsError,
seriesError,
customFiltersError,
tagsError,
Expand All @@ -19,6 +20,8 @@ function ErrorPage(props) {

if (!isLocalStorageSupported) {
errorMessage = 'Local Storage is not supported or disabled. A plugin or private browsing may have disabled it.';
} else if (hasTranslationsError) {
errorMessage = 'Failed to load translations from API';
} else if (seriesError) {
errorMessage = getErrorMessage(seriesError, 'Failed to load series from API');
} else if (customFiltersError) {
Expand Down Expand Up @@ -49,6 +52,7 @@ function ErrorPage(props) {
ErrorPage.propTypes = {
version: PropTypes.string.isRequired,
isLocalStorageSupported: PropTypes.bool.isRequired,
hasTranslationsError: PropTypes.bool.isRequired,
seriesError: PropTypes.object,
customFiltersError: PropTypes.object,
tagsError: PropTypes.object,
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/Components/Page/PageConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class PageConnector extends Component {

render() {
const {
hasTranslationsError,
isPopulated,
hasError,
dispatchFetchSeries,
Expand All @@ -232,11 +233,12 @@ class PageConnector extends Component {
...otherProps
} = this.props;

if (hasError || !this.state.isLocalStorageSupported) {
if (hasTranslationsError || hasError || !this.state.isLocalStorageSupported) {
return (
<ErrorPage
{...this.state}
{...otherProps}
hasTranslationsError={hasTranslationsError}
/>
);
}
Expand All @@ -257,6 +259,7 @@ class PageConnector extends Component {
}

PageConnector.propTypes = {
hasTranslationsError: PropTypes.bool.isRequired,
isPopulated: PropTypes.bool.isRequired,
hasError: PropTypes.bool.isRequired,
isSidebarVisible: PropTypes.bool.isRequired,
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/Utilities/String/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ function getTranslations() {

let translations = {};

getTranslations().then((data) => {
translations = data.strings;
});
export function fetchTranslations() {
return getTranslations().then((data) => {
translations = data.strings;
});
}

export default function translate(key, tokens) {
const translation = translations[key] || key;
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createBrowserHistory } from 'history';
import React from 'react';
import { render } from 'react-dom';
import createAppStore from 'Store/createAppStore';
import { fetchTranslations } from 'Utilities/String/translate';
import App from './App/App';

import './preload';
Expand All @@ -12,11 +13,20 @@ import './index.css';

const history = createBrowserHistory();
const store = createAppStore(history);
let hasTranslationsError = false;

try {
await fetchTranslations();

} catch (error) {
hasTranslationsError = true;
}

render(
<App
store={store}
history={history}
hasTranslationsError={hasTranslationsError}
/>,
document.getElementById('root')
);

0 comments on commit ad2721d

Please sign in to comment.