|
1 | 1 | // vim: ts=4 et sts=4 sw=4
|
2 | 2 |
|
3 |
| -import 'intl-pluralrules'; |
4 |
| -import { FluentBundle, FluentResource } from '@fluent/bundle/compat'; |
5 |
| -import { FluentParser, lineOffset, columnOffset, Resource } |
6 |
| - from '@fluent/syntax/compat'; |
| 3 | +import { FluentBundle, FluentResource } from "@fluent/bundle/compat"; |
| 4 | +import { |
| 5 | + FluentParser, |
| 6 | + lineOffset, |
| 7 | + columnOffset, |
| 8 | + Resource, |
| 9 | +} from "@fluent/syntax/compat"; |
7 | 10 |
|
8 | 11 | const fluent_parser = new FluentParser();
|
9 | 12 |
|
10 | 13 | export function annotation_display(source, junk, annot) {
|
11 |
| - const { code, message, span: { start } } = annot; |
| 14 | + const { |
| 15 | + code, |
| 16 | + message, |
| 17 | + span: { start }, |
| 18 | + } = annot; |
12 | 19 |
|
13 |
| - const slice = source.substring(junk.span.start, junk.span.end); |
14 |
| - const line_offset = lineOffset(source, start); |
15 |
| - const column_offset = columnOffset(source, start); |
16 |
| - const span_offset = lineOffset(source, junk.span.start); |
17 |
| - const head_len = line_offset - span_offset + 1; |
18 |
| - const lines = slice.split('\n'); |
19 |
| - const head = lines.slice(0, head_len).join('\n') + '\n'; |
20 |
| - const tail = lines.slice(head_len).join('\n'); |
| 20 | + const slice = source.substring(junk.span.start, junk.span.end); |
| 21 | + const line_offset = lineOffset(source, start); |
| 22 | + const column_offset = columnOffset(source, start); |
| 23 | + const span_offset = lineOffset(source, junk.span.start); |
| 24 | + const head_len = line_offset - span_offset + 1; |
| 25 | + const lines = slice.split("\n"); |
| 26 | + const head = lines.slice(0, head_len).join("\n") + "\n"; |
| 27 | + const tail = lines.slice(head_len).join("\n"); |
21 | 28 |
|
22 |
| - return { |
23 |
| - code, |
24 |
| - message, |
25 |
| - line_offset, |
26 |
| - column_offset, |
27 |
| - head, |
28 |
| - tail |
29 |
| - } |
| 29 | + return { |
| 30 | + code, |
| 31 | + message, |
| 32 | + line_offset, |
| 33 | + column_offset, |
| 34 | + head, |
| 35 | + tail, |
| 36 | + }; |
30 | 37 | }
|
31 | 38 |
|
32 | 39 | export function parse_translations(translations) {
|
33 |
| - try { |
34 |
| - var res = fluent_parser.parse(translations); |
35 |
| - } catch (err) { |
36 |
| - console.error(err); |
37 |
| - return [ |
38 |
| - new Resource([]), |
39 |
| - [] |
40 |
| - ]; |
41 |
| - } |
| 40 | + try { |
| 41 | + var res = fluent_parser.parse(translations); |
| 42 | + } catch (err) { |
| 43 | + console.error(err); |
| 44 | + return [new Resource([]), []]; |
| 45 | + } |
42 | 46 |
|
43 |
| - const junks = res.body.filter(entry => entry.type === "Junk"); |
44 |
| - const annotations = junks.reduce( |
45 |
| - (annots, junk) => annots.concat( |
46 |
| - junk.annotations.map( |
47 |
| - annot => annotation_display(translations, junk, annot) |
48 |
| - ) |
49 |
| - ), |
50 |
| - [] |
51 |
| - ); |
52 |
| - return [res, annotations]; |
| 47 | + const junks = res.body.filter((entry) => entry.type === "Junk"); |
| 48 | + const annotations = junks.reduce( |
| 49 | + (annots, junk) => |
| 50 | + annots.concat( |
| 51 | + junk.annotations.map((annot) => |
| 52 | + annotation_display(translations, junk, annot) |
| 53 | + ) |
| 54 | + ), |
| 55 | + [] |
| 56 | + ); |
| 57 | + return [res, annotations]; |
53 | 58 | }
|
54 | 59 |
|
55 | 60 | export function create_bundle(locale, translations) {
|
56 |
| - const bundle = new FluentBundle(locale); |
57 |
| - bundle.addResource(new FluentResource(translations)); |
58 |
| - return bundle; |
| 61 | + const bundle = new FluentBundle(locale); |
| 62 | + bundle.addResource(new FluentResource(translations)); |
| 63 | + return bundle; |
59 | 64 | }
|
60 | 65 |
|
61 | 66 | export function format_messages(ast, bundle, externals) {
|
62 |
| - const outputs = new Map(); |
63 |
| - const errors = []; |
64 |
| - for (let entry of ast.body) { |
65 |
| - if (entry.type !== 'Message') { |
66 |
| - continue; |
67 |
| - } |
68 |
| - const id = entry.id.name; |
69 |
| - const message = bundle.getMessage(id); |
70 |
| - if (message.value) { |
71 |
| - let output = bundle.formatPattern(message.value, externals, errors); |
72 |
| - outputs.set(id, output); |
73 |
| - } |
| 67 | + const outputs = new Map(); |
| 68 | + const errors = []; |
| 69 | + for (let entry of ast.body) { |
| 70 | + if (entry.type !== "Message") { |
| 71 | + continue; |
74 | 72 | }
|
75 |
| - return [outputs, errors]; |
| 73 | + const id = entry.id.name; |
| 74 | + const message = bundle.getMessage(id); |
| 75 | + if (message.value) { |
| 76 | + let output = bundle.formatPattern(message.value, externals, errors); |
| 77 | + outputs.set(id, output); |
| 78 | + } |
| 79 | + } |
| 80 | + return [outputs, errors]; |
76 | 81 | }
|
77 | 82 |
|
78 | 83 | export function parse_externals(externals) {
|
79 |
| - try { |
80 |
| - return [JSON.parse(externals), []]; |
81 |
| - } catch (err) { |
82 |
| - return [{}, [err]]; |
83 |
| - } |
| 84 | + try { |
| 85 | + return [JSON.parse(externals), []]; |
| 86 | + } catch (err) { |
| 87 | + return [{}, [err]]; |
| 88 | + } |
84 | 89 | }
|
0 commit comments