• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / a94de615-1a97-46e8-b07e-aa98619b3649

13 Nov 2024 12:34PM UTC coverage: 90.366% (-0.03%) from 90.398%
a94de615-1a97-46e8-b07e-aa98619b3649

push

circleci

minlovehua
feat: support i18n

5517 of 6751 branches covered (81.72%)

Branch coverage included in aggregate %.

67 of 76 new or added lines in 19 files covered. (88.16%)

55 existing lines in 10 files now uncovered.

13225 of 13989 relevant lines covered (94.54%)

1000.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.56
/src/i18n/i18n.service.ts
1
import { Injectable, inject } from '@angular/core';
2
import { IndexableObject, SafeAny } from 'ngx-tethys/types';
3
import { en_US, THY_I18N_LOCALE_KEY, ThyI18nLocale, zh_CN } from '.';
4

1✔
5
@Injectable({
6
    providedIn: 'root'
357✔
7
})
8
export class ThyI18nService {
9
    constructor() {
2,441✔
10
        this.setLocale(inject(THY_I18N_LOCALE_KEY, { optional: true }) || 'zh-CN');
2,441!
11
    }
2,441✔
12

16✔
13
    locale: ThyI18nLocale;
14

2,441✔
15
    translate(path: string, variablesMap?: SafeAny): string {
NEW
16
        let content = this.getLocaleByPath(this.locale, path) as string;
×
17
        if (typeof content === 'string') {
18
            if (variablesMap) {
19
                Object.keys(variablesMap).forEach(key => (content = content.replace(new RegExp(`{{ ${key} }}`, 'g'), variablesMap[key])));
57,991✔
20
            }
21
            return content;
22
        }
357✔
23
        return path;
357✔
24
    }
357!
NEW
25

×
26
    getLocaleKey(): string {
27
        return this.locale ? this.locale.key : '';
357!
28
    }
NEW
29

×
NEW
30
    setLocale(localeKey: string): void {
×
31
        const key = localeKey.toLowerCase();
NEW
32
        const currentLocaleKey = this.getLocaleKey().toLowerCase();
×
NEW
33

×
34
        if (currentLocaleKey === key) {
35
            return;
357✔
36
        }
37

38
        switch (key) {
39
            case 'zh-ch':
2,441✔
40
                this.locale = zh_CN;
2,441✔
41
                break;
2,441✔
42
            case 'en-us':
2,441✔
43
                this.locale = en_US;
2,441✔
44
                break;
4,882✔
45
            default:
46
                this.locale = zh_CN;
2,441!
47
        }
48
    }
1✔
49

50
    private getLocaleByPath(locale: IndexableObject, path: string): string | object | SafeAny {
1✔
51
        let result = locale;
52
        const paths = path.split('.');
53
        const depth = paths.length;
54
        let index = 0;
55
        while (result && index < depth) {
56
            result = result[paths[index++]];
57
        }
58
        return index === depth ? result : null;
59
    }
60
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc