{"id":25831434,"url":"https://github.com/sfmohassel/libc.translation","last_synced_at":"2025-02-28T20:33:53.364Z","repository":{"id":55151148,"uuid":"237749326","full_name":"sfmohassel/libc.translation","owner":"sfmohassel","description":"A library to help you implement translation using json files to replace .resx files in .net standard projects","archived":false,"fork":false,"pushed_at":"2024-11-21T20:35:25.000Z","size":81,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-21T21:28:22.680Z","etag":null,"topics":["c-sharp","csharp","culture","i18n","i18n-csharp","json-files","net-standard","resx-files","translation-text"],"latest_commit_sha":null,"homepage":"","language":"C#","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/sfmohassel.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}},"created_at":"2020-02-02T09:46:38.000Z","updated_at":"2024-11-21T20:35:41.000Z","dependencies_parsed_at":"2023-12-03T15:35:52.161Z","dependency_job_id":null,"html_url":"https://github.com/sfmohassel/libc.translation","commit_stats":null,"previous_names":["sfmohassel/libc.translation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sfmohassel%2Flibc.translation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sfmohassel%2Flibc.translation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sfmohassel%2Flibc.translation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sfmohassel%2Flibc.translation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sfmohassel","download_url":"https://codeload.github.com/sfmohassel/libc.translation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241226941,"owners_count":19930489,"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":["c-sharp","csharp","culture","i18n","i18n-csharp","json-files","net-standard","resx-files","translation-text"],"created_at":"2025-02-28T20:33:53.214Z","updated_at":"2025-02-28T20:33:53.357Z","avatar_url":"https://github.com/sfmohassel.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libc.translation\n\nA library to help you implement translation using json files to replace .resx files in .net standard projects\n\n## Breaking and important changes\n\n- From version 3.0.0 onwards the library only depends on [System.Text.Json](https://www.nuget.org/packages/System.Text.Json).\n- From version 5.0.0 onwards `LocalizationSource` is renamed to `JsonLocalizationSource` to better reflect the purpose of the class. From this version we support nested objects (look below the example)\n\n## Understand the code\n\nYou can read this [article](https://sfmohassel.medium.com/c-how-to-use-a-simple-form-of-i18n-instead-of-resource-files-resx-files-26eec9460a88) to understand how the code works.\n\n## Why we need this?\n\nHave you ever got tired of using .resx files in visual studio?\n\u003cbr/\u003e\nDo you want to have i18n json files for localizing your projects?\n\u003cbr/\u003e\nDo you hate sattelite assemblies alongside your application files like me?\n\u003cbr/\u003e\nIf yes, then this is for you :-)\n\n## How does it work?\n\n- Add [this nuget package](https://www.nuget.org/packages/libc.translation/)\n- Then we need an `ILocalizationSource` instance. This instance can be created using the default `JsonLocalizationSource` class.\n\u003cbr/\u003e\n\n`JsonLocalizationSource` class enables us to load a json file containing all our translations from three different sources:\n\n  - A file on disk\n  \n  ```csharp\n  ILocalizationSource source = new JsonLocalizationSource(new FileInfo(\"\u003cpath to json file\u003e\"), PropertyCaseSensitivity.CaseSensitive);\n  ```\n  \n  - An stream object\n  \n  ```csharp\n  Stream stream = new FileInfo(\"\u003cpath to json file\u003e\").OpenRead();\n  ILocalizationSource source = new JsonLocalizationSource(stream, PropertyCaseSensitivity.CaseSensitive);\n  ```\n  \n  - An embedded json file in assembly\n  \n  ```csharp\n  Assembly assembly = Assembly.GetExecutingAssembly();\n  string resourceId = \"libc.translations.tests.embedded.json\";\n  ILocalizationSource source = new JsonLocalizationSource(assembly, resourceId, PropertyCaseSensitivity.CaseSensitive);\n  ```\n\n- Then we need an `ILocalizer` instance. This instance can be created using the default `Localizer` class.\n\u003cbr/\u003e\n\n`Localizer` class enables us to pass an `ILocalizationSource` object and a __fallback culture__ (which defaults to \"en\" value).\n\n```csharp\nILocalizer localizer = new Localizer(source, \"en\");\n```\n\n- Now there are some methods to obtain desired translation text using a culture and a key.\n\n## Example\n\nSuppose we have created an i18n json file and a localizer with English as the fallback language like this:\n\n```csharp\nILocalizer localizer = new Localizer(source, \"en\");\n```\n\n```json\n{\n  \"ar\": {\n    \"InvalidInput\": \"إدخال غير صالح\",\n    \"UnknownError\": \"خطأ غير معروف {0}\",\n    \"home-page\": {\n      \"title\": \"أهلا بك\",\n      \"body\": {\n        \"text\": \"بعض النصوص\"\n      } \n    } \n  },\n  \"de\": {\n    \"InvalidInput\": \"Ungültige Eingabe\",\n    \"UnknownError\": \"Unbekannter Fehler {0}\",\n    \"home-page\": {\n      \"title\": \"Willkommen\",\n      \"body\": {\n        \"text\": \"Etwas Text\"\n      }\n    }\n  },\n  \"en\": {\n    \"InvalidInput\": \"Invalid input\",\n    \"UnknownError\": \"Unknown error {0}\",\n    \"home-page\": {\n      \"title\": \"Welcome\",\n      \"body\": {\n        \"text\": \"Some text\"\n      }\n    }\n  }\n}\n```\n\n- Set current thread's culture (this is actually not needed):\n\n```csharp\nCultureInfo.CurrentCulture = new CultureInfo(\"ar\");\n```\n\n- Get a translation text for a culture and key. If the text for the given culture is not found, fallback culture is used:\n\n```csharp\nvar text = localizer.Get(\"es\", \"InvalidInput\");\n// text is (English is our fallback culture): Invalid input\n```\n\n- Get a translation text for a key and thread's current culture. If the text for the given culture is not found, fallback culture is used:\n\n```csharp\nvar text = localizer.Get(\"InvalidInput\");\n// text is (thread culture is \"ar\"): إدخال غير صالح\n```\n\n- Get a formatted translation text for a culture and key. If the text for the given culture is not found, fallback culture is used:\n\n```csharp\nvar text = localizer.GetFormat(\"de\", \"unknownerror\", \"!!!\");\n// text is: Unbekannter Fehler !!!\n```\n\n- Get a formatted translation text for a key and thread's current culture. If the text for the given culture is not found, fallback culture is used:\n\n```csharp\nvar text = localizer.GetFormat(\"unknownerror\", \"!!!\");\n// text is: خطأ غير معروف !!!\n```\n\n- Get a nested translation text for a key:\n\n```csharp\nvar text = localizer.Get(\"de\", \"home-page.title\");\n// text is: Willkommen\n\ntext = localizer.Get(\"de\", \"home-page.body.text\");\n// text is: Etwas Text\n```\n\n## Contribution\n\nThis repository belongs to the community and will always appreciate any contribution\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsfmohassel%2Flibc.translation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsfmohassel%2Flibc.translation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsfmohassel%2Flibc.translation/lists"}