Skip to content

Commit e62db9e

Browse files
committed
Messages Internationalization (mebjas#132)
1 parent a4e82a0 commit e62db9e

File tree

10 files changed

+233
-330
lines changed

10 files changed

+233
-330
lines changed

src/camera/retriever.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* @fileoverview
33
* Libraries associated with retrieving cameras.
4-
*
4+
*
55
* @author mebjas <[email protected]>
66
*/
77

88
import { CameraDevice } from "./core";
9-
import { Html5QrcodeStrings } from "../strings";
9+
import { t } from "../strings";
1010

1111
/** Class for retrieving cameras on the device. */
1212
export class CameraRetriever {
@@ -16,7 +16,7 @@ export class CameraRetriever {
1616
if (navigator.mediaDevices) {
1717
return CameraRetriever.getCamerasFromMediaDevices();
1818
}
19-
19+
2020
// Using deprecated api to support really old browsers.
2121
var mst = <any>MediaStreamTrack;
2222
if (MediaStreamTrack && mst.getSources) {
@@ -28,9 +28,9 @@ export class CameraRetriever {
2828

2929
private static rejectWithError(): Promise<Array<CameraDevice>> {
3030
// This can potentially happen if the page is loaded without SSL.
31-
let errorMessage = Html5QrcodeStrings.unableToQuerySupportedDevices();
31+
let errorMessage = t('common.unableToQuerySupportedDevices');
3232
if (!CameraRetriever.isHttpsOrLocalhost()) {
33-
errorMessage = Html5QrcodeStrings.insecureContextCameraQueryError();
33+
errorMessage = t('common.insecureContextCameraQueryError');
3434
}
3535
return Promise.reject(errorMessage);
3636
}
@@ -71,7 +71,7 @@ export class CameraRetriever {
7171
}
7272

7373
private static getCamerasFromMediaStreamTrack()
74-
: Promise<Array<CameraDevice>> {
74+
: Promise<Array<CameraDevice>> {
7575
return new Promise((resolve, _) => {
7676
const callback = (sourceInfos: Array<any>) => {
7777
const results: Array<CameraDevice> = [];

src/html5-qrcode-scanner.ts

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* @fileoverview
33
* Complete Scanner build on top of {@link Html5Qrcode}.
44
* - Decode QR Code using web cam or smartphone camera
5-
*
5+
*
66
* @author mebjas <[email protected]>
7-
*
7+
*
88
* The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
99
* http://www.denso-wave.com/qrcode/faqpatent-e.html
1010
*/
@@ -33,9 +33,7 @@ import {
3333
Html5QrcodeFullConfig,
3434
} from "./html5-qrcode";
3535

36-
import {
37-
Html5QrcodeScannerStrings,
38-
} from "./strings";
36+
import { t } from "./strings";
3937

4038
import {
4139
ASSET_FILE_SCAN,
@@ -94,14 +92,14 @@ interface Html5QrcodeScannerConfig
9492
* were previously granted and what camera was last used. If the permissions
9593
* is already granted for "camera", QR code scanning will automatically
9694
* start for previously used camera.
97-
*
95+
*
9896
* Note: default value is {@code true}.
9997
*/
10098
rememberLastUsedCamera?: boolean | undefined;
10199

102100
/**
103101
* Sets the desired scan types to be supported in the scanner.
104-
*
102+
*
105103
* - Not setting a value will follow the default order supported by
106104
* library.
107105
* - First value would be used as the default value. Example:
@@ -119,26 +117,26 @@ interface Html5QrcodeScannerConfig
119117
/**
120118
* If {@code true} the rendered UI will have button to turn flash on or off
121119
* based on device + browser support.
122-
*
120+
*
123121
* Note: default value is {@code false}.
124122
*/
125123
showTorchButtonIfSupported?: boolean | undefined;
126124

127125
/**
128126
* If {@code true} the rendered UI will have slider to zoom camera based on
129127
* device + browser support.
130-
*
128+
*
131129
* Note: default value is {@code false}.
132-
*
130+
*
133131
* TODO(minhazav): Document this API, currently hidden.
134132
*/
135133
showZoomSliderIfSupported?: boolean | undefined;
136134

137135
/**
138136
* Default zoom value if supported.
139-
*
137+
*
140138
* Note: default value is 1x.
141-
*
139+
*
142140
* TODO(minhazav): Document this API, currently hidden.
143141
*/
144142
defaultZoomValueIfSupported?: number | undefined;
@@ -194,7 +192,7 @@ export class Html5QrcodeScanner {
194192
*
195193
* @param elementId Id of the HTML element.
196194
* @param config Extra configurations to tune the code scanner.
197-
* @param verbose - If true, all logs would be printed to console.
195+
* @param verbose - If true, all logs would be printed to console.
198196
*/
199197
public constructor(
200198
elementId: string,
@@ -223,7 +221,7 @@ export class Html5QrcodeScanner {
223221

224222
/**
225223
* Renders the User Interface.
226-
*
224+
*
227225
* @param qrCodeSuccessCallback Callback called when an instance of a QR
228226
* code or any other supported bar code is found.
229227
* @param qrCodeErrorCallback optional, callback called in cases where no
@@ -246,7 +244,7 @@ export class Html5QrcodeScanner {
246244

247245
this.lastMatchFound = decodedText;
248246
this.setHeaderMessage(
249-
Html5QrcodeScannerStrings.lastMatch(decodedText),
247+
t('scanner.lastMatch', { decodedText }),
250248
Html5QrcodeScannerStatus.STATUS_SUCCESS);
251249
}
252250
};
@@ -273,13 +271,13 @@ export class Html5QrcodeScanner {
273271
//#region State related public APIs
274272
/**
275273
* Pauses the ongoing scan.
276-
*
274+
*
277275
* Notes:
278276
* - Should only be called if camera scan is ongoing.
279-
*
277+
*
280278
* @param shouldPauseVideo (Optional, default = false) If {@code true}
281279
* the video will be paused.
282-
*
280+
*
283281
* @throws error if method is called when scanner is not in scanning state.
284282
*/
285283
public pause(shouldPauseVideo?: boolean) {
@@ -289,19 +287,19 @@ export class Html5QrcodeScanner {
289287

290288
this.getHtml5QrcodeOrFail().pause(shouldPauseVideo);
291289
}
292-
290+
293291
/**
294292
* Resumes the paused scan.
295-
*
293+
*
296294
* If the video was previously paused by setting {@code shouldPauseVideo}
297295
* to {@code true} in {@link Html5QrcodeScanner#pause(shouldPauseVideo)},
298296
* calling this method will resume the video.
299-
*
297+
*
300298
* Notes:
301299
* - Should only be called if camera scan is ongoing.
302300
* - With this caller will start getting results in success and error
303301
* callbacks.
304-
*
302+
*
305303
* @throws error if method is called when scanner is not in paused state.
306304
*/
307305
public resume() {
@@ -319,7 +317,7 @@ export class Html5QrcodeScanner {
319317

320318
/**
321319
* Removes the QR Code scanner UI.
322-
*
320+
*
323321
* @returns Promise which succeeds if the cleanup is complete successfully,
324322
* fails otherwise.
325323
*/
@@ -371,11 +369,11 @@ export class Html5QrcodeScanner {
371369
//#region Beta APIs to modify running stream state.
372370
/**
373371
* Returns the capabilities of the running video track.
374-
*
372+
*
375373
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/getConstraints
376-
*
374+
*
377375
* Note: Should only be called if {@code Html5QrcodeScanner#getState()}
378-
* returns {@code Html5QrcodeScannerState#SCANNING} or
376+
* returns {@code Html5QrcodeScannerState#SCANNING} or
379377
* {@code Html5QrcodeScannerState#PAUSED}.
380378
*
381379
* @returns the capabilities of a running video track.
@@ -388,11 +386,11 @@ export class Html5QrcodeScanner {
388386
/**
389387
* Returns the object containing the current values of each constrainable
390388
* property of the running video track.
391-
*
389+
*
392390
* Read more: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/getSettings
393-
*
391+
*
394392
* Note: Should only be called if {@code Html5QrcodeScanner#getState()}
395-
* returns {@code Html5QrcodeScannerState#SCANNING} or
393+
* returns {@code Html5QrcodeScannerState#SCANNING} or
396394
* {@code Html5QrcodeScannerState#PAUSED}.
397395
*
398396
* @returns the supported settings of the running video track.
@@ -406,7 +404,7 @@ export class Html5QrcodeScanner {
406404
* Apply a video constraints on running video track from camera.
407405
*
408406
* Note: Should only be called if {@code Html5QrcodeScanner#getState()}
409-
* returns {@code Html5QrcodeScannerState#SCANNING} or
407+
* returns {@code Html5QrcodeScannerState#SCANNING} or
410408
* {@code Html5QrcodeScannerState#PAUSED}.
411409
*
412410
* @param {MediaTrackConstraints} specifies a variety of video or camera
@@ -536,7 +534,7 @@ export class Html5QrcodeScanner {
536534
const $this = this;
537535
$this.showHideScanTypeSwapLink(false);
538536
$this.setHeaderMessage(
539-
Html5QrcodeScannerStrings.cameraPermissionRequesting());
537+
t('scanner.cameraPermissionRequesting'));
540538

541539
const createPermissionButtonIfNotExists = () => {
542540
if (!requestPermissionButton) {
@@ -556,14 +554,14 @@ export class Html5QrcodeScanner {
556554
$this.renderCameraSelection(cameras);
557555
} else {
558556
$this.setHeaderMessage(
559-
Html5QrcodeScannerStrings.noCameraFound(),
557+
t('scanner.noCameraFound'),
560558
Html5QrcodeScannerStatus.STATUS_WARNING);
561559
createPermissionButtonIfNotExists();
562560
}
563561
}).catch((error) => {
564562
$this.persistedDataManager.setHasPermission(
565563
/* hasPermission */ false);
566-
564+
567565
if (requestPermissionButton) {
568566
requestPermissionButton.disabled = false;
569567
} else {
@@ -589,7 +587,7 @@ export class Html5QrcodeScanner {
589587
.createElement<HTMLButtonElement>(
590588
"button", this.getCameraPermissionButtonId());
591589
requestPermissionButton.innerText
592-
= Html5QrcodeScannerStrings.cameraPermissionTitle();
590+
= t('scanner.cameraPermissionTitle');
593591

594592
requestPermissionButton.addEventListener("click", function () {
595593
requestPermissionButton.disabled = true;
@@ -678,7 +676,7 @@ export class Html5QrcodeScanner {
678676
return;
679677
}
680678

681-
$this.setHeaderMessage(Html5QrcodeScannerStrings.loadingImage());
679+
$this.setHeaderMessage(t('scanner.loadingImage'));
682680
$this.html5Qrcode.scanFileV2(file, /* showImage= */ true)
683681
.then((html5qrcodeResult: Html5QrcodeResult) => {
684682
$this.resetHeaderMessage();
@@ -742,14 +740,14 @@ export class Html5QrcodeScanner {
742740
= BaseUiElementFactory.createElement<HTMLButtonElement>(
743741
"button", PublicUiElementIdAndClasses.CAMERA_START_BUTTON_ID);
744742
cameraActionStartButton.innerText
745-
= Html5QrcodeScannerStrings.scanButtonStartScanningText();
743+
= t('scanner.scanButtonStartScanningText');
746744
cameraActionContainer.appendChild(cameraActionStartButton);
747745

748746
const cameraActionStopButton
749747
= BaseUiElementFactory.createElement<HTMLButtonElement>(
750748
"button", PublicUiElementIdAndClasses.CAMERA_STOP_BUTTON_ID);
751749
cameraActionStopButton.innerText
752-
= Html5QrcodeScannerStrings.scanButtonStopScanningText();
750+
= t('scanner.scanButtonStopScanningText');
753751
cameraActionStopButton.style.display = "none";
754752
cameraActionStopButton.disabled = true;
755753
cameraActionContainer.appendChild(cameraActionStopButton);
@@ -792,8 +790,7 @@ export class Html5QrcodeScanner {
792790
cameraActionStartButton.style.display = "none";
793791
}
794792
cameraActionStartButton.innerText
795-
= Html5QrcodeScannerStrings
796-
.scanButtonStartScanningText();
793+
= t('scanner.scanButtonStartScanningText');
797794
cameraActionStartButton.style.opacity = "1";
798795
cameraActionStartButton.disabled = false;
799796
if (shouldShow) {
@@ -804,15 +801,15 @@ export class Html5QrcodeScanner {
804801
cameraActionStartButton.addEventListener("click", (_) => {
805802
// Update the UI.
806803
cameraActionStartButton.innerText
807-
= Html5QrcodeScannerStrings.scanButtonScanningStarting();
804+
= t('scanner.scanButtonScanningStarting');
808805
cameraSelectUi.disable();
809806
cameraActionStartButton.disabled = true;
810807
cameraActionStartButton.style.opacity = "0.5";
811808
// Swap link is available only when both scan types are required.
812809
if (this.scanTypeSelector.hasMoreThanOneScanType()) {
813810
$this.showHideScanTypeSwapLink(false);
814811
}
815-
$this.resetHeaderMessage();
812+
$this.resetHeaderMessage();
816813

817814
// Attempt starting the camera.
818815
const cameraId = cameraSelectUi.getValue();
@@ -866,7 +863,7 @@ export class Html5QrcodeScanner {
866863
if(this.scanTypeSelector.hasMoreThanOneScanType()) {
867864
$this.showHideScanTypeSwapLink(true);
868865
}
869-
866+
870867
cameraSelectUi.enable();
871868
cameraActionStartButton.disabled = false;
872869
cameraActionStopButton.style.display = "none";
@@ -900,9 +897,9 @@ export class Html5QrcodeScanner {
900897
private createSectionSwap() {
901898
const $this = this;
902899
const TEXT_IF_CAMERA_SCAN_SELECTED
903-
= Html5QrcodeScannerStrings.textIfCameraScanSelected();
900+
= t('scanner.textIfCameraScanSelected');
904901
const TEXT_IF_FILE_SCAN_SELECTED
905-
= Html5QrcodeScannerStrings.textIfFileScanSelected();
902+
= t('scanner.textIfFileScanSelected');
906903

907904
// TODO(minhaz): Export this as an UI element.
908905
const section = document.getElementById(this.getDashboardSectionId())!;
@@ -930,7 +927,7 @@ export class Html5QrcodeScanner {
930927
$this.resetHeaderMessage();
931928
$this.fileSelectionUi!.resetValue();
932929
$this.sectionSwapAllowed = false;
933-
930+
934931
if (ScanTypeSelector.isCameraScanType($this.currentScanType)) {
935932
// Swap to file based scanning.
936933
$this.clearScanRegion();
@@ -1051,7 +1048,7 @@ export class Html5QrcodeScanner {
10511048
this.cameraScanImage.width = 64;
10521049
this.cameraScanImage.style.opacity = "0.8";
10531050
this.cameraScanImage.src = ASSET_CAMERA_SCAN;
1054-
this.cameraScanImage.alt = Html5QrcodeScannerStrings.cameraScanAltText();
1051+
this.cameraScanImage.alt = t('scanner.cameraScanAltText');
10551052
}
10561053

10571054
private insertFileScanImageToScanRegion() {
@@ -1073,7 +1070,7 @@ export class Html5QrcodeScanner {
10731070
this.fileScanImage.width = 64;
10741071
this.fileScanImage.style.opacity = "0.8";
10751072
this.fileScanImage.src = ASSET_FILE_SCAN;
1076-
this.fileScanImage.alt = Html5QrcodeScannerStrings.fileScanAltText();
1073+
this.fileScanImage.alt = t('scanner.fileScanAltText');
10771074
}
10781075

10791076
private clearScanRegion() {

0 commit comments

Comments
 (0)