Skip to content

Commit

Permalink
Merge pull request #546 from nextcloud/fix/documentaton
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Jan 6, 2023
2 parents e14c786 + 2b7c218 commit 059b02e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Deploy documentation
on:
push:
branches:
- master

permissions:
contents: write

jobs:
build-and-deploy:
concurrency: ci-${{ github.ref }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up node
uses: actions/setup-node@v3
with:
node-version-file: 'package.json'

- name: Install dependencies & build
run: |
npm ci
npm run build:doc
- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: dist/doc
branch: gh-pages
32 changes: 29 additions & 3 deletions lib/gettext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
/**
* 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()
gt.gettext('some string to translate')
```
*/
import GetText from "node-gettext"

import { getLanguage } from "."

/**
* @notExported
*/
class GettextBuilder {

private locale?: string
Expand All @@ -13,6 +33,7 @@ class GettextBuilder {
return this
}

/** Try to detect locale from context with `en` as fallback value */
detectLocale(): GettextBuilder {
return this.setLanguage(getLanguage().replace('-', '_'))
}
Expand All @@ -33,6 +54,9 @@ class GettextBuilder {

}

/**
* @notExported
*/
class GettextWrapper {

private gt: GetText
Expand All @@ -50,7 +74,7 @@ class GettextWrapper {
this.gt.setLocale(locale)
}

private subtitudePlaceholders(translated: string, vars: object): string {
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') {
Expand All @@ -61,14 +85,16 @@ class GettextWrapper {
})
}

gettext(original: string, placeholders: object = {}): string {
/** 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
)
}

ngettext(singular: string, plural: string, count: number, placeholders: object = {}): string {
/** 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
Expand Down
15 changes: 15 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
/**
* This module provides all functions for the `OC.L10N` namespace
*
* @packageDocumentation
* @module @nextcloud/l10n
* @example
* ```js
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
console.log(t('my-app', 'Hello {name}', { name: 'J. Doe' }));
const count = 2;
console.warn(n('my-app', 'Got an error', 'Got multiple errors', 2));
```
*/

export * from './translation'
export * from './date'
1 change: 1 addition & 0 deletions lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { generateFilePath } from '@nextcloud/router'
import DOMPurify from 'dompurify'
import escapeHTML from 'escape-html'

/** @notExported */
interface TranslationOptions {
/** enable/disable auto escape of placeholders (by default enabled) */
escape?: boolean
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"scripts": {
"build": "tsc && tsc --project tsconfig.cjs.json",
"build:doc": "typedoc --excludeNotExported --mode file --out dist/doc lib && touch dist/doc/.nojekyll",
"build:doc": "typedoc && touch dist/doc/.nojekyll",
"check-types": "tsc --noEmit",
"check-es-compat": "npm run build && check-es-compat dist/*.js",
"dev": "tsc --watch",
Expand All @@ -43,6 +43,7 @@
"@nextcloud/browserslist-config": "^2.3.0",
"@nextcloud/typings": "^1.0.0",
"@types/node-gettext": "^3.0",
"@zamiell/typedoc-plugin-not-exported": "^0.2.0",
"check-es-compat": "^2.2.0",
"gettext-parser": "^6.0.0",
"jest": "^29.3.1",
Expand Down
10 changes: 10 additions & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://typedoc.org/schema.json",
"entryPoints": ["./lib/index.ts", "./lib/gettext.ts"],
"out": "dist/doc",
"excludePrivate": true,
"navigationLinks": {
"GitHub": "https://github.com/nextcloud/nextcloud-l10n/",
"Nextcloud": "https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html"
}
}

0 comments on commit 059b02e

Please sign in to comment.