Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use localization with String.Format

I work on an Asp.net Core MVC website with localization and I've a text to display with variables inside like :

@{var item = "car"}
<h1>Max's @item is blue</h1>

but in french it's

@{var item = "la voiture"}
<h1>@item de Max est bleue</h1>

So the words's order change, I've try :

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
    <h1>@String.Format(Localizer["Max's {0} is blue"],@item)</h1>

with a traduction :

    Max's {0} is blue => {0} de Max est bleu

but I've an error :

FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

How can I do this ?

like image 625
Alexis Avatar asked Oct 29 '25 19:10

Alexis


1 Answers

 @Localizer["My Format {0}", myValue]

It solves the problem because that is the syntax for localizer with parameters.

like image 130
Roland Avatar answered Nov 01 '25 09:11

Roland