-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TypeScript for translation service
- Loading branch information
Showing
14 changed files
with
63 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
server/http/application-detail/application-detail-presenter.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
server/http/local-catalog-list/local-catalog-list-presenter.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
export type Translations = Record<string, string | Function>; | ||
|
||
export interface TranslationService { | ||
translate: (serverMessage: string, args: any) => string; | ||
} | ||
|
||
/** | ||
* Create translation service from pairs or server local strings. | ||
*/ | ||
export function createTranslationService(serverToLocal: Translations) { | ||
return new DefaultTranslationService(serverToLocal); | ||
} | ||
|
||
class DefaultTranslationService implements TranslationService { | ||
|
||
readonly serverToLocal: Translations; | ||
|
||
constructor(serverToLocal: Translations) { | ||
this.serverToLocal = serverToLocal; | ||
} | ||
|
||
translate(serverMessage: string, args: any) { | ||
let result; | ||
const entry = this.serverToLocal[serverMessage]; | ||
// When given a function we do not care about anything else. | ||
if (entry instanceof Function) { | ||
return entry(args); | ||
} | ||
// We allow for simple "{}" substitution. | ||
if (Array.isArray(entry)) { | ||
// Initial value. | ||
result = entry[0][1]; | ||
for (let [separator, localizedMessage] of entry) { | ||
if (separator > args) { | ||
break; | ||
} | ||
result = localizedMessage; | ||
} | ||
} else { | ||
result = entry; | ||
if (result === undefined) { | ||
console.error("Missing localization entry.", { serverMessage }); | ||
result = ""; | ||
} | ||
} | ||
return result.replace("{}", args); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters