Skip to content

Commit 7c65d5c

Browse files
committed
feat(pat display time): Acquire language from the DOM tree instead only the HTML node if the language is not configured.
1 parent d659e09 commit 7c65d5c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/pat/display-time/display-time.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "regenerator-runtime/runtime"; // needed for ``await`` support
22
import Base from "../../core/base";
33
import Parser from "../../core/parser";
4+
import dom from "../../core/dom";
45
import logging from "../../core/logging";
56

67
// Lazy loading modules.
@@ -27,7 +28,7 @@ export default Base.extend({
2728

2829
this.options = parser.parse(this.el, this.options);
2930

30-
let lang = this.options.locale || document.querySelector("html").lang || "en";
31+
let lang = this.options.locale || dom.acquire_attribute(this.el, "lang") || "en";
3132
// we don't support any country-specific language variants, always use first 2 letters
3233
lang = lang.substr(0, 2).toLowerCase();
3334
try {

src/pat/display-time/display-time.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ describe("pat-display-time tests", () => {
5959
expect(el.textContent).toBe("April Donnerstag 22. 2021, 4:00:00");
6060
});
6161

62+
it("Example setting the output format in German due containment in German container", async () => {
63+
document.body.innerHTML = `
64+
<section lang="de">
65+
<time
66+
class="pat-display-time"
67+
datetime="2021-04-22T23:00Z"
68+
data-pat-display-time="output-format: MMMM dddd Do YYYY, h:mm:ss">
69+
</time>
70+
</section>
71+
`;
72+
const el = document.querySelector(".pat-display-time");
73+
74+
Pattern.init(el);
75+
await utils.timeout(1); // wait a tick for async to settle.
76+
77+
expect(el.textContent).toBe("April Donnerstag 22. 2021, 4:00:00");
78+
});
79+
6280
it("Output formatted as local", async () => {
6381
document.body.innerHTML = `
6482
<time

0 commit comments

Comments
 (0)