ngs-plural
Version:
Angular library for handling pluralization in different languages.
146 lines (99 loc) • 3.78 kB
Markdown
is a lightweight Angular library for handling pluralization in different languages using a pipe and a service. It simplifies the process of generating grammatically correct plural forms based on a numeric count and supports custom language rules.
- **[TranslatePluralPipe](
- **[PluralPipe](
- **[PluralizationService](
[ ](https://ngs-plural.onrender.com)
English, Russian, Spanish, French, Italian, German, Portuguese, Dutch, Swedish, Norwegian, Danish, Romanian, Catalan, Hungarian, Finnish, Turkish, Ukrainian, Belarusian, Serbian, Croatian, Bulgarian, Czech, Slovak, Slovene, Macedonian.
[+ Add a language](
[+ Request a new language via email](mailto:andrei.shpileuski.work@gmail.com)
- [Supported Languages](
- [Installation & Usage](
- [TranslatePluralPipe](
- [PluralPipe](
- [PluralizationService](
- [Contributing](
---
Install `ngs-plural` via npm:
```bash
npm install ngs-plural
```
JSON ({{lang}}.json):
```json
{ "APPLES": ["apple", "apples"] }
```
HTML:
```
{{ 1 | translatePlural: 'APPLES' }}
// output: 1 apple (used 'currentLanguage' from TranslateService)
{{ 5 | translatePlural: 'APPLES' }}
// output: 5 apples (used 'currentLanguage' from TranslateService)
{{ 0 | translatePlural: 'APPLES' : false }}
// output: apples (used 'currentLanguage' from TranslateService)
```
TypeScript:
```typescript
const appleWordForms = {
en: ["apple", "apples"],
ru: ["яблоко", "яблока", "яблок"],
//...
};
```
HTML:
```
{{ 0 | plural: { lang: 'en', forms: appleWordForms } }}
// output: 0 apples
{{ 0 | plural: { lang: 'en', forms: appleWordForms } : false }}
// output: apples
{{ 0 | plural: { lang: 'en', forms: [ "apple", "apples" ] } }}
// output: 0 apples
{{ 0 | plural: { lang: 'en', forms: [ "apple", "apples" ] } : false }}
// output: apples
{{ 0 | plural: { forms: appleWordForms } }}
// output: 0 apples (used 'en' by default)
{{ 0 | plural: { forms: appleWordForms } : false }}
// output: apples (used 'en' by default)
```
TypeScript:
```typescript
export class SomeComponent {
pluralizationService = inject(PluralizationService);
//// Static
resultStatic = this.pluralizationService.getPluralFormLocal(1, "en", APPLES, true);
//// Observable
resultObservable$: Observable<string> = combineLatest([this.language$, this.count$]).pipe(
map(([lang, count]) => {
return this.pluralizationService.getPluralFormLocal(count, lang, APPLES, true);
}),
);
//// Signal
resultSignal: Signal<string> = computed(() => {
return this.pluralizationService.getPluralFormLocal(this.count(), this.language(), APPLES, true);
});
}
```
[ ](https://ngs-plural.onrender.com)
---
To contribute or use the library in development mode, you can clone the repository and install dependencies.
1. Fork the repository
2. Clone the repo, install dependencies
```bash
git clone https://github.com/andrei-shpileuski/ngs-plural.git
```
```bash
cd ngs-plural
```
```bash
npm install
```
3. Create a new branch for your changes
4. Submit a pull request with a detailed description of the changes
`ngs-plural`