{"id":17788621,"url":"https://github.com/frederikstonge/fstonge.aspnetcore","last_synced_at":"2025-04-02T00:36:36.063Z","repository":{"id":64398149,"uuid":"243438771","full_name":"frederikstonge/fstonge.AspNetCore","owner":"frederikstonge","description":"Library I made to automatically translate routes in asp.net core for .net 5, .net 6 and .net 7.","archived":false,"fork":false,"pushed_at":"2022-12-07T20:02:48.000Z","size":123,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-27T08:37:04.885Z","etag":null,"topics":["asp-net-core","localization","translation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frederikstonge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-27T05:29:10.000Z","updated_at":"2024-03-29T17:41:06.000Z","dependencies_parsed_at":"2023-01-02T12:31:35.820Z","dependency_job_id":null,"html_url":"https://github.com/frederikstonge/fstonge.AspNetCore","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frederikstonge%2Ffstonge.AspNetCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frederikstonge%2Ffstonge.AspNetCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frederikstonge%2Ffstonge.AspNetCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frederikstonge%2Ffstonge.AspNetCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frederikstonge","download_url":"https://codeload.github.com/frederikstonge/fstonge.AspNetCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246735380,"owners_count":20825222,"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":["asp-net-core","localization","translation"],"created_at":"2024-10-27T10:20:27.418Z","updated_at":"2025-04-02T00:36:36.006Z","avatar_url":"https://github.com/frederikstonge.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# ASP.NET Core Utilities\n\n## Route translations\n\n### How to use it:\n#### In Startup.cs\nUnder ConfigureServices, you need to add the following:\n```c#\npublic void ConfigureServices(IServiceCollection services)\n{\n    var builder = services.AddControllersWithViews(options =\u003e\n    {\n        // Add filter to set current filter in cookies\n        options.AddCultureCookieFilter();\n    });\n    \n    services.AddLocalization(options =\u003e options.ResourcesPath = \"Resources\");\n    builder.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);\n    builder.AddDataAnnotationsLocalization();\n\n    // Inject required services, add routing and replace current LinkGenerator\n    services.AddRoutingLocalization(_configuration);\n}\n```\nUnder Configure, you need to add the following:\n```c#\npublic void Configure(IApplicationBuilder app)\n{\n    // Setup Request localization, Rewriter and Routing\n    app.UseRoutingLocalization();\n    app.UseStaticFiles();\n    \n    // Setup Endpoints with culture\n    app.UseEndpointsLocalization();\n}\n```\n\n#### In appsettings.json\nYou need to add the following structure in your appsettings:\n```json\n{\n  \"RoutingTranslation\":\n  {\n    \"SupportedCultures\": \"fr, en\",\n    \"DefaultCulture\": \"fr\"\n  }\n}\n```\n\n#### Attribute in Controllers\n```c#\n[Translate(\"en\", \"orders\")]\n[Translate(\"fr\", \"commandes\")]\npublic class OrdersController : Controller\n{\n    [Translate(\"fr\", \"liste\")]\n    public IActionResult List()\n    {\n        return View();\n    }\n}\n```\n\n#### Generate your first urls\n```c#\n\u003cform asp-controller=\"orders\" asp-action=\"list\"\u003e\u003c/form\u003e\n\n\u003ca asp-controller=\"products\" asp-action=\"detail\" asp-route-id=\"12\"\u003e\u003c/a\u003e\n\n\u003ca asp-route-culture=\"fr\"\u003eFrench\u003c/a\u003e\n```\n\n### Options\nYou can inject an IOptions that contains your configured cultures. Use the following:\n```c#\nIOptions\u003cRequestLocalizationOptions\u003e options\n```\n\n### Custom route translation\nYou can create your own custom validation by deriving from ICustomTranslation:\n```c#\npublic class ProductTranslation : ICustomTranslation\n{    \n    public string ControllerName =\u003e \"products\";\n    \n    public string ActionName =\u003e \"detail\";\n    \n    public RewriteRule[] RewriteRules =\u003e new[]\n    {\n        new RewriteRule(\n            @\"^([-a-zA-Z]+)\\/([^\\/]+)\\/[-\\/a-zA-Z0-9]+\\/p-([=._a-zA-Z0-9]+)-.*$\",\n            \"$1/$2/detail/$3\")\n    };\n    \n    public string GenerateUrlPath(RouteValueDictionary values, FragmentString fragment)\n    {\n        return \"/\" +\n           $\"{values.GetParameterValue(RouteValue.Culture)}/\" +\n           $\"{values.GetParameterValue(RouteValue.Controller)}/\" + \n           \"10-control-and-testing/\" +\n           \"14-testing-string/\" +\n           $\"p-{values.GetParameterValue(RouteValue.Id)}-testing-product-string\";\n    }\n}\n```\nThen add it as a scoped in Startup.cs:\n```c#\n// Add custom translations as singleton\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddScoped\u003cICustomTranslation, ProductTranslation\u003e();\n}\n```\n\n## Async Distribued Session\n\n### How to use it:\n#### In Startup.cs\nUnder ConfigureServices, you need to add the following:\n```c#\npublic void ConfigureServices(IServiceCollection services)\n{\n    var builder = services.AddControllersWithViews();\n    \n    // Store temp data in session\n    builder.AddSessionStateTempDataProvider();\n\n    // Add any distributed cache (this is in memory, but I can be Redis or others)\n    services.AddDistributedMemoryCache();\n\n    // Use the package to use the async distributed session\n    services.AddAsyncDistributedSession();\n}\n```\nUnder Configure, you need to add the following:\n```c#\npublic void Configure(IApplicationBuilder app)\n{\n    \n    // Setup async distributed session\n    app.UseAsyncDistributedSession();\n\n    app.UseEndpoints();\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrederikstonge%2Ffstonge.aspnetcore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrederikstonge%2Ffstonge.aspnetcore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrederikstonge%2Ffstonge.aspnetcore/lists"}