|
| 1 | +[中](README.zh-CN.md) | EN |
| 2 | + |
| 3 | +Provides the ability to parse and obtain Culture, and use it with [I18N](../Masa.Contrib.Globalization.I18N/README.md). Currently, there are three ways to switch languages: |
| 4 | + |
| 5 | +* URL parameter method: ?culture=en-US, this method has the highest priority, the format is: culture=region code |
| 6 | +* Cookies method: call L.SetCulture (region code) method to switch |
| 7 | +* Client browser language automatic matching: If neither of the previous two methods are set, it supports automatic matching according to the client browser language. |
| 8 | + |
| 9 | +## Masa.Contrib.Globalization.I18N.AspNetCore |
| 10 | + |
| 11 | +Example: |
| 12 | + |
| 13 | +``` powershell |
| 14 | +Install-Package Masa.Contrib.Globalization.I18N.AspNetCore |
| 15 | +``` |
| 16 | + |
| 17 | +### getting Started |
| 18 | + |
| 19 | +1. Default resource folder structure |
| 20 | + |
| 21 | +``` structure |
| 22 | +- Resources |
| 23 | + - I18n |
| 24 | + - en-US.json |
| 25 | + - zh-CN.json |
| 26 | + - supportedCultures.json |
| 27 | +``` |
| 28 | + |
| 29 | +* en-US.json |
| 30 | + |
| 31 | +``` en-US.json |
| 32 | +{ |
| 33 | + "Home":"Home", |
| 34 | + "Docs":"Docs", |
| 35 | + "Blog":"Blog", |
| 36 | + "Team":"Team", |
| 37 | + "Search":"Search" |
| 38 | + "User":{ |
| 39 | + "Name":"Name" |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +* zh-CN.json |
| 45 | + |
| 46 | +``` zh-CN.json |
| 47 | +{ |
| 48 | + "Home":"Home", |
| 49 | + "Docs":"Documents", |
| 50 | + "Blog":"Blog", |
| 51 | + "Team":"Team", |
| 52 | + "Search":"Search", |
| 53 | + "User":{ |
| 54 | + "Name":"Name" |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +* supportedCultures.json |
| 60 | + |
| 61 | +``` supportedCultures.json |
| 62 | +[ |
| 63 | + { |
| 64 | + "Culture":"zh-CN", |
| 65 | + "DisplayName":"Simplified Chinese", |
| 66 | + "Icon": "{Replace-Your-Icon}" |
| 67 | + }, |
| 68 | + { |
| 69 | + "Culture":"en-US", |
| 70 | + "DisplayName":"English (United States)", |
| 71 | + "Icon": "{Replace-Your-Icon}" |
| 72 | + } |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | +2. Register to use I18N, modify `Program.cs` |
| 77 | + |
| 78 | +``` C# |
| 79 | +services.AddI18N(); |
| 80 | +``` |
| 81 | + |
| 82 | +3. Use `Masa.Contrib.Globalization.I18N.AspNetCore` to provide the ability to parse Culture |
| 83 | + |
| 84 | +``` C# |
| 85 | +app.UseI18N(); |
| 86 | +``` |
| 87 | + |
| 88 | +4. How to use I18N |
| 89 | + |
| 90 | +* Get `II18N` from DI (**II18N** is the interface, supports getting from DI) |
| 91 | +* Use `I18N` (**I18N** is a static class) |
| 92 | + |
| 93 | +Take `I18N` as an example: |
| 94 | + |
| 95 | +```` C# |
| 96 | +var home = I18N.T("Home"); //Get the value of the language corresponding to the key value Home, this method call will return "Home"; |
| 97 | +var name = I18N.T("User.Name");//Output: name (support nesting) |
| 98 | +```` |
0 commit comments