Skip to content

Commit

Permalink
fix(lint): Fix and unify indention to use tabs
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux committed Jan 12, 2023
1 parent 637a929 commit 9a8f441
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 358 deletions.
157 changes: 85 additions & 72 deletions lib/gettext.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
/**
* This module provides functionality to translate applications independent from Nextcloud
*
*
* @packageDocumentation
* @module @nextcloud/l10n/gettext
* @example
* ```js
import { getGettextBuilder } from '@nextcloud/l10n/gettext'
const gt = getGettextBuilder()
.detectLocale() // or use setLanguage()
.addTranslation(/* ... *\/)
.build()
.detectLocale() // or use setLanguage()
.addTranslation(/* ... *\/)
.build()
gt.gettext('some string to translate')
```
*/
import GetText from "node-gettext"
import GetText from 'node-gettext'

import { getLanguage } from "."
import { getLanguage } from '.'

/**
* @notExported
*/
class GettextBuilder {

private locale?: string
private translations = {}
private debug = false
private locale?: string
private translations = {}
private debug = false

setLanguage(language: string): GettextBuilder {
this.locale = language
return this
}
setLanguage(language: string): GettextBuilder {
this.locale = language
return this
}

/** Try to detect locale from context with `en` as fallback value */
detectLocale(): GettextBuilder {
return this.setLanguage(getLanguage().replace('-', '_'))
}
/** Try to detect locale from context with `en` as fallback value */
detectLocale(): GettextBuilder {
return this.setLanguage(getLanguage().replace('-', '_'))
}

addTranslation(language: string, data: any): GettextBuilder {
this.translations[language] = data
return this
}
addTranslation(language: string, data: object): GettextBuilder {
this.translations[language] = data
return this
}

enableDebugMode(): GettextBuilder {
this.debug = true
return this
}
enableDebugMode(): GettextBuilder {
this.debug = true
return this
}

build(): GettextWrapper {
return new GettextWrapper(this.locale || 'en', this.translations, this.debug)
}
build(): GettextWrapper {
return new GettextWrapper(this.locale || 'en', this.translations, this.debug)
}

}

Expand All @@ -59,50 +57,65 @@ class GettextBuilder {
*/
class GettextWrapper {

private gt: GetText

constructor(locale: string, data: any, debug: boolean) {
this.gt = new GetText({
debug,
sourceLocale: 'en',
})

for (let key in data) {
this.gt.addTranslations(key, 'messages', data[key])
}

this.gt.setLocale(locale)
}

private subtitudePlaceholders(translated: string, vars: Record<string, string | number>): string {
return translated.replace(/{([^{}]*)}/g, (a, b) => {
const r = vars[b]
if (typeof r === 'string' || typeof r === 'number') {
return r.toString()
} else {
return a
}
})
}

/** Get translated string (singular form), optionally with placeholders */
gettext(original: string, placeholders: Record<string, string | number> = {}): string {
return this.subtitudePlaceholders(
this.gt.gettext(original),
placeholders
)
}

/** Get translated string with plural forms */
ngettext(singular: string, plural: string, count: number, placeholders: Record<string, string | number> = {}): string {
return this.subtitudePlaceholders(
this.gt.ngettext(singular, plural, count).replace(/%n/g, count.toString()),
placeholders
)
}
private gt: GetText

constructor(locale: string, data: object, debug: boolean) {
this.gt = new GetText({
debug,
sourceLocale: 'en',
})

for (const key in data) {
this.gt.addTranslations(key, 'messages', data[key])
}

this.gt.setLocale(locale)
}

private subtitudePlaceholders(translated: string, vars: Record<string, string | number>): string {
return translated.replace(/{([^{}]*)}/g, (a, b) => {
const r = vars[b]
if (typeof r === 'string' || typeof r === 'number') {
return r.toString()
} else {
return a
}
})
}

/**
* Get translated string (singular form), optionally with placeholders
*
* @param original original string to translate
* @param placeholders map of placeholder key to value
*/
gettext(original: string, placeholders: Record<string, string | number> = {}): string {
return this.subtitudePlaceholders(
this.gt.gettext(original),
placeholders
)
}

/**
* Get translated string with plural forms
*
* @param singular Singular text form
* @param plural Plural text form to be used if `count` requires it
* @param count The number to insert into the text
* @param placeholders optional map of placeholder key to value
*/
ngettext(singular: string, plural: string, count: number, placeholders: Record<string, string | number> = {}): string {
return this.subtitudePlaceholders(
this.gt.ngettext(singular, plural, count).replace(/%n/g, count.toString()),
placeholders
)
}

}

/**
* Create a new GettextBuilder instance
*/
export function getGettextBuilder() {
return new GettextBuilder()
return new GettextBuilder()
}
Loading

0 comments on commit 9a8f441

Please sign in to comment.