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

Wraps mozL10n to async calls; splits firefox and generic l10n libs. #8394

Merged
merged 1 commit into from
May 31, 2017
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
4 changes: 2 additions & 2 deletions examples/mobile-viewer/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

<script src="../../build/dist/build/pdf.js"></script>
<script src="../../build/dist/web/pdf_viewer.js"></script>

<script src="viewer.js"></script>
</head>

<body>
Expand Down Expand Up @@ -72,5 +70,7 @@ <h1 id="title"></h1>
<button class="toolbarButton zoomOut" title="Zoom Out" id="zoomOut"></button>
<button class="toolbarButton zoomIn" title="Zoom In" id="zoomIn"></button>
</footer>

<script src="viewer.js"></script>
</body>
</html>
58 changes: 33 additions & 25 deletions examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,28 @@ var PDFViewerApplication = {
self.setTitleUsingMetadata(pdfDocument);
}, function (exception) {
var message = exception && exception.message;
var loadingErrorMessage = mozL10n.get('loading_error', null,
'An error occurred while loading the PDF.');
var l10n = self.l10n;
var loadingErrorMessage;

if (exception instanceof PDFJS.InvalidPDFException) {
// change error message also for other builds
loadingErrorMessage = mozL10n.get('invalid_file_error', null,
loadingErrorMessage = l10n.get('invalid_file_error', null,
'Invalid or corrupted PDF file.');
} else if (exception instanceof PDFJS.MissingPDFException) {
// special message for missing PDFs
loadingErrorMessage = mozL10n.get('missing_file_error', null,
loadingErrorMessage = l10n.get('missing_file_error', null,
'Missing PDF file.');
} else if (exception instanceof PDFJS.UnexpectedResponseException) {
loadingErrorMessage = mozL10n.get('unexpected_response_error', null,
loadingErrorMessage = l10n.get('unexpected_response_error', null,
'Unexpected server response.');
} else {
loadingErrorMessage = l10n.get('loading_error', null,
'An error occurred while loading the PDF.');
}

var moreInfo = {
message: message
};
self.error(loadingErrorMessage, moreInfo);
loadingErrorMessage.then(function (msg) {
self.error(msg, {message: message});
});
self.loadingBar.hide();
});
},
Expand Down Expand Up @@ -186,28 +188,29 @@ var PDFViewerApplication = {
},

error: function pdfViewError(message, moreInfo) {
var moreInfoText = mozL10n.get('error_version_info',
var l10n = this.l10n;
var moreInfoText = [l10n.get('error_version_info',
{version: PDFJS.version || '?', build: PDFJS.build || '?'},
'PDF.js v{{version}} (build: {{build}})') + '\n';
'PDF.js v{{version}} (build: {{build}})')];

if (moreInfo) {
moreInfoText +=
mozL10n.get('error_message', {message: moreInfo.message},
'Message: {{message}}');
moreInfoText.push(
l10n.get('error_message', {message: moreInfo.message},
'Message: {{message}}'));
if (moreInfo.stack) {
moreInfoText += '\n' +
mozL10n.get('error_stack', {stack: moreInfo.stack},
'Stack: {{stack}}');
moreInfoText.push(
l10n.get('error_stack', {stack: moreInfo.stack},
'Stack: {{stack}}'));
} else {
if (moreInfo.filename) {
moreInfoText += '\n' +
mozL10n.get('error_file', {file: moreInfo.filename},
'File: {{file}}');
moreInfoText.push(
l10n.get('error_file', {file: moreInfo.filename},
'File: {{file}}'));
}
if (moreInfo.lineNumber) {
moreInfoText += '\n' +
mozL10n.get('error_line', {line: moreInfo.lineNumber},
'Line: {{line}}');
moreInfoText.push(
l10n.get('error_line', {line: moreInfo.lineNumber},
'Line: {{line}}'));
}
}
}
Expand Down Expand Up @@ -239,7 +242,9 @@ var PDFViewerApplication = {
};
moreInfoButton.removeAttribute('hidden');
lessInfoButton.setAttribute('hidden', 'true');
errorMoreInfo.value = moreInfoText;
Promise.all(moreInfoText).then(function (parts) {
errorMoreInfo.value = parts.join('\n');
});
},

progress: function pdfViewProgress(level) {
Expand Down Expand Up @@ -286,10 +291,13 @@ var PDFViewerApplication = {
var linkService = new PDFJS.PDFLinkService();
this.pdfLinkService = linkService;

this.l10n = PDFJS.NullL10n;

var container = document.getElementById('viewerContainer');
var pdfViewer = new PDFJS.PDFViewer({
container: container,
linkService: linkService
linkService: linkService,
l10n: this.l10n,
});
this.pdfViewer = pdfViewer;
linkService.setViewer(pdfViewer);
Expand Down
29 changes: 2 additions & 27 deletions extensions/firefox/tools/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,6 @@
}
}

function translateDocument() {
gLanguage = gExternalLocalizerServices.getLocale();

translateFragment();

gReadyState = "complete";

// fire a 'localized' DOM event
var evtObject = document.createEvent("Event");
evtObject.initEvent("localized", false, false);
evtObject.language = gLanguage;
window.dispatchEvent(evtObject);
}

window.addEventListener("DOMContentLoaded", function() {
if (gExternalLocalizerServices) {
translateDocument();
}
// ... else see setExternalLocalizerServices below
});

// Public API
document.mozL10n = {
// get a localized string
Expand Down Expand Up @@ -143,12 +122,8 @@

setExternalLocalizerServices(externalLocalizerServices) {
gExternalLocalizerServices = externalLocalizerServices;

// ... in case if we missed DOMContentLoaded above.
if (window.document.readyState === "interactive" ||
window.document.readyState === "complete") {
translateDocument();
}
gLanguage = gExternalLocalizerServices.getLocale();
gReadyState = "complete";
},

// translate an element or document fragment
Expand Down
4 changes: 2 additions & 2 deletions external/webL10n/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*/
/*
Additional modifications for PDF.js project:
- Disables language initialization on page loading;
- Disables language initialization on page loading.
- Disables document translation on page loading.
- Removes consoleWarn and consoleLog and use console.log/warn directly.
- Removes window._ assignment.
- Remove compatibility code for OldIE.
Expand Down Expand Up @@ -998,7 +999,6 @@ document.webL10n = (function(window, document, undefined) {
loadLocale(lang, function() {
if (callback)
callback();
translateFragment();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention this change at https://github.com/yurydelendik/pdf.js/blob/6f76510a74f086de4fdb9a21a677649de9618c56/external/webL10n/l10n.js#L24 as well. If we update webL10n in the future, this change may be accidentally reverted if it's not mentioned there.

});
},

Expand Down
11 changes: 0 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,6 @@ gulp.task('generic', ['buildnumber', 'locale'], function () {
gulp.src(COMMON_WEB_FILES, {base: 'web/'})
.pipe(gulp.dest(GENERIC_DIR + 'web')),
gulp.src('LICENSE').pipe(gulp.dest(GENERIC_DIR)),
gulp.src([
'external/webL10n/l10n.js'
]).pipe(gulp.dest(GENERIC_DIR + 'web')),
gulp.src([
'web/locale/*/viewer.properties',
'web/locale/locale.properties'
Expand Down Expand Up @@ -695,7 +692,6 @@ gulp.task('minified-pre', ['buildnumber', 'locale'], function () {

gulp.task('minified-post', ['minified-pre'], function () {
var viewerFiles = [
'external/webL10n/l10n.js',
MINIFIED_DIR + BUILD_DIR + 'pdf.js',
MINIFIED_DIR + '/web/viewer.js'
];
Expand Down Expand Up @@ -789,9 +785,6 @@ gulp.task('firefox-pre', ['buildnumber', 'locale'], function () {
.pipe(gulp.dest(FIREFOX_BUILD_DIR)),
gulp.src('LICENSE').pipe(gulp.dest(FIREFOX_BUILD_DIR)),

gulp.src(FIREFOX_EXTENSION_DIR + 'tools/l10n.js')
.pipe(gulp.dest(FIREFOX_BUILD_CONTENT_DIR + '/web')),

preprocessJS(FIREFOX_CONTENT_DIR + 'PdfStreamConverter.jsm', defines, true)
.pipe(replace(/\bPDFJSSCRIPT_STREAM_CONVERTER_ID\b/g,
FIREFOX_STREAM_CONVERTER_ID))
Expand Down Expand Up @@ -892,8 +885,6 @@ gulp.task('mozcentral-pre', ['buildnumber', 'locale'], function () {
.pipe(replace(/\bPDFJSSCRIPT_COMMIT\b/g, commit))
.pipe(gulp.dest(MOZCENTRAL_EXTENSION_DIR)),
gulp.src('LICENSE').pipe(gulp.dest(MOZCENTRAL_EXTENSION_DIR)),
gulp.src(FIREFOX_EXTENSION_DIR + 'tools/l10n.js')
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + '/web')),

preprocessJS(FIREFOX_CONTENT_DIR + 'PdfJs.jsm', defines, true)
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR)),
Expand Down Expand Up @@ -935,8 +926,6 @@ gulp.task('chromium-pre', ['buildnumber', 'locale'], function () {
gulp.src(COMMON_WEB_FILES, {base: 'web/'})
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + 'web')),

gulp.src('external/webL10n/l10n.js')
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + 'web')),
gulp.src([
'web/locale/*/viewer.properties',
'web/locale/locale.properties'
Expand Down
13 changes: 8 additions & 5 deletions web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { AnnotationLayer } from 'pdfjs-lib';
import { mozL10n } from './ui_utils';
import { NullL10n } from './ui_utils';
import { SimpleLinkService } from './pdf_link_service';

/**
Expand All @@ -24,6 +24,7 @@ import { SimpleLinkService } from './pdf_link_service';
* @property {boolean} renderInteractiveForms
* @property {IPDFLinkService} linkService
* @property {DownloadManager} downloadManager
* @property {IL10n} l10n - Localization service.
*/

class AnnotationLayerBuilder {
Expand All @@ -36,6 +37,7 @@ class AnnotationLayerBuilder {
this.renderInteractiveForms = options.renderInteractiveForms;
this.linkService = options.linkService;
this.downloadManager = options.downloadManager;
this.l10n = options.l10n || NullL10n;

this.div = null;
}
Expand Down Expand Up @@ -73,9 +75,7 @@ class AnnotationLayerBuilder {
parameters.div = this.div;

AnnotationLayer.render(parameters);
if (typeof mozL10n !== 'undefined') {
mozL10n.translate(this.div);
}
this.l10n.translate(this.div);
}
});
}
Expand All @@ -96,15 +96,18 @@ class DefaultAnnotationLayerFactory {
* @param {HTMLDivElement} pageDiv
* @param {PDFPage} pdfPage
* @param {boolean} renderInteractiveForms
* @param {IL10n} l10n
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(pageDiv, pdfPage,
renderInteractiveForms = false) {
renderInteractiveForms = false,
l10n = NullL10n) {
return new AnnotationLayerBuilder({
pageDiv,
pdfPage,
renderInteractiveForms,
linkService: new SimpleLinkService(),
l10n,
});
}
}
Expand Down
Loading