5

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 ?

1
  • 1
    Try with Localizer["Max's {0} is blue"].Value,@item Commented Jun 1, 2018 at 10:08

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

This answer was flagged by someone as low quality because while it provides a code snippet that may solve the problem stated in the question, it does not explain why it solves the problem. In the future, please add detail to your answer that shows why the code you post applies to the question being asked. Thank you.
-1

@Camilo Terevinto's solution work perfectly. here's the full solution if it can help anyone :

View :

@model Project.Models.item
    <h1>@String.Format(Localizer["Max's {0} is {1}"].Value, Model.Name, Model.Color)</h1>

Resx :

Max's {0} is {1} => {0} de Max est {1}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.