// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Text; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.Extensions.Localization; using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNetCore.Mvc.Localization { /// /// An implementation that derives the resource location from the executing view's /// file path. /// public class ViewLocalizer : IViewLocalizer, ICanHasViewContext { private readonly IHtmlLocalizerFactory _localizerFactory; private readonly string _applicationName; private IHtmlLocalizer _localizer; /// /// Creates a new . /// /// The . /// The . public ViewLocalizer(IHtmlLocalizerFactory localizerFactory, IApplicationEnvironment applicationEnvironment) { if (localizerFactory == null) { throw new ArgumentNullException(nameof(localizerFactory)); } if (applicationEnvironment == null) { throw new ArgumentNullException(nameof(applicationEnvironment)); } _applicationName = applicationEnvironment.ApplicationName; _localizerFactory = localizerFactory; } /// public virtual LocalizedHtmlString this[string key] { get { if (key == null) { throw new ArgumentNullException(nameof(key)); } return _localizer[key]; } } /// public virtual LocalizedHtmlString this[string key, params object[] arguments] { get { if (key == null) { throw new ArgumentNullException(nameof(key)); } return _localizer[key, arguments]; } } /// public LocalizedString GetString(string name) => _localizer.GetString(name); /// public LocalizedString GetString(string name, params object[] values) => _localizer.GetString(name, values); /// public IHtmlLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); /// public IEnumerable GetAllStrings(bool includeParentCultures) => _localizer.GetAllStrings(includeParentCultures); /// /// Apply the specified . /// /// The . public void Contextualize(ViewContext viewContext) { if (viewContext == null) { throw new ArgumentNullException(nameof(viewContext)); } // Given a view path "/Views/Home/Index.cshtml" we want a baseName like "MyApplication.Views.Home.Index" var path = viewContext.ExecutingFilePath; if (string.IsNullOrEmpty(path)) { path = viewContext.View.Path; } Debug.Assert(!string.IsNullOrEmpty(path), "Couldn't determine a path for the view"); _localizer = _localizerFactory.Create(BuildBaseName(path), _applicationName); } private string BuildBaseName(string path) { var extension = Path.GetExtension(path); var startIndex = path[0] == '/' || path[0] == '\\' ? 1 : 0; var length = path.Length - startIndex - extension.Length; var capacity = length + _applicationName.Length + 1; var builder = new StringBuilder(path, startIndex, length, capacity); builder.Replace('/', '.').Replace('\\', '.'); // Prepend the application name builder.Insert(0, '.'); builder.Insert(0, _applicationName); return builder.ToString(); } } }