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

Closes #21: Pass a custom document object to the options of handle #22

Merged
merged 2 commits into from
Mar 28, 2023
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ localize("#btn1");
localize("#btn1");
```

### use a custom `document` object
```js
<template id="template">
<a id="btn1" href="#" data-i18n="myKey"></a>
</template>
const shadowRoot = document.body.attachShadow();
const template = document.getElementById("template");
shadowRoot.appendChild(template.content.cloneNode(true));
localize("#btn1", {document: shadowRoot});
```

## Motivation
- Having an occasion to try some packages like rollup, babel or uglify.
- Obtaining the same kind of functionnalities than with `jquery-i18next` in a project not using jquery.
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function init(i18next, options={}){
}

function handle(selector, opts){
var elems = options.document.querySelectorAll(selector);
const document = opts?.document || options.document;
var elems = document.querySelectorAll(selector);
for(let i = 0; i < elems.length; i++){
let elem = elems[i];
let childs = elem.querySelectorAll('[' + options.selectorAttr + ']');
Expand Down