


This article mainly introduces the Asp.Net Core MVC project implementation multi-language instance (Globalization/Localization), with It has a certain reference value. If you are interested, you can check it out. I just recently implemented a multi-language function for a
RazorMVC project, called Globalization, Localization, whatever. The final effect to be achieved is to switch the language of the entire site with one click, and only one set of pages needs to be written during development. Let’s get to the point
First, we need to create a CultureConfigurer class to manage localization resources and complete the "translation" link:
Here I used
Static class, and then execute the Init() method when the MVC project StartUp is actually a bit stupid. Of course, you can also write an interface first and then use dependency injection to create a singleton . using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
namespace Localization
{
public enum Culture
{
Cn,
En
}
public static class CultureConfigurer
{
private static Dictionary<string, string> _enDictionary;
private static Dictionary<string, string> _cnDictionary;
public static void Init()
{
var assembly = Assembly.Load(new AssemblyName("Localization"));
var resourceNames = assembly.GetManifestResourceNames();
foreach (var resourceName in resourceNames)
{
if (resourceName.EndsWith("en-US.json") || resourceName.EndsWith("zh-CN.json"))
{
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
Dictionary<string, string> localizationDictionary =
JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
if (resourceName.EndsWith("en-US.json"))
{
_enDictionary = localizationDictionary;
}
else
{
_cnDictionary = localizationDictionary;
}
}
}
}
}
}
}
public static string GetValue(string key, Culture culture)
{
switch (culture)
{
case (Culture.Cn):
{
if (_cnDictionary.ContainsKey(key))
{
return _cnDictionary[key];
}
else
{
return $"[{key}]";
}
}
case (Culture.En):
{
if (_enDictionary.ContainsKey(key))
{
return _enDictionary[key];
}
else
{
return $"[{key}]";
}
}
default:
{
return $"[{key}]";
}
}
}
}
}
There are a few points to note here:
1. The enum class Culture is used to represent the language to be implemented. Here I just simply implemented Chinese and English (I don’t understand the others), The corresponding CultureConfigurer class has two Dictionary
in Chinese and English. 2. Use Assembly.Load to load the assembly. The parameter is your own assembly name. I just wrote a
# here. ## 3. I chose the json file for the resource file, also for the convenience of calling in js. Of course, you can also usexml
or any format you want to use. You only need to adjust the parsing method and load the file content. Just go to the corresponding Dictionary4. When you see the GetValue method
, I believe everyone has understood it. In fact, it is multi-language, no matter what language it is, it uses a certain word. key, and then call this method to "translate" into words in the current language. For example, if "Open" is used as the Key, then there should be a KeyValuePair in the Chinese Dictionary that is "Open": "Open", and the corresponding English should have an "Open": "Open", then when Culture is in Chinese, the display will be "Open" means "Open" in English."embed": { "include": [ "Localization/SourceFiles/*.json" ] }If it is VS2017, it is a csproj file, then right-click the resource file to be added, select "
Properties
", change the configuration to "All configurations", and change the "Generate operation" in the advanced configuration properties to " Embedded resources", as shown below:At this point, we have written the core class to implement localization, and now we need to solve the problem of how to display it on the page:
using System; using Microsoft.AspNetCore.Mvc.Razor; using Localization; namespace MVC.Views { public abstract class MyRazorPage<TModel> : RazorPage<TModel> { public virtual string L(string source) { var value = Context.Request.Cookies["culture"]; Culture c; if (string.IsNullOrEmpty(value) || !Enum.TryParse(value, out c)) { c = Culture.Cn; } return CultureConfigurer.GetValue(source, c); } } }Note that this class is an
abstract class
,inheritsRazorPage
The above is the detailed content of Detailed introduction to the sample code for multi-language implementation of Asp.Net Core MVC project. For more information, please follow other related articles on the PHP Chinese website!

Microsoft的Windows112022Update(22H2)默认启用CoreIsolation的内存完整性保护。但是,如果您运行的是旧版本的操作系统,例如Windows112022Update(22H1),则需要手动打开此功能。在Windows11中开启CoreIsolation的内存完整性功能对于不了解核心隔离的用户,这是一个安全过程,旨在通过将Windows上的基本核心活动隔离在内存中来保护它们免受恶意程序的侵害。该进程与内存完整性功能相结合,可确保

电脑中core有两种意思:1、核心,也即内核,是CPU最重要的组成部分,CPU所有的计算、接受存储命令、处理数据都由核心执行;2、酷睿,core是英特尔的处理器名称,酷睿是英特尔公司继奔腾处理器之后推出的处理器品牌,目前已经发布了十二代酷睿处理器。

如何利用vue和Element-plus实现多语言和国际化支持导语:在当今全球化的时代背景下,为应对不同语言和文化的用户需求,多语言和国际化支持成为了许多前端项目必备的功能。本文将介绍如何利用vue和Element-plus实现多语言和国际化支持,以便于项目能够灵活适应不同语言环境下的需求。一、安装Element-plusElement-plus是vue官方

随着互联网的日益普及,越来越多的网站需要支持多语言。这是因为网站的受众群体可能来自不同的地区和文化背景,如果只提供单一语言的网站,可能会限制访问者的数量和体验。本文将介绍如何在PHP中实现多语言网站。一、语言文件的创建和设计语言文件是存储所有文本字符串及其对应翻译的文件,需要以特定的格式创建。在创建语言文件时,需要考虑以下几个方面:1.命名和存储位置文件名应

随着国际化的不断发展,越来越多的网站和应用程序需要支持多语言切换功能。Vue作为一款流行的前端框架,提供了一种名为i18n的插件,可以帮助我们实现多语言切换。本文将介绍Vue中使用i18n实现多语言切换的常见技巧。第一步:安装i18n插件首先,我们需要使用npm或yarn安装i18n插件。在命令行中输入以下命令:npminst

CakePHP是一个流行的PHP开发框架,它可以帮助开发者快速构建高质量的Web应用程序。随着全球化的发展,越来越多的应用需要支持多语言,CakePHP也提供了相应的支持。本文将介绍CakePHP如何处理多语言。一、多语言支持多语言支持是CakePHP的一项重要功能。从版本2.0开始,CakePHP支持gettext文件格式,该

如何使用PHP和Typecho打造多语言支持的网站导语:随着全球化的发展,构建一个多语言支持的网站逐渐成为企业和个人所追求的目标。而PHP作为一种流行的编程语言,结合Typecho这一优秀的PHP开源博客程序,可以轻松实现多语言网站的搭建。本文将介绍如何使用PHP和Typecho来打造一个多语言支持的网站,并提供相关的代码示例。一、安装和配置Typecho首
![如何修复 Windows 11 / 10 中的处理器热跳闸错误 [修复]](https://img.php.cn/upload/article/000/000/164/168169038621890.png)
大多数设备(例如笔记本电脑和台式机)长期被年轻游戏玩家和编码人员频繁使用。由于应用程序过载,系统有时会挂起。这使用户被迫关闭他们的系统。这主要发生在安装和玩重度游戏的玩家身上。当系统在强制关闭后尝试启动时,它会在黑屏上抛出一个错误,如下所示:以下是在此引导期间检测到的警告。这些可以在事件日志页面的设置中查看。警告:处理器热跳闸。按任意键继续。..当台式机或笔记本电脑的处理器温度超过其阈值温度时,总是会抛出这些类型的警告消息。下面列出了在Windows系统上发生这种情况的原因。许多繁重的应用程序在


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
