Test Failed
Push — develop ( 424bfb...c8c23d )
by Endre
02:45
created

src/Language/Translator.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 28
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 20
mnd 2
bc 2
fnc 1
dl 0
loc 28
bpm 2
cpm 3
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A Translator.translate 0 12 3
1
export interface IValueMap {
2
  [pattern: string]: string
3
}
4
5
export interface ILanguageData {
6
  [key: string]: string
7
}
8
9
export default class Translator {
10
  language: ILanguageData;
11
12
  constructor(languageData: ILanguageData) {
13
    this.language = languageData;
14
  }
15
16
  public translate(key: string, valueMap: IValueMap = {}) {
17
    if (this.language.hasOwnProperty(key)) {
18
      let translated = String(this.language[key]);
19
      for (let pattern in valueMap) {
20
        translated = translated.replace(new RegExp('{' + pattern + '}', 'g'), valueMap[pattern]);
21
      }
22
23
      return translated;
24
    }
25
26
    return key;
27
  }
28
}