typedoc-plugin-localization

Typedoc plugin which localize your documentation

Usage no npm install needed!

<script type="module">
  import typedocPluginLocalization from 'https://cdn.skypack.dev/typedoc-plugin-localization';
</script>

README

typedoc-plugin-localization

Build Status npm version

Inspired and used by Ignite UI for Angular

Plugin

A plugin for Typedoc

When using Typedoc 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 version@0.13.0 and above.

Arguments

  • --generate-json <path/to/jsons/directory/>
    Specifies the location where the JSON's should be written to.
  • --generate-from-json <path/to/generated/jsons>
    Specifies from where to get the loclized JSON's data.
  • --templateStrings <path/to/template-strings/json>
    Specifies the path to the JSON file which would contains your localized hardcoded template strings. Additional information could be found here
  • --localize <localizaiton key>
    Specifies your localization key which would match the translations section in your templateStrings file. Additional information could be found here
  • --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

<main-proj-dir> - This file has to contain the file structure of the project.
<json-exports-dir> - This file would contains all generated json files with retrieved comments.
<out-typedoc-dir> - 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 `<main-proj-dir>` --generate-json `<json-export-dir>`

We can use Ignite UI for 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.

{
    "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",
                "<igx-grid [data]=\"employeeData\" autoGenerate=\"false\">",
                "  <igx-column field=\"first\" header=\"First Name\"></igx-column>",
                "  <igx-column field=\"last\" header=\"Last Name\"></igx-column>",
                "  <igx-column field=\"role\" header=\"Role\"></igx-column>",
                "</igx-grid>",
                "```",
                ""
            ],
            "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",
                        "<igx-grid [data]=\"Data\" [autoGenerate]=\"true\"></igx-grid>",
                        "```"
                    ]
                }
            },
        ....
        },
        "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:

{
    "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 `<main-proj-dir>` --generate-from-json `<json-exports-dir>` --out `<out-typedoc-dir>`

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.

        <ul>
            {{#each sources}}
                {{#if url}}
                    <li><span>Defined in</span> <a href="{{url}}">{{fileName}}:{{line}}</a></li>
                {{else}}
                    <li><span>Defined in</span> {{fileName}}:{{line}}</li>
                {{/if}}
            {{/each}}
        </ul>

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:

    {
        localize-key: {
            "string": "translation"
        }
    }
    

    The localization key is your responsibility. You can name it however you like.

    In our case

    {
        jp: {
            "Defined in": "定義:"
        }
    }
    
  2. Update your partial view with the helper localize function.

        <ul>
            {{#each sources}}
                {{#if url}}
                    <li><span>{{#localize}}Defined in{{/localize}}</span> <a href="{{url}}">{{fileName}}:{{line}}</a></li>
                {{else}}
                    <li><span>{{#localize}}Defined in{{/localize}}</span> {{fileName}}:{{line}}</li>
                {{/if}}
            {{/each}}
        </ul>
    
  3. Execute the translation command which would contain the localization key and the path to the template strings file.
    For instance:

    npx typedoc `<main-proj-dir>` --localize `<localization-key>` --templateStrings <path/to/the/file>`
    

    In case of 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.