Exemplo n.º 1
0
        /// <summary>
        ///   Get language for the specified culture
        /// </summary>
        /// <param name="culture"> Requested culture </param>
        /// <returns> A collection of prompts (will create a new collection if it do not exist) </returns>
        protected ViewPromptCollection GetLanguage(CultureInfo culture)
        {
            ViewPromptCollection prompts;

            if (_languages.TryGetValue(culture, out prompts))
            {
                return(prompts);
            }

            lock (WriteLock)
            {
                if (_languages.TryGetValue(culture, out prompts))
                {
                    return(prompts);
                }

                prompts = LoadLanguage(culture);
                if (prompts == null)
                {
                    prompts = new ViewPromptCollection(culture);
                }
                _languages.Add(culture, prompts);
            }

            return(prompts);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Create a new prompt in the specified language
        /// </summary>
        /// <param name="culture"> Language that the translation is for </param>
        /// <param name="viewPath"> Path to view. You can use <see cref="ViewPromptKey.GetViewPath" /> </param>
        /// <param name="textName"> Text to translate </param>
        /// <param name="translatedText"> Translated text </param>
        public void CreatePrompt(CultureInfo culture, string viewPath, string textName, string translatedText)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }
            if (viewPath == null)
            {
                throw new ArgumentNullException("viewPath");
            }
            if (textName == null)
            {
                throw new ArgumentNullException("textName");
            }
            if (translatedText == null)
            {
                throw new ArgumentNullException("translatedText");
            }

            var key    = new ViewPromptKey(viewPath, textName);
            var prompt = new ViewPrompt
            {
                Key            = key,
                TranslatedText = translatedText,
                LocaleId       = culture.LCID,
                TextName       = textName,
                ViewPath       = viewPath
            };
            var language = GetLanguage(culture);

            if (language == null)
            {
                var prompts    = GetAllPrompts(culture, DefaultUICulture.Value, new SearchFilter());
                var collection = new ViewPromptCollection(culture);
                collection.AddRange(prompts);

                // dont forget to translate
                var dbPrompt = collection.Get(key);
                if (dbPrompt == null)
                {
                    collection.Add(prompt);
                }
                else
                {
                    dbPrompt.TranslatedText = translatedText;
                }

                language = _languages[culture];
            }
            else
            {
                language.Add(prompt);
            }

            SaveLanguage(culture, language);
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Create translation for a new language
        /// </summary>
        /// <param name="culture"> Language to create </param>
        /// <param name="templateCulture"> Language to use as a template </param>
        public void CreateLanguage(CultureInfo culture, CultureInfo templateCulture)
        {
            var prompts     = GetLanguage(templateCulture).Select(p => new ViewPrompt(culture.LCID, p));
            var newLanguage = new ViewPromptCollection(culture);

            newLanguage.AddRange(prompts);
            lock (_languages)
            {
                _languages[culture] = newLanguage;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Load a language
        /// </summary>
        /// <param name="culture">Culture to load for</param>
        /// <returns>Language if found; otherwise null.</returns>
        protected ViewPromptCollection LoadLanguage(CultureInfo culture)
        {
            var filename = GetFullPath(culture);

            if (!File.Exists(filename))
            {
                return(null);
            }

            using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var prompts = new ViewPromptCollection(culture);
                var items   = Deserialize(stream);
                prompts.AddRange(items);
                return(prompts);
            }
        }