Coder Social home page Coder Social logo

typedoc-plugin-localization's Introduction

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 [email protected] 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. It takes up to two levels.
For instance when you have a /directory/inner-dir1/inner-dir2/file.ts it will create the following structure exports/directory/inner-dir1/ which > will contains all files that are under it or files that are deeply nested.

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.

typedoc-plugin-localization's People

Contributors

aleksandyr avatar zdrawku avatar cary-hu avatar damyanpetev avatar benzhai01 avatar zprodev avatar

Watchers

Raynor Chen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.