Skip to content

Commit

Permalink
Merge pull request #531 from justinlampley/Add-LocalesAsStaticFiles
Browse files Browse the repository at this point in the history
Fetch locales through http requests to the backend server
  • Loading branch information
jurgelenas authored Jun 24, 2023
2 parents e34238e + 2a37025 commit 85faa97
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@
},
"extraResources": [
"./assets/**",
"./devices/**"
"./devices/**",
{
"from": "./src/i18n",
"to": "i18n"
}
],
"publish": {
"provider": "github",
Expand Down
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export default class ApiServer {
await this.buildContainer(config, logger);
this.app = express();

this.app.use('/locales', express.static(config.localesPath));

this.httpServer = createServer(this.app);
/*
I know, crazy. It is 45 minutes, but on slower network connection it might take a while to download
Expand Down
1 change: 1 addition & 0 deletions src/api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IConfig {
targetsStoragePath: string;
userDefinesStoragePath: string;
userDataPath: string;
localesPath: string;
}

export const ConfigToken = new Token<IConfig>('CONFIG_TOKEN');
7 changes: 6 additions & 1 deletion src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector'; // For auto-detecting language
import Backend from 'i18next-http-backend'; // For loading translations from backend

const searchParams = new URLSearchParams(window.location.search.slice(1));
const loadPath = `${
searchParams.get('base_url') ?? 'http://localhost:3500/'
}/locales/{{lng}}/translation.json`;

i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en',
backend: {
loadPath: './i18n/locales/{{lng}}/messages.json',
loadPath,
},
debug:
process.env.NODE_ENV === 'development' ||
Expand Down
12 changes: 10 additions & 2 deletions src/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ const createWindow = async () => {
? path.join(process.resourcesPath, 'devices')
: path.join(__dirname, '../devices');

const localesPath = app.isPackaged
? path.join(process.resourcesPath, 'i18n')
: path.join(__dirname, 'i18n');

logger.log('localesPath', { localesPath });

logger.log('local api server PATH', {
PATH,
});
Expand All @@ -332,6 +338,7 @@ const createWindow = async () => {
targetsStoragePath,
userDefinesStoragePath,
userDataPath: app.getPath('userData'),
localesPath,
},
logger,
port
Expand Down Expand Up @@ -364,10 +371,11 @@ const createWindow = async () => {
}
});

const apiUrl = `http://localhost:${port}/graphql`;
const baseUrl = `http://localhost:${port}`;
const apiUrl = `${baseUrl}/graphql`;
const subscriptionsUrl = `ws://localhost:${port}/graphql`;
mainWindow.loadURL(
`file://${__dirname}/index.html?api_url=${apiUrl}&subscriptions_url=${subscriptionsUrl}`
`file://${__dirname}/index.html?base_url=${baseUrl}&api_url=${apiUrl}&subscriptions_url=${subscriptionsUrl}`
);

// TODO: Use 'ready-to-show' event
Expand Down

0 comments on commit 85faa97

Please sign in to comment.