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

auto detect language on first start #78

Merged
merged 1 commit into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serve = args.some(val => val === '--serve');
// MY IMPORTANT IMPORT !!!!
const dialog = require('electron').dialog;

let userWantedToOpen = null;
let userWantedToOpen: string = null;
let myWindow = null;

// TODO -- maybe clean up the `userWantedToOpen` with whatever pattern recommended by Electron
Expand Down Expand Up @@ -121,7 +121,7 @@ try {
// OPEN FILE ON MAC FROM FILE DOUBLE CLICK
// THIS RUNS (ONLY) on MAC !!!
app.on('will-finish-launching', function () {
app.on('open-file', (event, filePath) => {
app.on('open-file', (event, filePath: string) => {
if (filePath) {
userWantedToOpen = filePath;
if (!macFirstRun) {
Expand Down Expand Up @@ -310,6 +310,7 @@ ipc.on('just-started', function (event, someMessage) {

fs.readFile(path.join(pathToAppData, 'video-hub-app', 'settings.json'), (err, data) => {
if (err) {
win.setBounds({ x: 0, y: 0, width: screenWidth, height: screenHeight });
event.sender.send('pleaseOpenWizard', true); // firstRun = true!
} else {

Expand All @@ -324,7 +325,10 @@ ipc.on('just-started', function (event, someMessage) {
win.setBounds({ x: 0, y: 0, width: screenWidth, height: screenHeight });
}

event.sender.send('settingsReturning', savedSettings, userWantedToOpen);
// Reference: https://github.com/electron/electron/blob/master/docs/api/locales.md
const locale: string = app.getLocale();

event.sender.send('settingsReturning', savedSettings, userWantedToOpen, locale);
}
});
});
Expand Down
6 changes: 6 additions & 0 deletions src/app/components/common/app-state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export type SupportedLanguage = 'en' | 'ru';
// Please conform the supported languages exactly to the first two characters from here:
// https://github.com/electron/electron/blob/master/docs/api/locales.md

export let AppState: AppStateInterface = {
currentVhaFile: '', // full path to the .vha file
currentView: 'thumbs', // can be 'thumbs' | 'filmstrip' | 'files'
currentZoomLevel: 1,
hubName: '',
imgHeight: 100, // gallery/filmstrip height
language: 'en',
menuHidden: false,
numOfFolders: 0,
selectedOutputFolder: '',
Expand All @@ -16,6 +21,7 @@ export interface AppStateInterface {
currentZoomLevel: number;
hubName: string;
imgHeight: number;
language: SupportedLanguage;
menuHidden: boolean;
numOfFolders: number;
selectedOutputFolder: string;
Expand Down
3 changes: 0 additions & 3 deletions src/app/components/common/settings-object.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { AppStateInterface } from './app-state';
import { HistoryItem } from './history-item.interface';

export type SupportedLanguage = 'en' | 'ru';

export interface SettingsObject {
appState: AppStateInterface;
buttonSettings: any;
language: SupportedLanguage;
vhaFileHistory: HistoryItem[];
windowSizeAndPosition: WinBounds;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ <h2>{{ 'SETTINGS.changeLanguage' | translate }}</h2>

<div style="margin: 20px">
<select (change)="changeLanguage($event.target.value)" class="languageDropDown">
<option value="en" [selected]="currentLanguage == 'en'">English</option>
<option value="ru" [selected]="currentLanguage == 'ru'">Russian</option>
<option value="en" [selected]="appState.language == 'en'">English</option>
<option value="ru" [selected]="appState.language == 'ru'">Russian</option>
</select>
</div>

Expand Down
60 changes: 42 additions & 18 deletions src/app/components/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { FinalObject, ImageElement } from '../common/final-object.interface';
import { HistoryItem } from '../common/history-item.interface';
import { ImportSettingsObject } from '../common/import.interface';
import { SavableProperties } from '../common/savable-properties.interface';
import { SettingsObject, SupportedLanguage } from '../common/settings-object.interface';
import { SettingsObject } from '../common/settings-object.interface';
import { WizardOptions } from '../common/wizard-options.interface';

import { AppState } from '../common/app-state';
import { AppState, SupportedLanguage } from '../common/app-state';
import { Filters } from '../common/filters';
import { SettingsButtons, SettingsButtonsGroups, SettingsMetaGroupLabels, SettingsMetaGroup } from '../common/settings-buttons';

Expand Down Expand Up @@ -203,8 +203,6 @@ export class HomeComponent implements OnInit, AfterViewInit {

isFirstRunEver = false;

currentLanguage: SupportedLanguage;

// Listen for key presses
@HostListener('document:keydown', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
Expand Down Expand Up @@ -427,8 +425,13 @@ export class HomeComponent implements OnInit, AfterViewInit {
});

// Final object returns
this.electronService.ipcRenderer.on('finalObjectReturning',
(event, finalObject: FinalObject, pathToFile: string, outputFolderWithTrailingSlash: string, changedRootFolder = false) => {
this.electronService.ipcRenderer.on('finalObjectReturning', (
event,
finalObject: FinalObject,
pathToFile: string,
outputFolderWithTrailingSlash: string,
changedRootFolder = false
) => {
this.finalArrayNeedsSaving = changedRootFolder;
this.appState.currentVhaFile = pathToFile;
this.hubNameToRemember = finalObject.hubName;
Expand All @@ -450,9 +453,15 @@ export class HomeComponent implements OnInit, AfterViewInit {
});

// Returning settings
this.electronService.ipcRenderer.on('settingsReturning', (event, settingsObject: SettingsObject, userWantedToOpen) => {
this.electronService.ipcRenderer.on('settingsReturning', (
event,
settingsObject: SettingsObject,
userWantedToOpen: string,
locale: string
) => {
this.vhaFileHistory = (settingsObject.vhaFileHistory || []);
this.restoreSettingsFromBefore(settingsObject);
this.setOrRestoreLanguage(settingsObject.appState.language, locale);
if (this.appState.currentZoomLevel !== 1) {
this.electronService.webFrame.setZoomFactor(this.appState.currentZoomLevel);
}
Expand Down Expand Up @@ -1124,7 +1133,6 @@ export class HomeComponent implements OnInit, AfterViewInit {
return {
appState: this.appState,
buttonSettings: buttonSettings,
language: this.currentLanguage,
vhaFileHistory: this.vhaFileHistory,
};
}
Expand Down Expand Up @@ -1176,13 +1184,24 @@ export class HomeComponent implements OnInit, AfterViewInit {
this.settingsButtons[element].hidden = settingsObject.buttonSettings[element].hidden;
}
});
if (settingsObject.language) {
this.changeLanguage(settingsObject.language);
}
this.computeTextBufferAmount();
this.settingsButtons['showTags'].toggled = false; // never show tags on load (they don't load right anyway)
}

/**
* Restore the language from settings or try to set it from the user's locale
* @param storedSetting the `language` attribute in AppState
* @param locale the string that comes from `app.getLocale()`
* List of locales is here: https://github.com/electron/electron/blob/master/docs/api/locales.md
*/
setOrRestoreLanguage(chosenLanguage: SupportedLanguage, locale: string): void {
if (chosenLanguage) {
this.changeLanguage(chosenLanguage);
} else {
this.changeLanguage(<any>locale.substring(0, 2));
}
}

/**
* Update the min and max resolution for the resolution filter
* @param selection
Expand Down Expand Up @@ -1367,16 +1386,21 @@ export class HomeComponent implements OnInit, AfterViewInit {

/**
* Change the language via ngx-translate
* `en` is the default
* @param language
*/
changeLanguage(language: SupportedLanguage): void {
this.currentLanguage = language;
if (language === 'en') {
this.translate.use('en');
this.translate.setTranslation('en', English );
} else {
this.translate.use('ru');
this.translate.setTranslation('ru', Russian );
switch (language) {
case 'ru':
this.translate.use('ru');
this.translate.setTranslation('ru', Russian );
this.appState.language = 'ru';
break;
default:
this.translate.use('en');
this.translate.setTranslation('en', English );
this.appState.language = 'en';
break;
}
}

Expand Down