|
1 |
| -# language-accepter |
2 |
| -Language Support Middleware for Express Framework |
| 1 | +# General Information About the Package |
| 2 | + |
| 3 | +The package aims to make it easy to build language support for API systems. By using this package, you can provide i18n support to your API systems easily and quickly. The package is used to provide language support to Sci-Hook systems and is regularly updated by the Sci-Hook Development Team. |
| 4 | + |
| 5 | +# How to Install Package? |
| 6 | + |
| 7 | +It is possible to install the package directly to your project folder via npm. For this, you must come to the terminal where you run your project and run the npm install command below: |
| 8 | + |
| 9 | +`$ npm install language-accepter` |
| 10 | + |
| 11 | +After running the command, the latest version of the package will automatically start downloading to the "node_modules" folder. Please wait until npm service completes the process. |
| 12 | + |
| 13 | +# How to Use the Package? |
| 14 | + |
| 15 | +To include the package in your project, simply write the following Typescript import code: |
| 16 | + |
| 17 | +```ts |
| 18 | +import {languageParser} from 'language-accepter'; |
| 19 | +``` |
| 20 | + |
| 21 | +The added function named `languageParser` is an Express middleware. This middleware receives the configuration specified as an object in it. Its most basic usage is as follows: |
| 22 | + |
| 23 | +```ts |
| 24 | +app.use(languageParser({ |
| 25 | + "accepted-languages": ["en","tr"], |
| 26 | + "default-language": "en" |
| 27 | +})); |
| 28 | +``` |
| 29 | + |
| 30 | +The `accepted-languages` value you see here is the section where you have defined the languages you have supported in your system. The `default-language` value is the language that will be automatically redirected if any of the languages supported by the client are not sent. For example: If the `default-language` value is set to `en` and the `fr` value is sent that is not in the client system, the client will be automatically redirected to the `en` language. |
| 31 | + |
| 32 | +Below are all the values and their descriptions: |
| 33 | + |
| 34 | +| Value | Description | |
| 35 | +| ------------- | ------------------------------ | |
| 36 | +| `accepted-languages` | An object list of languages accepted by the system. | |
| 37 | +| `default-language` | The default language assigned to the system. | |
| 38 | +| `autocreate-lanuage-cookie` | Creates a language cookie on the client side. | |
| 39 | +| `language-info-locations` | A list of names of the values containing the languages sent by the client. | |
| 40 | + |
| 41 | +A working example where all features are used: |
| 42 | + |
| 43 | +```ts |
| 44 | +import * as express from 'express'; |
| 45 | +import {languageParser} from 'language-accepter'; |
| 46 | + |
| 47 | +var app = express(); |
| 48 | + |
| 49 | +app.use(languageParser({ |
| 50 | + "accepted-languages":["en","tr"], |
| 51 | + "default-language":"en", |
| 52 | + "autocreate-lanuage-cookie":{ |
| 53 | + name:"location", |
| 54 | + expires:20000 |
| 55 | + }, |
| 56 | + "language-info-locations":[ |
| 57 | + "headers.location", |
| 58 | + "cookies.location" |
| 59 | + ] |
| 60 | +})); |
| 61 | + |
| 62 | +app.use((req,res) => { |
| 63 | + res.send("Your language:" + res.lang); |
| 64 | +}) |
| 65 | + |
| 66 | +app.listen(80); |
| 67 | +``` |
| 68 | + |
| 69 | +- If one of the values specified in `language-info-locations` contains one of the languages specified in `accepted-languages` and is sent, this will suppress the languages specified in the `Accept-Language` header. |
| 70 | + |
| 71 | +- The `autocreate-language-cookie` property ensures that the language detected by the system is stored in a cookie on the client. The value assigned to `name` names this cookie. The value `expires` determines the expiration time of this cookie. |
| 72 | + |
| 73 | +- The language information detected by `languageParser` is passed to the next middleware with the value `res.lang`. |
| 74 | + |
| 75 | +## Assigning as Config File |
| 76 | + |
| 77 | +### index.ts |
| 78 | + |
| 79 | +```ts |
| 80 | +import * as express from 'express'; |
| 81 | +import {languageParser} from 'language-accepter'; |
| 82 | + |
| 83 | +var app = express(); |
| 84 | + |
| 85 | +app.use(languageParser('language')); |
| 86 | + |
| 87 | +app.use((req,res) => { |
| 88 | + res.send("Your language:" + res.lang); |
| 89 | +}) |
| 90 | + |
| 91 | +app.listen(80); |
| 92 | +``` |
| 93 | + |
| 94 | +### language.json |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "accepted-languages":["en","tr"], |
| 99 | + "default-language":"en", |
| 100 | + "autocreate-lanuage-cookie":{ |
| 101 | + "name":"location", |
| 102 | + "expires":20000 |
| 103 | + }, |
| 104 | + "language-info-locations":[ |
| 105 | + "headers.location", |
| 106 | + "cookies.location" |
| 107 | + ] |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +It can also be configured with a JSON by giving the name of the above JSON file in `languageParser`. |
| 112 | + |
| 113 | +# Contributors |
| 114 | + |
| 115 | +This software package is developed and provided by the Sci-Hook Organization. It is actively developed by [Emirhan Gerçeker](https://github.com/lim10tech). |
| 116 | + |
| 117 | +# License |
| 118 | + |
| 119 | +This package is made available as open-source under the [MIT License](https://github.com/Sci-Hook/language-accepter/blob/main/LICENSE). It is open for development, modification and free use. You can use the package in your personal or organizational projects, you don't need permission from anywhere. Attribution to the developers and Sci-Hook is appreciated but not legally required. |
0 commit comments