integrate localization

This commit is contained in:
Luis Riegger 2021-05-09 16:27:32 +02:00
parent 65227077c8
commit 449bef0aa0
4 changed files with 87 additions and 31 deletions

View File

@ -1,6 +1,8 @@
import {CharacteristicValue, Logger, PlatformAccessory, PlatformAccessoryEvent, PlatformConfig, Service} from 'homebridge'; import {CharacteristicValue, Logger, PlatformAccessory, PlatformAccessoryEvent, PlatformConfig, Service} from 'homebridge';
import {HomebridgeKoboldPlatform} from '../homebridgeKoboldPlatform'; import {HomebridgeKoboldPlatform} from '../homebridgeKoboldPlatform';
import {Options} from '../models/options'; import {Options} from '../models/options';
import { RobotService, CleanType } from '../models/services';
import { SERVICES, BACKGROUND_INTERVAL, PREFIX } from '../defaults';
/** /**
* Platform Accessory * Platform Accessory
@ -30,7 +32,7 @@ export class KoboldVacuumRobotAccessory
// Config // Config
private readonly backgroundUpdateInterval: number; private readonly backgroundUpdateInterval: number;
private readonly prefix: boolean; private readonly prefix: boolean;
private readonly availableServices: string[]; private readonly availableServices: Set<RobotService>;
// Transient // Transient
private isSpotCleaning: boolean; private isSpotCleaning: boolean;
@ -53,7 +55,7 @@ export class KoboldVacuumRobotAccessory
this.backgroundUpdateInterval = KoboldVacuumRobotAccessory.parseBackgroundUpdateInterval(this.config['backgroundUpdate']); this.backgroundUpdateInterval = KoboldVacuumRobotAccessory.parseBackgroundUpdateInterval(this.config['backgroundUpdate']);
this.prefix = this.config['prefix'] || PREFIX; this.prefix = this.config['prefix'] || PREFIX;
this.availableServices = this.config['services'] || SERVICES; this.availableServices = new Set(this.config['services']) || SERVICES;
this.isSpotCleaning = false; this.isSpotCleaning = false;
@ -82,7 +84,7 @@ export class KoboldVacuumRobotAccessory
}); });
// Services // Services
this.cleanService = this.getSwitchService(RobotService.CLEAN_HOUSE); this.cleanService = this.getSwitchService(RobotService.CLEAN);
this.spotCleanService = this.getSwitchService(RobotService.CLEAN_SPOT); this.spotCleanService = this.getSwitchService(RobotService.CLEAN_SPOT);
this.goToDockService = this.getSwitchService(RobotService.GO_TO_DOCK); this.goToDockService = this.getSwitchService(RobotService.GO_TO_DOCK);
this.dockStateService = this.getOccupancyService(RobotService.DOCKED) this.dockStateService = this.getOccupancyService(RobotService.DOCKED)
@ -170,11 +172,11 @@ export class KoboldVacuumRobotAccessory
}); });
} }
private getSwitchService(serviceName: string) private getSwitchService(serviceName: RobotService)
{ {
let displayName = this.prefix ? this.robot.name + serviceName : serviceName; let displayName = this.prefix ? this.robot.name + serviceName : serviceName;
if (this.availableServices.includes(serviceName)) if (this.availableServices.has(serviceName))
{ {
return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.Switch, displayName, serviceName); return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.Switch, displayName, serviceName);
} }
@ -188,11 +190,11 @@ export class KoboldVacuumRobotAccessory
} }
} }
private getOccupancyService(serviceName: string) private getOccupancyService(serviceName: RobotService)
{ {
let displayName = this.prefix ? this.robot.name + serviceName : serviceName; let displayName = this.prefix ? this.robot.name + serviceName : serviceName;
if (this.availableServices.includes(serviceName)) if (this.availableServices.has(serviceName))
{ {
return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.OccupancySensor, displayName, serviceName); return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.OccupancySensor, displayName, serviceName);
} }
@ -616,11 +618,6 @@ export class KoboldVacuumRobotAccessory
} }
} }
enum CleanType
{
ALL,
SPOT
}
enum DebugType enum DebugType
{ {
@ -628,21 +625,3 @@ enum DebugType
STATUS, STATUS,
INFO INFO
} }
enum RobotService
{
CLEAN_HOUSE = "Clean house",
CLEAN_SPOT = "Clean spot",
GO_TO_DOCK = "Go to dock",
DOCKED = "Docked sensor",
BIN_FULL = "Bin full sensor",
FIND_ME = "Find me",
SCHEDULE = "Schedule",
ECO = "Eco",
NOGO_LINES = "Nogo lines",
EXTRA_CARE = "Extra care"
}
const BACKGROUND_INTERVAL = 30;
const PREFIX = false;
const SERVICES = Object.values(RobotService);

5
src/defaults.ts Normal file
View File

@ -0,0 +1,5 @@
import { RobotService } from "./models/services";
export const BACKGROUND_INTERVAL = 30;
export const PREFIX = false;
export const SERVICES = new Set(Object.values(RobotService));

54
src/localization.ts Normal file
View File

@ -0,0 +1,54 @@
export enum availableLocales {
EN = "en",
DE = "de",
FR = "fr",
}
const localizationDicts = {
'en': {
"clean": "Clean",
"cleanThe": "Clean the",
"goToDock": "Go to Dock",
"dockState": "Docked",
"binFull": "Bin Full",
"eco": "Eco Mode",
"noGoLines": "NoGo Lines",
"extraCare": "Extra Care",
"schedule": "Schedule",
"findMe": "Find me",
"cleanSpot": "Clean Spot",
"battery": "Battery"
},
'de': {
"clean": "Sauge",
"cleanThe": "Sauge",
"goToDock": "Zur Basis",
"dockState": "In der Basis",
"binFull": "Behälter voll",
"eco": "Eco Modus",
"noGoLines": "NoGo Linien",
"extraCare": "Extra Care",
"schedule": "Zeitplan",
"findMe": "Finde mich",
"cleanSpot": "Spot Reinigung",
"battery": "Batterie"
},
'fr': {
"clean": "Aspirer",
"cleanThe": "Aspirer",
"goToDock": "Retour à la base",
"dockState": "Sur la base",
"binFull": "Conteneur plein",
"eco": "Eco mode",
"noGoLines": "Lignes NoGo",
"extraCare": "Extra Care",
"schedule": "Planifier",
"findMe": "Me retrouver",
"cleanSpot": "Nettoyage local",
"battery": "Batterie"
}
}
export function localize(label: string, locale: availableLocales) : string {
return localizationDicts[locale][label] ?? label
}

18
src/models/services.ts Normal file
View File

@ -0,0 +1,18 @@
export enum CleanType {
ALL,
SPOT,
}
export enum RobotService {
CLEAN = "clean",
CLEAN_SPOT = "cleanSpot",
GO_TO_DOCK = "goToDock",
DOCKED = "dockState",
BIN_FULL = "binFull",
FIND_ME = "findMe",
SCHEDULE = "schedule",
ECO = "eco",
NOGO_LINES = "noGoLines",
EXTRA_CARE = "extraCare",
BATTERY = "battery",
}