{"id":23733795,"url":"https://github.com/fmazzant/localization","last_synced_at":"2026-02-04T01:41:38.982Z","repository":{"id":41476465,"uuid":"310006010","full_name":"fmazzant/Localization","owner":"fmazzant","description":"A library for .NET that can help you to manage the localization in your application. The library includes Wpf extension, Xamarin extension and Mvc extension.","archived":false,"fork":false,"pushed_at":"2024-05-08T20:40:30.000Z","size":1905,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-30T17:18:39.262Z","etag":null,"topics":["c-sharp","culture","dotnet-standard","localization","localizationmanager","mvc","nuget","wpf","wpf-application","wpf-window","xamarin","xamarin-forms"],"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/fmazzant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2020-11-04T13:09:06.000Z","updated_at":"2024-05-08T20:40:34.000Z","dependencies_parsed_at":"2024-05-12T00:15:21.621Z","dependency_job_id":null,"html_url":"https://github.com/fmazzant/Localization","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmazzant%2FLocalization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmazzant%2FLocalization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmazzant%2FLocalization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmazzant%2FLocalization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fmazzant","download_url":"https://codeload.github.com/fmazzant/Localization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231945870,"owners_count":18449946,"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","culture","dotnet-standard","localization","localizationmanager","mvc","nuget","wpf","wpf-application","wpf-window","xamarin","xamarin-forms"],"created_at":"2024-12-31T05:19:37.473Z","updated_at":"2026-02-04T01:41:38.927Z","avatar_url":"https://github.com/fmazzant.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Localization\n\nA library for .net that can help you to manage the localization in your application.\n\nTo use it in your project, Mafe.Localization NuGet package represents a base library.\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization)\n\nUsing Wpf Windows it is necesary installing the Mafe.Localization.Windows library:\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization.Windows?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization.Windows)\n\nUsing Xamarin it is possible installing the Mafe.Localization.Xamarin library:\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization.Xamarin?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization.Xamarin)\n\nUsing ASP.NET MVC it is possible installing the Mafe.Localization.Mvc library:\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization.Mvc?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization.Mvc)\n\nUsing Blazor it is possible installing the Mafe.Localization.Blazor library:\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization.Blazor?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization.Blazor)\n\nUsing .NET MAUI it is possible installing the Mafe.Localization.Maui library:\n\n[![Nuget](https://img.shields.io/nuget/v/Mafe.Localization.Maui?style=flat-square)](https://www.nuget.org/packages/Mafe.Localization.Maui)\n\n\n# Version \u003e= 2.0.0\nThe version 2.0.0 changed the Assembly's name. So is necessary adding \"Mafe.\" where is necessary.\n\nXamarin project and WPF proejct needs this upgrade.\n\nTo Xamarin:\n```c# xaml\n xmlns:culture=\"clr-namespace:Localization.Xamarin;assembly=Mafe.Localization.Xamarin\"\n```\nTo WPF:\n```c# xaml\nxmlns:culture=\"clr-namespace:Localization.Windows;assembly=Mafe.Localization.Windows\"\n```\n\n# Architecture\nThe following image represets a localization manager diagram:\n\n![alt Localization Manager Diagram](art/diagram.png)\n\nActually there are two extenesion of library: Wpf Window and Xamarin App. \nThe library can be used without extentions but using the Localization Manager \nInstance directly.\n\n# LocalizationManager\n\nLocalizationManager is more important component of this library. The library manages\nthe current culture and it provides to load a vocabolary in culture.\n\n### Init\n\nThe Init provides to initilize the LocalizationManager. There two kind: Initializing\nwithout culture (takes the culture from main thread) and with a specific culture.\n\nThe initilizing without culture like this:\n\n```c#\nLocalizationManager.Init(new MockVocabolaryServiceProvider { });\n```\n\nThe inizializing with the culture like this:\n\n```c#\nLocalizationManager.Init(new MockVocabolaryServiceProvider { }, new CultureInfo(\"en-US\"));\n```\n\nThe inizializing library is necessary to load and configure the vocabolary.\n\n### SetCulture\n\nThe SetCulture changes the current culture, like this:\n\n```c#\nLocalizationManager.SetCulture(new CultureInfo(\"en-US\"));\n```\n\n### Translate without default value\n\nGet the resource's value we are using the following code:\n\n```c#\nvar label = LocalizationManager.Instance[\"resourceKey\"];\n```\n\nThe result can be null.\n\n### Translate with default value\n\nIt is possible get the resource's value with a Translate method, like this:\n\n```c#\nvar label = LocalizationManager.Instance.Translate(\"resourceKey\",\"#Default value\");\n```\n\nIf resource's value doesn't exests the function returns the Default value \npassed as argument.\n\n# Vocabolary Service Provider\n\nThe following code defines the vocabolary service provider where all vacabolaries \nare defined or where the vocabolaries are loaded. \n\nInside the Provider is possbile to manage a cache.\n\nA sample is represented into following code:\n\n```c# xaml\npublic class MockVocabolaryServiceProvider : IVocabolaryServiceProvider\n{\n    Dictionary\u003cstring, Vocabolary\u003e all = null;\n\n    public Task AddOrUpdateTermAsync(IVocabolary vocabolary, string key, string defaultValue = null) \n        =\u003e Task.Delay(1);\n\n    public Task Initialize()\n    {\n        all = new Dictionary\u003cstring, Vocabolary\u003e\n        {\n            {\"it-IT\", new Vocabolary {\n                    { \"MainWindow\",\"Main Window in italian language\" }\n                }\n            },\n            {\"en-US\", new Vocabolary {\n                    { \"MainWindow\",\"Main Window in english language\" } \n                } \n            }\n        };\n        return Task.FromResult(all);\n    }\n\n    public Task\u003cIVocabolary\u003e LoadVocabolaryAsync(CultureInfo cultureInfo)\n    {\n        string cultureDefault = \"it-IT\";\n        if (all.ContainsKey(cultureInfo.ToString()))\n            cultureDefault = cultureInfo.ToString();\n        return Task.FromResult\u003cIVocabolary\u003e(all[cultureDefault]);\n    }\n\n    public Task SaveAsync(IVocabolary vocabolary) \n        =\u003e Task.Delay(1);\n}\n```\n\n# Xamarin\n\nThe Xamarin has a specific library to manage the culture inside the project. \n\nIn first time is necessary initializing the LocalizationManager. Like this:\n```c#\nLocalizationManager.Init(new MockVocabolaryServiceProvider { });\n```\nInside your xaml file is necessary include the Localization.Xamarin library, like this:\n```c# xaml\n xmlns:culture=\"clr-namespace:Localization.Xamarin;assembly=Mafe.Localization.Xamarin\"\n```\nIn the following code is displayed how the Label's text works:\n```c# xaml\n\u003cLabel Text=\"{culture:Translate MainWindow, DefaultValue='Main Window'}\"\u003e\u003c/Label\u003e\n```\nIn this case, when the culture changed, the LocalizationManager changing the\nLabel's Text value with the current culture value.\n\nUsing this code to invoke a func when the culture is changed, like this:\n```c# xaml\nlabel.Translate(Label.TextProperty, () =\u003e\n{\n    string val = doSomething();\n    return val;\n});\n```\nwhere label is a BindableObject.\n\n# .NET MAUI\n\nThe .NET MAUI has a specific library to manage the culture inside the project. \n\nIn first time is necessary initializing the LocalizationManager. Like this:\n```c#\nvar builder = MauiApp.CreateBuilder();\nbuilder\n    .UseMauiApp\u003c...\u003e()\n    .ConfigureLocalization(new MockVocabolaryServiceProvider { })\n    ...;\n```\nor you can initializing the LocalizationManager like Xamarin:\n```c#\nLocalizationManager.Init(new MockVocabolaryServiceProvider { });\n```\nInside your xaml file is necessary include the Localization.Maui library, like this:\n```c# xaml\n xmlns:culture=\"clr-namespace:Localization.Maui;assembly=Mafe.Localization.Maui\"\n```\nIn the following code is displayed how the Label's text works:\n```c# xaml\n\u003cLabel Text=\"{culture:Translate MainWindow, DefaultValue='Main Window'}\"\u003e\u003c/Label\u003e\n```\nIn this case, when the culture changed, the LocalizationManager changing the\nLabel's Text value with the current culture value.\n\nUsing this code to invoke a func when the culture is changed, like this:\n```c# xaml\nlabel.Translate(Label.TextProperty, () =\u003e\n{\n    string val = doSomething();\n    return val;\n});\n```\nwhere label is a BindableObject.\n\n# Migration Xamarin to .NET MAUI\n\nTo migrate from Xamarin to .NET MAUI is very easy. It's necessary to rename within Xaml file the include definition, \nlike this.\n\nfrom\n\n```c# xaml\n xmlns:culture=\"clr-namespace:Localization.Xamarin;assembly=Mafe.Localization.Xamarin\"\n```\n\nto\n\n```c# xaml\n xmlns:culture=\"clr-namespace:Localization.Maui;assembly=Mafe.Localization.Maui\"\n```\n\nAfter you can build and run again your app. Clearly you need to install the package Mafe.Localization.Maui and remove\nMafe.Localization.Xamarin from your project.\n\n# WPF Windows\n\nThe Wpf Windows has a specific library to manage the culture inside the project. \n\nIn first time is necessary initializing the LocalizationManager. Like this:\n\n```c#\nLocalizationManager.Init(new MockVocabolaryServiceProvider { });\n```\n\nInside your xaml file is necessary include the Localization.Windows library, like this:\n\n```c# xaml\nxmlns:culture=\"clr-namespace:Localization.Windows;assembly=Mafe.Localization.Windows\"\n```\n\nIn the following code is displayed how the Label's text works:\n\n```c# xaml\n\u003cLabel Content=\"{culture:Translate MainWindow, DefaultValue='Main Window'}\"\u003e\u003c/Label\u003e\n```\n\nIn this case, when the culture changed, the LocalizationManager changing the\nLabel's Text value with the current culture value.\n\nUsing this code to invoke a func when the culture is changed, like this:\n```c# xaml\nlabel.Translate(Label.TextProperty, () =\u003e\n{\n    string val = doSomething();\n    return val;\n});\n```\nwhere label is a FrameworkElement.\n\n# ASP.NET MVC\n\nThe ASP.NET MVC has a specific library to manage the culture inside the project. \n\nIn first time we configure the localizationManager inside the Startup.cs:\n\n```c#\nusing Localization.Mvc.Extensions;\n...\npublic void ConfigureServices(IServiceCollection services)\n{\n    ... \n    services.AddMvcLocalizationManager(options =\u003e {\n        options.ServiceProvider = new MockVocabolaryServiceProvider { };\n        options.Culture = new CultureInfo(\"it-IT\");\n    });\n    ...\n}\n```\nThe \"options.Culture\" value represents the initilized culture.\n\nThen we define the ViewModel using the TranslateAttribute, like this:\n\n```c#\npublic class MyViewModel\n{\n    [Translate(ResourceKey = \"NameText\", DefaultValue = \"#Name\")]\n    public string Name { get; set; }\n\n    [Translate(ResourceKey = \"LastNameText\", DefaultValue = \"#LastName\")]\n    public string LastName { get; set; }\n\n    [Translate(ResourceKey = \"AddressText\", DefaultValue = \"#Address\")]\n    public string Address { get; set; }\n}\n```\nNow we using the ViewModel inside the view, like this:\n```c#\n\u003cp\u003e\n    @Html.TranslateLabelFor(m =\u003e m.Name)\n    @Html.Label(\"Name\", Model.Name)\n\u003c/p\u003e\n\u003cp\u003e\n    @Html.TranslateLabelFor(m =\u003e m.LastName)\n    @Html.Label(\"LastName\", Model.LastName)\n\u003c/p\u003e\n\u003cp\u003e\n    @Html.TranslateLabelFor(m =\u003e m.Address)\n    @Html.Label(\"Address\", Model.Address)\n\u003c/p\u003e\n```\nIt is possible using the TranslateLabel directly, like this:\n```c#\n\u003cp\u003e\n     @Html.TranslateLabel(\"LabelTitle\", \"ResourceKey\", \"#Default Value of Label\")\n\u003c/p\u003e\n```\n# Blazor\n\nThe Blazor has a specific library to manage the culture inside the project. \n\nIn first time we configure the localizationManager inside the Startup.cs:\n\n```c#\nusing Localization.Blazor.Extensions;\n...\npublic void ConfigureServices(IServiceCollection services)\n{\n    ... \n    services.AddBlazorLocalizationManager(options =\u003e {\n        options.ServiceProvider = new MockVocabolaryServiceProvider { };\n        options.Culture = new CultureInfo(\"it-IT\");\n    });\n    ...\n}\n```\nThe \"options.Culture\" value represents the initilized culture.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmazzant%2Flocalization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffmazzant%2Flocalization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmazzant%2Flocalization/lists"}