Update Globalization.yaml #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: .NET 8 Globalization Example | |
on: | |
push | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the repository | |
uses: actions/checkout@v2 | |
- name: Set up .NET 8 | |
uses: actions/setup-dotnet@v3 | |
with: | |
dotnet-version: '8.0.x' | |
- name: Create the .NET 8 Console Application | |
run: | | |
# Create a new .NET 8 console application | |
dotnet new console -n GlobalizationDemo | |
cd GlobalizationDemo | |
# Install Localization NuGet package | |
dotnet add package Microsoft.Extensions.Localization | |
# Create Resources folder and add localization files | |
mkdir Resources | |
echo '<?xml version="1.0" encoding="utf-8"?>' > Resources/Messages.resx | |
echo '<root><data name="Greeting" xml:space="preserve"><value>Hello!</value></data></root>' >> Resources/Messages.resx | |
echo '<?xml version="1.0" encoding="utf-8"?>' > Resources/Messages.es.resx | |
echo '<root><data name="Greeting" xml:space="preserve"><value>¡Hola!</value></data></root>' >> Resources/Messages.es.resx | |
# Create Program.cs with globalization logic | |
echo 'using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Localization; | |
using System; | |
using System.Globalization; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Set the culture to Spanish for this example | |
var culture = new CultureInfo("es-ES"); | |
CultureInfo.CurrentCulture = culture; | |
CultureInfo.CurrentUICulture = culture; | |
// Setup dependency injection (DI) | |
var serviceProvider = new ServiceCollection() | |
.AddLocalization() | |
.BuildServiceProvider(); | |
// Get localization service | |
var localizer = serviceProvider.GetRequiredService<IStringLocalizer<Program>>(); | |
// Print the localized greeting | |
Console.WriteLine(localizer["Greeting"]); | |
} | |
}' > Program.cs | |
- name: Build the .NET Project | |
run: | | |
cd GlobalizationDemo | |
dotnet build --configuration Release | |
- name: Run the .NET Project | |
run: | | |
cd GlobalizationDemo | |
dotnet run --configuration Release | |
- name: Validate Output | |
run: | | |
# Check the output for localized greeting | |
OUTPUT=$(dotnet run --configuration Release) | |
if [[ "$OUTPUT" != "¡Hola!" ]]; then | |
echo "Test failed. Expected '¡Hola!' but got '$OUTPUT'." | |
exit 1 | |
else | |
echo "Test passed. Output is correct." | |
fi |