예제 #1
0
        /// <summary>
        /// Load a common prompt
        /// </summary>
        /// <param name="text">Text to translate</param>
        /// <returns>Translation if found; otherwise null</returns>
        /// <remarks>Used to avoid duplications of prompts.</remarks>
        protected virtual string LoadCommonPrompt(string text)
        {
            var key    = new ViewPromptKey("CommonPrompts", text);
            var prompt = _repository.GetPrompt(CultureInfo.CurrentUICulture, key);

            return(prompt == null ? null : prompt.TranslatedText);
        }
예제 #2
0
        /// <summary>
        /// Translate a text prompt
        /// </summary>
        /// <param name="viewPath">Virtual path to the view, so that we can identify layouts and request common prompts.</param>
        /// <param name="routeData">Used to lookup the view location</param>
        /// <param name="text">Text to translate</param>
        /// <returns>
        /// String if found; otherwise null.
        /// </returns>
        public virtual string Translate(string viewPath, RouteData routeData, string text)
        {
            if (routeData == null)
            {
                throw new ArgumentNullException("routeData");
            }
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            if (!_repository.Exists(CultureInfo.CurrentUICulture))
            {
                //use english as default
                CloneDefaultCulture();
            }

            var    textToSay = "";
            string uri       = ViewPromptKey.GetViewPath(viewPath, routeData);

            /*
             * uri = viewPath.ToLower().Contains("/views/shared/")
             *        ? viewPath.TrimStart('~').Remove(viewPath.LastIndexOf('/'))
             *        : ViewPromptKey.GetViewPath(routeData);
             */
            var id     = new ViewPromptKey(uri, text);
            var prompt = _repository.GetPrompt(CultureInfo.CurrentUICulture, id);

            if (prompt == null)
            {
                textToSay = LoadCommonPrompt(text);
                if (textToSay == null)
                {
                    _repository.CreatePrompt(CultureInfo.CurrentUICulture, uri, text, "");
                }
            }
            else
            {
                textToSay = prompt.TranslatedText;
            }

            return(string.IsNullOrEmpty(textToSay)
                       ? FormatMissingPrompt(text)
                       : textToSay);
        }
예제 #3
0
        public virtual string Translate(RouteData routeData, string text)
        {
            if (routeData == null)
            {
                throw new ArgumentNullException("routeData");
            }
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            if (!_repository.Exists(CultureInfo.CurrentUICulture))
            {
                //use english as default
                CloneDefaultCulture();
            }

            var textToSay = "";
            var uri       = ViewPromptKey.GetViewPath(routeData);
            var id        = new ViewPromptKey(uri, text);
            var prompt    = _repository.GetPrompt(CultureInfo.CurrentUICulture, id);

            if (prompt == null)
            {
                textToSay = LoadCommonPrompt(text);
                if (textToSay == null)
                {
                    _repository.CreatePrompt(CultureInfo.CurrentUICulture, uri, text, "");
                }
            }
            else
            {
                textToSay = prompt.TranslatedText;
            }

            return(string.IsNullOrEmpty(textToSay)
                       ? FormatMissingPrompt(text)
                       : textToSay);
        }