Fetching localized data from APIs
Before we wrap up this chapter, let’s have a quick look at how we could pass the user’s language to the API so that it can return localized data. One approach is to include a language parameter in every service and repository method, allowing the ViewModel to pass the user’s current language. However, I believe adding such parameters can clutter the code. A cleaner alternative is to handle this within the repositories. Let’s see how:
- First, let’s update the
IRecipeAPI
interface by adding alanguage
parameter of typestring
to theGetRecipes
method. The following snippet shows how we can configure Refit to pass this additional parameter as anAccept-Language
request header when executing the API call:Task<ApiResponse<RecipeOverviewItemsDto>> GetRecipes([Header("Accept-Language")] string language, int pageSize = 7, int pageIndex = 0);
We could pass this...