{"id":13629621,"url":"https://github.com/kl1mm/localize","last_synced_at":"2025-07-22T23:04:51.733Z","repository":{"id":46194353,"uuid":"250501382","full_name":"kl1mm/localize","owner":"kl1mm","description":null,"archived":false,"fork":false,"pushed_at":"2025-06-06T12:28:36.000Z","size":319,"stargazers_count":42,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2025-07-22T07:35:10.521Z","etag":null,"topics":["json","localization","nuget"],"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/kl1mm.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,"zenodo":null}},"created_at":"2020-03-27T10:11:57.000Z","updated_at":"2025-07-15T01:53:33.000Z","dependencies_parsed_at":"2024-01-06T02:09:53.347Z","dependency_job_id":"644a996c-ff4d-4281-9dd5-799216f27c5b","html_url":"https://github.com/kl1mm/localize","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/kl1mm/localize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kl1mm%2Flocalize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kl1mm%2Flocalize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kl1mm%2Flocalize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kl1mm%2Flocalize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kl1mm","download_url":"https://codeload.github.com/kl1mm/localize/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kl1mm%2Flocalize/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266464424,"owners_count":23932925,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["json","localization","nuget"],"created_at":"2024-08-01T22:01:15.047Z","updated_at":"2025-07-22T23:04:51.697Z","avatar_url":"https://github.com/kl1mm.png","language":"C#","readme":"﻿# Localize\n\nSimple package to localize **strings** from json files via static source code generation.\n\nImplemented via [C# source generators](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview)\n\n[![CI\\CD](https://github.com/kl1mm/localize/actions/workflows/dotnet.yml/badge.svg?branch=main)](https://github.com/kl1mm/localize/actions/workflows/dotnet.yml)\n\n## Usage\n\n_Also see [**example project**](https://github.com/kl1mm/localize/tree/develop/example/kli.Localize.Example)_\n\n### Install the nuget package\n\nAdd a [Nuget package](https://www.nuget.org/packages/kli.Localize/) reference to the project file in the project you want to localize:\u003cbr\u003e\n\n`\u003cPackageReference Include=\"kli.Localize\" Version=\"\u003cversion\u003e\" /\u003e`\n\n### Create \\*.json files for your localized texts.\n\nExample:\n\n```json\n{\n    \"SampleText\": \"FooBar\",\n    \"Other\": \"Text42\"\n}\n```\n\nGive your default localization a name **without** specifying the culture (e.g. `Locale.json`). All other localizations follow the pattern `\u003cFilename\u003e_\u003cCultureInfo.Name\u003e.json` (e.g. `Locale_en-US.json` for American English or `Locale_en.json` for English)\n\n![locale_files image][locale_files]\n\n### Add json files to csproj\n\nIn an `ItemGroup` in your csproj file add an `AdditionFiles` element for **each default localization** json file. Set the `Include` attribute to the path of the file.\n\nExample:\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n    \u003cItemGroup\u003e\n        \u003cPackageReference Include=\"kli.Localize\" Version=\"1.0.*\" /\u003e\n\n        \u003cAdditionalFiles Include=\"TestLocalizations\\Locale.json\" /\u003e\n    \u003c/ItemGroup\u003e\n\u003c/Project\u003e\n```\n\nThis means: if you have a `Locale.json` and a `Locale_en-US.json` you **only** have to add the `Locale.json` as `\u003cAdditionalFiles\u003e`. You can add as many files as you want.\n\n### Use it in your code\n\nNow you should be able to locate the generated source code in your project under Dependencies/Analyzers.\u003cbr\u003e\n_Of course you can also view and debug the generated source code._\u003cbr\u003e\n\n![generated_1 image][generated_1]\n\u003cbr\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eGenerated code example\u003c/summary\u003e\n\n```csharp\n//------------------------------------------------------------------------------\n// \u003cauto-generated\u003e\n//     This code was generated by kli.Localize.Generator.\n//\n//     Changes to this file may cause incorrect behavior and will be lost if\n//     the code is regenerated.\n// \u003c/auto-generated\u003e\n//------------------------------------------------------------------------------\nnamespace kli.Localize.Example.Localizations\n{\n    using System;\n    using System.Globalization;\n    using System.Collections.Generic;\n    using Translations = System.Collections.Generic.Dictionary\u003cstring, string\u003e;\n\n    public sealed class Locale\n    {\n        private static readonly LocalizationProvider provider = new LocalizationProvider();\n        public static IDictionary\u003cstring, string\u003e GetAll(CultureInfo cultureInfo = null) =\u003e provider.GetValues(cultureInfo ?? CultureInfo.CurrentUICulture);\n        public static string GetString(string key, CultureInfo cultureInfo = null) =\u003e provider.GetValue(key, cultureInfo ?? CultureInfo.CurrentUICulture);\n        ///\u003csummary\u003eSimilar to: Hallo Welt (German)\u003c/summary\u003e\n        public static string MyText =\u003e provider.GetValue(nameof(MyText), CultureInfo.CurrentUICulture);\n        private class LocalizationProvider\n        {\n            delegate bool SelectorFunc\u003cT\u003e(Translations translations, out T arg);\n            internal string GetValue(string key, CultureInfo cultureInfo)\n            {\n                bool ValueSelector(Translations translations, out string value)\n                {\n                    if (translations.TryGetValue(key, out value))\n                        return true;\n                    value = key;\n                    return false;\n                }\n\n                return TraverseCultures\u003cstring\u003e(cultureInfo, ValueSelector);\n            }\n\n            internal IDictionary\u003cstring, string\u003e GetValues(CultureInfo cultureInfo)\n            {\n                bool ValueSelector(Translations translations, out Translations value)\n                {\n                    value = translations;\n                    return true;\n                }\n\n                return TraverseCultures\u003cTranslations\u003e(cultureInfo, ValueSelector);\n            }\n\n            private T TraverseCultures\u003cT\u003e(CultureInfo cultureInfo, SelectorFunc\u003cT\u003e selectorFunc)\n            {\n                if (resources.TryGetValue(cultureInfo, out Translations translations))\n                {\n                    if (selectorFunc(translations, out T result) || cultureInfo == CultureInfo.InvariantCulture)\n                        return result;\n                }\n\n                return TraverseCultures\u003cT\u003e(cultureInfo.Parent, selectorFunc);\n            }\n\n            private static readonly Translations invariant = new()\n            {{\"MyText\", \"Hallo Welt (German)\"}, };\n            private static readonly Translations en = new()\n            {{\"MyText\", \"Hello World (English)\"}, };\n            private static readonly Dictionary\u003cCultureInfo, Translations\u003e resources = new()\n            {{CultureInfo.InvariantCulture, invariant}, {new CultureInfo(\"en\"), en}, };\n        }\n    }\n}\n```\n\n\u003c/details\u003e\n\n\u003cbr\u003e\n\nImport the namespace where you put your \\*.json files and use the generated code to access your localizations.\u003cbr\u003e\nAccess is based on [CultureInfo.CurrentUICulture](https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentuiculture)\n\u003cbr\u003e\u003cbr\u003e\n![useit image][useit]\n\n### Namespace generation\n\nThe namespace is generated using the following pattern:\u003cbr\u003e\n`rootnamespace + relative directory structure`\n\u003cbr\u003e\nSince v0.8 this behaviour can be overriden [see 'From version 0.8'](#From version 0.8)\n\n## Version Changes\n\n### From version 1.0\n\n#### BREAKING - Ignore none JSON-String/Object values\nAll properties that are not string or object will be ignored. \n```json\n{\n    \"Number\": 4.2,\n    \"Bool\": true,\n    \"Null\": null,\n    \"Array\": [1,2,3]\n}\n```\n\n#### [Add Support for Nested Classes #8](https://github.com/kl1mm/localize/issues/8)\nIt is now possible to use JSON objects in the localization files.\nDuring generation, the structure is mapped as a nested class for access\n\n```json\n{\n    \"SomeText\": \"some text\",\n    \"Sub\": \n    {\n        \"FileNotFound\": \"Not found\",\n        \"DivideByZero\": \"x / zero\"\n    },\n    \"UI\":{\n        \"LabelOne\": \"One\",\n        \"LabelTwo\": \"Two\",\n        \"Login\": {\n            \"LabelUserName\": \"User\",\n            \"LabelPassword\": \"Pass\"\n        }\n    }\n}\n```\n#### Improved Diagnostics\n - SGL0001: InvalidJsonFileFormat - `\u003cJsonReaderException.Message\u003e`\n - SGL0002: InvalidJsonPropertyName - `Json property key must be a valid C# identifier`\n - SGL0003: InvalidJsonTokenType - `Json property value must be an object or a string`\n\nAll diagnostics came with LinePostion (linenumber \u0026 column)\n\n### From version 0.8\n\nIt is now possible to override the namespace and the class/file name that will be generated:\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n    \u003cItemGroup\u003e\n        \u003cPackageReference Include=\"kli.Localize\" Version=\"0.8.*\" /\u003e\n\n        \u003cAdditionalFiles Include=\"Localizations\\Locale.json\" \n                         NamespaceName=\"Namespace.of.your.choice\"\n                         ClassName=\"MyClassName\" /\u003e\n    \u003c/ItemGroup\u003e\n\u003c/Project\u003e\n```\nFrom which the following is generated:\n```csharp\nnamespace Namespace.of.your.choice\n{\n    ...\n    public sealed class MyClassName {\n    ...\n```\n\n## Help! Why is no code generated?\n\nDirectly after including the package sometimes the tooling (Visual Studio) gets stuck. If you encounter any problems with source generation try to restart Visual Studio and/or check the build log for warnings/errors.\n\n## Need help? Problems?\n\nFeel free to create an [Issue](https://github.com/kl1mm/localize/issues)\n\n[locale_files]: docs/locale_files.png\n[generated_1]: docs/generated_1.png\n[useit]: docs/useit.png\n","funding_links":[],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["Localization","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkl1mm%2Flocalize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkl1mm%2Flocalize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkl1mm%2Flocalize/lists"}