{"id":27194722,"url":"https://github.com/NGneers/signal-translate","last_synced_at":"2025-04-09T19:04:19.976Z","repository":{"id":247468745,"uuid":"825913374","full_name":"NGneers/signal-translate","owner":"NGneers","description":"Translation service that is using signals at its core.","archived":false,"fork":false,"pushed_at":"2025-04-05T13:43:23.000Z","size":491,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T13:45:16.388Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NGneers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-08T18:38:45.000Z","updated_at":"2025-04-05T13:42:27.000Z","dependencies_parsed_at":"2024-07-20T13:26:56.905Z","dependency_job_id":"4ef5c104-abaf-4aad-a69b-2a428054813e","html_url":"https://github.com/NGneers/signal-translate","commit_stats":null,"previous_names":["ngneers/signal-translate"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NGneers%2Fsignal-translate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NGneers%2Fsignal-translate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NGneers%2Fsignal-translate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NGneers%2Fsignal-translate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NGneers","download_url":"https://codeload.github.com/NGneers/signal-translate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094986,"owners_count":21046770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-09T19:02:29.043Z","updated_at":"2025-04-09T19:04:19.964Z","avatar_url":"https://github.com/NGneers.png","language":"TypeScript","funding_links":[],"categories":["Recently Updated","Development Utilities"],"sub_categories":["[Apr 08, 2025](/content/2025/04/08/README.md)","Internationalization"],"readme":"[![npm](https://img.shields.io/npm/v/@ngneers/signal-translate?color=%2300d26a\u0026style=for-the-badge)](https://www.npmjs.com/package/@ngneers/signal-translate)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/NGneers/signal-translate/build.yml?branch=main\u0026style=for-the-badge)](https://github.com/NGneers/signal-translate/actions/workflows/build.yml)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/@ngneers/signal-translate?color=%23FF006F\u0026label=Bundle%20Size\u0026style=for-the-badge)](https://bundlephobia.com/package/@ngneers/signal-translate)\n\n# signal-translate\n\nProvides an easy way to provide translations in the form of signals in an Angular application.\n\n## Motivation 💥\n\nSignals are a powerful way to provide state in an Angular application.\nThey are a great way to provide translations as well.\nUnfortunately, traditional translation libraries are not designed to work with signals and instead rely on Observables.\nAlso these libraries typically do not provide type safety for translations.\n\n## Features 🔥\n\n✅ Easy to setup and use\n\n✅ Type safe translations\n\n✅ ESM \u0026 CJS exports\n\nThis library provides a way to easily provide translations in the form of signals.\n\n## Built With 🔧\n\n- [TypeScript](https://www.typescriptlang.org/)\n\n## Usage Example 🚀\n\n```ts\nimport { Injectable, inject, signal } from '@angular/core';\nimport { BaseTranslateService, InterpolatePipe, interpolate } from '@ngneers/signal-translate';\n\nimport type translations from './en.json';\n\n@Injectable({ providedIn: 'root' })\nexport class TranslateService extends BaseTranslateService\u003c\n  // Provide the type of the translations object for one language\n  typeof translations\n\u003e {\n  constructor() {\n    super(\n      // Provide the available languages\n      ['en', 'de'],\n      // Provide the initial language (if null the browser language (navigator.language) is used)\n      'en'\n    );\n  }\n\n  protected loadTranslations(lang: string): Promise\u003ctypeof translations\u003e {\n    // Load the translations for the given language\n    switch (lang) {\n      case 'en':\n        return import('./en.json').then(x =\u003e x.default);\n      case 'de':\n        return import('./de.json').then(x =\u003e x.default);\n      default:\n        // Only languages that are in the available languages array are passed\n        // so this should never happen\n        throw new Error(`Language ${lang} is not supported`);\n    }\n  }\n}\n\n// Usage\n@Component({\n  selector: 'app-root',\n  standalone: true,\n  imports: [InterpolatePipe],\n  template: `\n    \u003c!-- Just call the signal for the wanted translation. --\u003e\n    \u003ch1\u003e{{ translations.hello() }}\u003c/h1\u003e\n\n    \u003c!--\n      You can also pass parameters to the signal to interpolate translations\n      `{{ name }}` in the translation will be replaced with the value of the name property\n    --\u003e\n    \u003ch2\u003e{{ translations.greetings({ name: 'John' }) }}\u003c/h2\u003e\n\n    \u003c!-- You can also use the interpolate pipe to interpolate strings --\u003e\n    \u003ch2\u003e{{ \"Hello {{name}}!\" | interpolate: { name: 'John' } }}\u003c/h2\u003e\n\n    \u003c!--\n      You can access translations unsafely with an unspecified string using the _unsafe property.\n      This is useful for translations that are not known at compile time.\n    --\u003e\n    \u003ch2\u003e{{ translations._unsafe[\"status_\" + status()]() }}\u003c/h2\u003e\n\n    \u003cbutton (click)=\"setLanguage('de')\"\u003eChange language to german\u003c/button\u003e\n  `,\n})\nexport class AppComponent {\n  private readonly _translateService = inject(TranslateService);\n\n  // Retrieve the translations signal\n  protected readonly translations = _translateService.translations;\n\n  protected readonly status = signal\u003cstring\u003e('vip');\n\n  public setLanguage(lang: string): void {\n    // Set the language\n    this._translateService.setLanguage(lang);\n  }\n\n  public greet(): void {\n    // You can interpolate translations by passing the values as the argument\n    // `{{ name }}` in the translation will be replaced with the value of the name property\n    alert(this.translations.greetings({ name: 'John' }));\n\n    // You can also interpolate strings using the interpolate function\n    alert(interpolate(\"Hello {{name}}!\", { name: 'John' }));\n  }\n}\n```\n\n## Strongly typed translations 📚\n\nThe IDE will provide autocompletion for the translations.\n![Autocompletion](docs/images/auto-complete.png)\n\n## Contributing 🧑🏻‍💻\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag \"enhancement\".\nDon't forget to give the project a star! Thanks again!\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n## License 🔑\n\nDistributed under the MIT License. See `LICENSE.txt` for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNGneers%2Fsignal-translate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNGneers%2Fsignal-translate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNGneers%2Fsignal-translate/lists"}