An open API service indexing awesome lists of open source software.

https://github.com/igniteui/typedoc-plugin-localization


https://github.com/igniteui/typedoc-plugin-localization

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

## typedoc-plugin-localization
[![Build Status](https://travis-ci.org/IgniteUI/typedoc-plugin-localization.svg?branch=master)](https://travis-ci.org/IgniteUI/typedoc-plugin-localization)
[![npm version](https://badge.fury.io/js/typedoc-plugin-localization.svg)](https://badge.fury.io/js/typedoc-plugin-localization)

## Inspired and used by [Ignite UI for Angular](https://github.com/IgniteUI/igniteui-angular)

### Plugin

A plugin for [Typedoc](http://typedoc.org)

When using [Typedoc](http://typedoc.org) for API docs generation you may want to generate documentation with different languages.

By using this plugin you will be able to:
1. Merge all code comments(classes, methods, properties, enumerations etc.) that needs localization in a couple of json files.
2. Translate them.
3. Use the updated files to build a documentation for an entire project in the desired language.

### Installing

```
npm install typedoc-plugin-localization --save-dev
```

### Usage

#### Important notes
> Please take in mind that you are running your local npm packages by `npx` right before the command execution.
>

> The alternative would be to install the plugin globally with `-g` at the end of the command.
>

> Then you won't need to use `npx`.

> The plugin works with [typedoc](https://github.com/TypeStrong/typedoc) version@0.13.0 and above.

#### Arguments
* `--generate-json `

Specifies the location where the JSON's should be written to.
* `--generate-from-json `

Specifies from where to get the loclized JSON's data.
* `--templateStrings `

Specifies the path to the JSON file which would contains your localized hardcoded template strings. Additional information could be found [here](#additional-information)
* `--localize `

Specifies your localization key which would match the translations section in your templateStrings file. Additional information could be found [here](#additional-information)
* `--tags`

Include all commented tags into json exports. For instance /* @memberof Class */
* `--params`

Include all commented parameters into json exports. For instace /* @param option Options describing your settings. */

#### Path variable descriptions
`` - This file has to contain the file structure of the project.


`` - This file would contains all generated json files with retrieved comments.


`` - The directory where the documentation have to be generated

#### Step 1
In order to generate the json representation of each module of your application you will have to execute the command below:
```
npx typedoc `` --generate-json ``
```

We can use [Ignite UI for Angular](https://github.com/IgniteUI/igniteui-angular) repository for Example:

```
npx typedoc projects\igniteui-angular\src\ --generate-json exports
```

Folder `exports` will be automatically created

> This command will create `exports` folder.
>

>`projects\igniteui-angular\src\` This directory contains the file structure of the project.
>

> For instance when you have a `/directory/inner-dir1/inner-dir2/file.ts` it will create the following structure `exports/directory/inner-dir1/inner-dir2/` which will contains all files that are under it.

#### Step 2

After the export of the JSON files finished, you should modify the comments in the desired language.

```JSON
{
"IgxGridComponent": {
"comment": {
"text": [
"The Ignite UI Grid is used for presenting and manipulating tabular data in the simplest way possible. Once data",
"has been bound, it can be manipulated through filtering, sorting & editing operations.",
"",
"Example:",
"```html",
"",
" ",
" ",
" ",
"",
"```",
""
],
"shortText": [
"**Ignite UI for Angular Grid** -",
"[Documentation](https://www.infragistics.com/products/ignite-ui-angular/angular/components/grid.html)"
]
},
"properties": {
"data": {
"comment": {
"shortText": [
"An @Input property that lets you fill the `IgxGridComponent` with an array of data.",
"```html",
"",
"```"
]
}
},
....
},
"methods": {
"findNext": {
"comment": {
"parameters": {
"text": {
"comment": {
"text": "the string to search."
}
},
"caseSensitive": {
"comment": {
"text": "optionally, if the search should be case sensitive (defaults to false)."
}
},
"exactMatch": {
"comment": {
"text": "optionally, if the text should match the entire value (defaults to false)."
}
}
},
"tags": {
"memberof": {
"comment": {
"text": "IgxGridBaseComponent",
"tagName": "memberof"
}
}
},
"shortText": [
"Finds the next occurrence of a given string in the grid and scrolls to the cell if it isn't visible.",
"Returns how many times the grid contains the string.",
"```typescript",
"this.grid.findNext(\"financial\");",
"```"
]
}
}
....
}
....
```

The structure of every file has the following representation:
```JSON
{
"className": {
"properties": {},
"methods": {},
"accessors": {}
}
}
```

> If a current file does not contain any comments that have to be exported from the TypeDoc, it won't exists into the section with json files.

> Every directory that would represents all generated JSON's would contains a file `globalFunctions.json`. There you could find all exported global functions witin your application. Please take in mind that if such a funcion doesn't contain any sort of comment it won't be exported!

#### Step 3

When you finish with the translations you will have to generate the documentation with the transleted files `(json's)`.


So the following command have to be executed:
```
npx typedoc `` --generate-from-json `` --out ``
```

Example:
```
npx typedoc .\projects\igniteui-angular\src\ --generate-from-json .\exports\ --out localized
```

Folder `localized` will be automatically created.

#### Additional Information

For your convenience we have exposed a way to localize your hardcoded template strings. How?
We have registered a helper function within our plugin which brings you the ability to achieve this. How to use it?
Let's take an example of a `partial view`.

```html


    {{#each sources}}
    {{#if url}}

  • Defined in {{fileName}}:{{line}}

  • {{else}}

  • Defined in {{fileName}}:{{line}}

  • {{/if}}
    {{/each}}

```
Here we would like to translate "Defined in" hardcoded string.

1. Create a file somewhere in your app which would contains all your definitions of hordcoded strings you would like to translate.
The structure of the file should be like this:
```json
{
localize-key: {
"string": "translation"
}
}
```
> The localization key is your responsibility. You can name it however you like.

In our case
```json
{
jp: {
"Defined in": "定義:"
}
}
```

2. Update your `partial view` with the helper `localize` function.
```html


    {{#each sources}}
    {{#if url}}

  • {{#localize}}Defined in{{/localize}} {{fileName}}:{{line}}

  • {{else}}

  • {{#localize}}Defined in{{/localize}} {{fileName}}:{{line}}

  • {{/if}}
    {{/each}}

```

3. Execute the translation command which would contain the `localization key` and the `path` to the `template strings` file.
For instance:
```
npx typedoc `` --localize `` --templateStrings `
```
In case of [igniteui-angular](https://github.com/IgniteUI/igniteui-angular) it would be:
```
npx typedoc projects\igniteui-angular\src\ --localize jp --templateStrings ./extras/template/strings/shell-strings.json
```
You can see how our template strings file looks like here [./extras/template/strings/shell-strings.json](https://github.com/IgniteUI/igniteui-angular/blob/master/extras/template/strings/shell-strings.json).

#### Link and Debug
In order to run the plugin locally, after it is build, it should be linked to the repo you want to use it.

1. Execute the following commands

```
npm run build
```

```
npm pack
```

```
cp typedoc-plugin-localization-1.0.4.tgz ~
```

2. Go to the theme repository where the plugin is included as peer dependency and add the reference to the packed file.
Example:

```
"peerDependecies": {
"typedoc-plugin-localization": "file:../../../typedoc-plugin-localization-1.0.4.tgz",
}
```

3. Go to the repository which is using the theme and plugin.

```
npm install ~/typedoc-plugin-localization-1.0.4.tgz
```

4. Add launch.json configuration to enable debbuging

Example:
```
{
"configurations": [
{
"name": "Typedoc plugin",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/typedoc/bin/typedoc",
"args": [
"${workspaceFolder}/projects/igniteui-angular/src/public_api.ts",
"--generate-json",
"${workspaceFolder}/dist",
"--localize",
"en",
"--versioning",
"--product",
"ignite-ui-angular",
"--tsconfig",
"${workspaceFolder}/tsconfig.json"
],
"sourceMaps": true,
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
]
}
]
}
```