-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
928 additions
and
1,580 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"settings": { | ||
"key": ":D" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Module in charge of giving back i18next resources to the renderer process | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { app, ipcMain } from 'electron'; | ||
|
||
import channels from '../../shared/lib/ipc-channels'; | ||
import { LANGUAGES } from '../../shared/lib/languages'; | ||
import ConfigModule from './config'; | ||
import ModuleWindow from './module-window'; | ||
|
||
class I18nModule extends ModuleWindow { | ||
protected config: ConfigModule; | ||
|
||
constructor(window: Electron.BrowserWindow, config: ConfigModule) { | ||
super(window); | ||
|
||
this.config = config; | ||
} | ||
|
||
async load(): Promise<void> { | ||
console.log(app.getLocale(), app.getLocaleCountryCode()); | ||
console.log('========================'); | ||
const localesRoot = path.join(__dirname, '..', '..', 'locales'); | ||
|
||
ipcMain.handle(channels.I18N_GET_RESOURCES, (_event) => { | ||
const resources: Record<string, string> = {}; | ||
|
||
LANGUAGES.forEach((lang) => { | ||
resources[lang] = JSON.parse(fs.readFileSync(path.join(localesRoot, `${lang}.json`), { encoding: 'utf8' })); | ||
}); | ||
|
||
return resources; | ||
}); | ||
} | ||
} | ||
|
||
export default I18nModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* i18next instance | ||
*/ | ||
|
||
import { ipcRenderer } from 'electron'; | ||
import i18next, { BackendModule } from 'i18next'; | ||
import { initReactI18next, useTranslation as useI18nextTranslation, UseTranslationResponse } from 'react-i18next'; | ||
import BackendAdapter from 'i18next-multiload-backend-adapter'; | ||
import LanguageDetector from 'i18next-electron-language-detector'; | ||
|
||
import { DEFAULT_LANGUAGE, LANGUAGES, NAMESPACES } from '../../shared/lib/languages'; | ||
import channels from '../../shared/lib/ipc-channels'; | ||
|
||
const ipcBackend: BackendModule = { | ||
type: 'backend', | ||
init: function (_services, _backendOptions, _i18nextOptions) { | ||
/* use services and options */ | ||
}, | ||
read: function (_language, _namespace, callback) { | ||
callback( | ||
new Error( | ||
'i18next ipcBackend should not be using `read`, is i18next-multiload-backend-adapter correctly configured' | ||
), | ||
null | ||
); | ||
}, | ||
readMulti: function (languages, namespaces, callback) { | ||
console.info('request MULTI', languages, namespaces); | ||
|
||
ipcRenderer | ||
.invoke(channels.I18N_GET_RESOURCES) | ||
.then((resources: Record<string, Record<string, string>>) => { | ||
console.log('RECEIVED: ', resources); | ||
callback(null, resources); | ||
}) | ||
.catch((err) => { | ||
console.log('NOTRECEIVED'); | ||
callback(err, null); | ||
}); | ||
}, | ||
}; | ||
|
||
const i18n = i18next | ||
.createInstance({ | ||
backend: { | ||
backend: ipcBackend, | ||
}, | ||
|
||
// lng: 'en', // use detector instead | ||
supportedLngs: LANGUAGES, | ||
fallbackLng: DEFAULT_LANGUAGE, | ||
ns: NAMESPACES, | ||
defaultNS: '', | ||
|
||
debug: process.env.NODE_ENV === 'development', | ||
}) | ||
.use(LanguageDetector) | ||
.use(BackendAdapter) | ||
.use(initReactI18next); | ||
|
||
export default i18n; | ||
|
||
/** | ||
* Custom useTranslation hook to avoid suspense | ||
*/ | ||
export const useTranslation = (): UseTranslationResponse<''> => useI18nextTranslation('', { useSuspense: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { LANGUAGES } from '../languages'; | ||
|
||
describe('languages', () => { | ||
test('should all have a valid JSON file containing translations', () => { | ||
LANGUAGES.forEach((lang) => { | ||
expect(() => { | ||
const content = fs.readFileSync(path.join(process.cwd(), 'locales', `${lang}.json`), { encoding: 'utf8' }); | ||
JSON.parse(content); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Edit me if you add or remove a language | ||
*/ | ||
|
||
export const LANGUAGES = ['en']; | ||
export const DEFAULT_LANGUAGE = 'en'; | ||
export const NAMESPACES = ['settings', 'library', 'playlists']; |
1 change: 1 addition & 0 deletions
1
src/shared/types/declarations/i18next-electron-language-detector.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'i18next-electron-language-detector'; |
Oops, something went wrong.