Closed t2ym closed 5 years ago
Polymer({
importPath: import.meta.url,
__template: html`
<span id="label1">Legacy UI label 1</span><br>
<span id="label2">Legacy UI label 2</span><br>
<span id="label3">Legacy UI label 3</span>
`,
get _template() {
if (this instanceof HTMLElement && !this.constructor._templateLocalizable) {
this.constructor._templateLocalizable = BehaviorsStore._I18nBehavior._constructDefaultBundle(this.__template, this.is);
}
return this.__template;
},
set _template(t) {
//this.__template = t;
},
is: 'i18n-legacy-element',
behaviors: [
BehaviorsStore.I18nBehavior
]
});
_template
conversion via i18n-behavior/i18n-behavior.js
if (ElementMixin) {
// Polymer 2.x
BehaviorsStore._I18nBehavior = BehaviorsStore.I18nBehavior;
BehaviorsStore.I18nBehavior = [ BehaviorsStore._I18nBehavior ];
if (!document.currentScript) {
// Polymer 3.x
BehaviorsStore.I18nBehavior.push({
get _template() {
if (this.__template) {
return this.__template;
}
if (this instanceof HTMLElement &&
this.constructor.name === 'PolymerGenerated' &&
!this.constructor.__finalizeClass) {
this.constructor.__finalizeClass = this.constructor._finalizeClass;
this.constructor._finalizeClass = function _finalizeClass() {
let info = this.generatedFrom;
if (!this._templateLocalizable) {
if (info._template) {
this._templateLocalizable = BehaviorsStore._I18nBehavior._constructDefaultBundle(info._template, info.is);
}
else {
let template = DomModule.import(info.is, 'template');
if (template) {
this._templateLocalizable = BehaviorsStore._I18nBehavior._constructDefaultBundle(template, info.is);
}
}
}
if (!this.hasOwnProperty('importPath')) {
Object.defineProperty(this, 'importPath', { value: info.importPath });
}
return this.__finalizeClass();
}
}
return null;
},
set _template(value) {
this.__template = value;
}
});
}
Browsers | Version | PoC Working | Notes |
---|---|---|---|
Chrome | 64+ | ✅ | |
Firefox | Nightly 60.0a1 | w/ patch below | import.meta.url is not implemented; Bugzilla 1427610 Implement import.meta |
Safari | TP 49 (Safari 11.2, WebKit 13606.1.3.3)+ | ✅ | |
Edge | 17.17101+ | w/ patch below | import.meta.url is not implemented |
import.meta.url
(just for PoC)import.meta.url
with a literal string of response.url
valueaddEventListener('fetch', function (event) {
if (event.request.method === 'GET') {
event.respondWith(async function() {
let response = await fetch(event.request);
if (response.status === 200 && response.type !== 'opaque') {
let url = new URL(response.url);
if (url.pathname.match(/[.]js$/)) {
let code = await response.text();
if (code.indexOf('import.meta.url') >= 0) {
code = code.replace(/import[.]meta[.]url/g, '\'' + response.url + '\'');
}
response = new Response(code, { headers: {'Content-Type': 'text/javascript'} });
}
}
return response;
}());
}
});
gulp-i18n-preprocess
cd i18n-element/components/i18n-element/demo
../node_modules/.bin/gulp
unmodulize
gulp filter to generate temporary HTMLs from JS modulesvar unmodulize = gulpif('**/*.js', through.obj(function (file, enc, callback) {
let htmlTemplate = `<!-- temporary HTML --><link rel="import" href="../../../i18n-element.html"><innerHTML><dom-module>`;
let code = stripBom(String(file.contents));
let template = code.match(/html`([^`]*)`/);
let innerHTML = code.match(/[.]innerHTML = `([^`]*)`/);
let name = file.path.split('/').pop().replace(/[.]js$/,'');
if (template || innerHTML) {
let html = htmlTemplate;
if (template) {
html = html.replace('<dom-module>',
`<dom-module id="${name}"><template>${template[1]}</template></dom-module>\n`);
}
else {
html = html.replace('<dom-module>', '');
}
if (innerHTML) {
html = html.replace('<innerHTML>', innerHTML[1]);
}
else {
html = html.replace('<innerHTML>', '');
}
let htmlFile = new gutil.File({
cwd: file.cwd,
base: file.base,
path: file.path.substring(0, file.path.length - 3) + '.html',
contents: new Buffer(html)
});
console.log('unmodulize: htmlFile.path = ', htmlFile.path, ' name = ', name, 'html = ', html);
this.push(htmlFile);
}
callback(null, file);
}));
i18n
gulp taskgulp.task('i18n', () => {
return gulp.src([ 'poc/**/* ' ], { base: 'poc' })
.pipe(unmodulize) // generate temporary HTMLs from ES modules
.pipe(scan) // generate attributesRepository by scanning <i18n-attr-repo>
.pipe(basenameSort) // sort by component names
.pipe(dropDefaultJSON) // drop old default (English) JSONs
.pipe(preprocess) // preprocess temporary HTMLs and generate new default JSONs
.pipe(tmpHTML) // put preprocessed HTMLs to .tmp directory (not used)
.pipe(dropDummyHTML) // drop the preprocessed HTMLs
.pipe(tmpJSON) // put new default JSONs to .tmp directory
.pipe(importXliff) // import XLIFF to JSONs
.pipe(leverage) // merge diffs between old and new default JSONs into localized JSONs
.pipe(exportXliff) // export XLIFF from JSONs
.pipe(feedback) // feedback to existing JSONs
.pipe(debug({ title: title }))
.pipe(size({ title: title }))
.pipe(gulp.dest('poc')); // output to the same directory
});
Close as #60 is closed.
[Polymer 3.0-preview] Preliminary research on Polymer 3.0 support (ES Modules instead of HTML Imports)
Proof of Concept Demo Page on GitHub Pages with Polymer 3.0-preview and many inelegant patches and temporary hacking
link to GitHub pages
Notes
gh-pages
branchi18n-element
,i18n-behavior
,i18n-format
,i18n-number
) are converted bypolymer-modulizer
Main hacking
this._i18nPreprocess()
tostatic get template()
(automated below)The above hacking is automated in this change
Main Issues and Tasks (There are many other issues)
deepcopy@0.6.3
,make-plural@3
are incompatible with ES modules.root
fromthis
which is undefined in modules is temporarily replaced withwindow
document.currentScript
isnull
in modulesdocument.currentScript
is used for gettingownerDocument
, which is the starting point of searching templates in HTML Imports, which are not used in Polymer 3.0ownerDocument
defaults todocument
resolveUrl
cannot handle relative paths without a validimportPath
propertyimport.meta.url
import.meta.url
. Status for Firefox and Edge is unknown.polymer-modulizer
has--add-import-path
option to add a static propertyimportPath
, which returnsimport.meta.url
, to each element classstatic get template()
has to be wrapped bythis._i18nPreprocess()
gulp-i18n-preprocess
<i18n-attr-repo>
attribute declarations are ineffective to<i18n-dom-bind>
elements in Chrome and all elements in Safari<i18n-dom-bind>
references the repository of<i18n-attr-repo>
too early.<i18n-dom-bind>
be delayed until declarative<i18n-attr-repo>
elements become effective?<i18n-attr-repo>
handling is awkwardpolymer-bundler 4.0
andwebpack
are unknownimportPath
may have an inappropriate value for retrieving JSON resources for localespolymer-bundler
: Figure out how to handle import.meta.urlpolymer-modulizer
are released