Skip to content

Commit

Permalink
Merge pull request #542 from nextcloud/enh/provide-translation-func
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Jan 5, 2023
2 parents b12de9e + 6d66daf commit 2f69bdc
Show file tree
Hide file tree
Showing 7 changed files with 746 additions and 155 deletions.
121 changes: 121 additions & 0 deletions lib/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/// <reference types="@nextcloud/typings" />

declare var window: Nextcloud.v24.WindowWithGlobals

/**
* Get the first day of the week
*
* @return {number}
*/
export function getFirstDay(): number {
if (typeof window.firstDay === 'undefined') {
console.warn('No firstDay found')
return 1
}

return window.firstDay
}

/**
* Get a list of day names (full names)
*
* @return {string[]}
*/
export function getDayNames(): string[] {
if (typeof window.dayNames === 'undefined') {
console.warn('No dayNames found')
return [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
]
}

return window.dayNames
}

/**
* Get a list of day names (short names)
*
* @return {string[]}
*/
export function getDayNamesShort(): string[] {
if (typeof window.dayNamesShort === 'undefined') {
console.warn('No dayNamesShort found')
return ['Sun.', 'Mon.', 'Tue.', 'Wed.', 'Thu.', 'Fri.', 'Sat.']
}

return window.dayNamesShort
}

/**
* Get a list of day names (minified names)
*
* @return {string[]}
*/
export function getDayNamesMin(): string[] {
if (typeof window.dayNamesMin === 'undefined') {
console.warn('No dayNamesMin found')
return ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
}

return window.dayNamesMin
}

/**
* Get a list of month names (full names)
*
* @return {string[]}
*/
export function getMonthNames(): string[] {
if (typeof window.monthNames === 'undefined') {
console.warn('No monthNames found')
return [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
}

return window.monthNames
}

/**
* Get a list of month names (short names)
*
* @return {string[]}
*/
export function getMonthNamesShort(): string[] {
if (typeof window.monthNamesShort === 'undefined') {
console.warn('No monthNamesShort found')
return [
'Jan.',
'Feb.',
'Mar.',
'Apr.',
'May.',
'Jun.',
'Jul.',
'Aug.',
'Sep.',
'Oct.',
'Nov.',
'Dec.',
]
}

return window.monthNamesShort
}
154 changes: 2 additions & 152 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,152 +1,2 @@
/// <reference types="@nextcloud/typings" />

declare var OC: Nextcloud.v16.OC | Nextcloud.v17.OC | Nextcloud.v18.OC | Nextcloud.v19.OC |
Nextcloud.v20.OC | Nextcloud.v21.OC | Nextcloud.v22.OC | Nextcloud.v23.OC |
Nextcloud.v24.OC;
declare var window: Nextcloud.v16.WindowWithGlobals | Nextcloud.v17.WindowWithGlobals | Nextcloud.v18.WindowWithGlobals | Nextcloud.v19.WindowWithGlobals;

/**
* Returns the user's locale
*/
export function getLocale(): string {
return document.documentElement.dataset.locale || 'en'
}

export function getCanonicalLocale(): string {
return getLocale().replace(/_/g, '-')
}

/**
* Returns the user's language
*/
export function getLanguage(): string {
return document.documentElement.lang || 'en'
}

interface TranslationOptions {
escape?: boolean
}

/**
* Translate a string
*
* @param {string} app the id of the app for which to translate the string
* @param {string} text the string to translate
* @param {object} vars map of placeholder key to value
* @param {number} number to replace %n with
* @param {object} [options] options object
* @return {string}
*/
export function translate(app: string, text: string, vars?: object, count?: number, options?: TranslationOptions): string {
if (typeof OC === 'undefined') {
console.warn('No OC found')
return text
}

return OC.L10N.translate(app, text, vars, count, options)
}

/**
* Translate a plural string
*
* @param {string} app the id of the app for which to translate the string
* @param {string} textSingular the string to translate for exactly one object
* @param {string} textPlural the string to translate for n objects
* @param {number} count number to determine whether to use singular or plural
* @param {Object} vars of placeholder key to value
* @param {object} options options object
* @return {string}
*/

export function translatePlural(app: string, textSingular: string, textPlural: string, count: number, vars?: object, options?: TranslationOptions): string {
if (typeof OC === 'undefined') {
console.warn('No OC found')
return textSingular
}

return OC.L10N.translatePlural(app, textSingular, textPlural, count, vars, options)
}

/**
* Get the first day of the week
*
* @return {number}
*/
export function getFirstDay(): number {
if (typeof window.firstDay === 'undefined') {
console.warn('No firstDay found')
return 1
}

return window.firstDay
}

/**
* Get a list of day names (full names)
*
* @return {string[]}
*/
export function getDayNames(): string[] {
if (typeof window.dayNames === 'undefined') {
console.warn('No dayNames found')
return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
}

return window.dayNames
}

/**
* Get a list of day names (short names)
*
* @return {string[]}
*/
export function getDayNamesShort(): string[] {
if (typeof window.dayNamesShort === 'undefined') {
console.warn('No dayNamesShort found')
return ['Sun.', 'Mon.', 'Tue.', 'Wed.', 'Thu.', 'Fri.', 'Sat.']
}

return window.dayNamesShort
}

/**
* Get a list of day names (minified names)
*
* @return {string[]}
*/
export function getDayNamesMin(): string[] {
if (typeof window.dayNamesMin === 'undefined') {
console.warn('No dayNamesMin found')
return ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
}

return window.dayNamesMin
}

/**
* Get a list of month names (full names)
*
* @return {string[]}
*/
export function getMonthNames(): string[] {
if (typeof window.monthNames === 'undefined') {
console.warn('No monthNames found')
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
}

return window.monthNames
}

/**
* Get a list of month names (short names)
*
* @return {string[]}
*/
export function getMonthNamesShort(): string[] {
if (typeof window.monthNamesShort === 'undefined') {
console.warn('No monthNamesShort found')
return ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']
}

return window.monthNamesShort
}
export * from './translation'
export * from './date'
109 changes: 109 additions & 0 deletions lib/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
export type Translations = Record<string, string | undefined>
export type PluralFunction = (number: number) => number

declare var window: {
_oc_l10n_registry_translations: Record<string, Translations>
_oc_l10n_registry_plural_functions: Record<string, PluralFunction>
}

interface AppTranslations {
translations: Translations
pluralFunction: PluralFunction
}

/**
* Check if translations and plural function are set for given app
* @param {string} appId the app id
* @return {boolean}
*/
export function hasAppTranslations(appId: string) {
return (
window._oc_l10n_registry_translations?.[appId] !== undefined &&
window._oc_l10n_registry_plural_functions?.[appId] !== undefined
)
}

/**
* Register new, or extend available, translations for an app
* @param {string} appId the app id
* @param {object} translations the translations list
* @param {Function} pluralFunction the plural function
*/
export function registerAppTranslations(
appId: string,
translations: Translations,
pluralFunction: PluralFunction
) {
if (!hasAppTranslations(appId)) {
setAppTranslations(appId, translations, pluralFunction)
} else {
extendAppTranslations(appId, translations, pluralFunction)
}
}

/**
* Unregister all translations and plural function for given app
* @param {string} appId the app id
*/
export function unregisterAppTranslations(appId: string) {
delete window._oc_l10n_registry_translations[appId]
delete window._oc_l10n_registry_plural_functions[appId]
}

/**
* Get translations bundle for given app and current locale
* @param {string} appId the app id
* @return {object}
*/
export function getAppTranslations(appId: string): AppTranslations {
if (
typeof window._oc_l10n_registry_translations === 'undefined' ||
typeof window._oc_l10n_registry_plural_functions === 'undefined'
) {
console.warn('No OC L10N registry found')
return {
translations: {},
pluralFunction: (number: number) => number,
}
}

return {
translations: window._oc_l10n_registry_translations[appId] || {},
pluralFunction: window._oc_l10n_registry_plural_functions[appId],
}
}

/**
* Set new translations and plural function for an app
* @param {string} appId the app id
* @param {object} translations the translations list
* @param {Function} pluralFunction the plural function
*/
function setAppTranslations(
appId: string,
translations: Translations,
pluralFunction: PluralFunction
) {
window._oc_l10n_registry_translations[appId] = translations
window._oc_l10n_registry_plural_functions[appId] = pluralFunction
}

/**
* Extend translations for an app
* @param {string} appId the app id
* @param {object} translations the translations list
* @param {Function} [pluralFunction] the plural function (will override old value if given)
*/
function extendAppTranslations(
appId: string,
translations: Translations,
pluralFunction?: PluralFunction
) {
window._oc_l10n_registry_translations[appId] = Object.assign(
window._oc_l10n_registry_translations[appId],
translations
)
if (typeof pluralFunction === 'function') {
window._oc_l10n_registry_plural_functions[appId] = pluralFunction
}
}
Loading

0 comments on commit 2f69bdc

Please sign in to comment.