示例#1
0
        // NB: This is probably going to fail if the JSON contains anything that isn't a string.
        public string RequiredPostString(string key)
        {
            Debug.Assert(_requestInfo.HttpMethod == HttpMethods.Post);
            var values = _requestInfo.GetPostDataWhenFormEncoded().GetValues(key);

            if (values != null && values.Length == 1)
            {
                return(values[0]);
            }
            throw new ApplicationException("The query " + _requestInfo.RawUrl + " should have 1 value for " + key);
        }
示例#2
0
        private bool ProcessDirectoryWatcher(IRequestInfo info)
        {
            // thread synchronization is done in CheckForSampleTextChanges.
            var dirName = info.GetPostDataWhenFormEncoded()["dir"];

            if (dirName == "Sample Texts")
            {
                if (CheckForSampleTextChanges(info))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#3
0
        private static void ProcessError(IRequestInfo info)
        {
            // pop-up the error messages if a debugger is attached or an environment variable is set
            var popUpErrors = Debugger.IsAttached || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEBUG_BLOOM"));

            var post = info.GetPostDataWhenFormEncoded();

            // log the error message
            var errorMsg = post["message"] + Environment.NewLine + "File: " + post["url"].FromLocalhost()
                           + Environment.NewLine + "Line: " + post["line"] + " Column: " + post["column"] + Environment.NewLine;

            Logger.WriteMinorEvent(errorMsg);
            Console.Out.WriteLine(errorMsg);

            if (popUpErrors)
            {
                Shell.DisplayProblemToUser(errorMsg);
            }
        }
 private bool ProcessDirectoryWatcher(IRequestInfo info)
 {
     // thread synchronization is done in CheckForSampleTextChanges.
     var dirName = info.GetPostDataWhenFormEncoded()["dir"];
     if (dirName == "Sample Texts")
     {
         if (CheckForSampleTextChanges(info))
             return true;
     }
     return false;
 }
        private static void ProcessError(IRequestInfo info)
        {
            // pop-up the error messages if a debugger is attached or an environment variable is set
            var popUpErrors = Debugger.IsAttached || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEBUG_BLOOM"));

            var post = info.GetPostDataWhenFormEncoded();

            // log the error message
            var errorMsg = post["message"] + Environment.NewLine + "File: " + post["url"].FromLocalhost()
                + Environment.NewLine + "Line: " + post["line"] + " Column: " + post["column"] + Environment.NewLine;

            Logger.WriteMinorEvent(errorMsg);
            Console.Out.WriteLine(errorMsg);

            if (popUpErrors)
                Shell.DisplayProblemToUser(errorMsg);
        }
示例#6
0
        public static bool HandleRequest(string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings)
        {
            var lastSep     = localPath.IndexOf("/", System.StringComparison.Ordinal);
            var lastSegment = (lastSep > -1) ? localPath.Substring(lastSep + 1) : localPath;

            switch (lastSegment)
            {
            case "loadStrings":

                while (_localizing)
                {
                    Thread.Sleep(0);
                }

                try
                {
                    _localizing = true;

                    var d    = new Dictionary <string, string>();
                    var post = info.GetPostDataWhenFormEncoded();

                    if (post != null)
                    {
                        foreach (string key in post.Keys)
                        {
                            try
                            {
                                if (d.ContainsKey(key))
                                {
                                    continue;
                                }

                                // Now that end users can create templates, it's annoying to report that their names,
                                // page labels, and page descriptions don't have localizations.
                                if (IsTemplateBookKey(key))
                                {
                                    continue;
                                }

                                var translation = GetTranslationDefaultMayNotBeEnglish(key, post[key]);
                                d.Add(key, translation);
                            }
                            catch (Exception error)
                            {
                                Debug.Fail("Debug Only:" + error.Message + Environment.NewLine + "A bug reported at this location is BL-923");
                                //Until BL-923 is fixed (hard... it's a race condition, it's better to swallow this for users
                            }
                        }
                    }

                    info.ContentType = "application/json";
                    info.WriteCompleteOutput(JsonConvert.SerializeObject(d));
                    return(true);
                }
                finally
                {
                    _localizing = false;
                }
                break;

            case "translate":
                var    parameters  = info.GetQueryParameters();
                string id          = parameters["key"];
                string englishText = parameters["englishText"];
                string langId      = parameters["langId"];
                langId = langId.Replace("V", currentCollectionSettings.Language1Iso639Code);
                langId = langId.Replace("N1", currentCollectionSettings.Language2Iso639Code);
                langId = langId.Replace("N2", currentCollectionSettings.Language3Iso639Code);
                langId = langId.Replace("UI", LocalizationManager.UILanguageId);
                if (LocalizationManager.GetIsStringAvailableForLangId(id, langId))
                {
                    info.ContentType = "text/plain";
                    info.WriteCompleteOutput(LocalizationManager.GetDynamicStringOrEnglish("Bloom", id, englishText, null, langId));
                    return(true);
                }
                else
                {
                    // Don't report missing strings if they are numbers
                    // Enhance: We might get the Javascript to do locale specific numbers someday
                    // The C# side doesn't currently have the smarts to do DigitSubstitution
                    // See Remark at https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution(v=vs.110).aspx
                    if (IsInteger(id))
                    {
                        englishText = id;
                    }
                    else
                    {
                        // Now that end users can create templates, it's annoying to report that their names,
                        // page labels, and page descriptions don't have localizations.
                        if (IsTemplateBookKey(id))
                        {
                            englishText = englishText.Trim();
                        }
                        else
                        {
                            // it's ok if we don't have a translation, but if the string isn't even in the list of things that need translating,
                            // then we want to remind the developer to add it to the english xlf file.
                            if (!LocalizationManager.GetIsStringAvailableForLangId(id, "en"))
                            {
                                ReportL10NMissingString(id, englishText, UrlPathString.CreateFromUrlEncodedString(parameters["comment"] ?? "").NotEncoded);
                            }
                            else
                            {
                                //ok, so we don't have it translated yet. Make sure it's at least listed in the things that can be translated.
                                // And return the English string, which is what we would do the next time anyway.  (BL-3374)
                                LocalizationManager.GetDynamicString("Bloom", id, englishText);
                            }
                        }
                    }
                    info.ContentType = "text/plain";
                    info.WriteCompleteOutput(englishText);
                    return(true);
                }
                break;
            }

            return(false);
        }
示例#7
0
        public static bool HandleRequest(string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings)
        {
            var lastSep = localPath.IndexOf("/", System.StringComparison.Ordinal);
            var lastSegment = (lastSep > -1) ? localPath.Substring(lastSep + 1) : localPath;

            switch (lastSegment)
            {
                case "loadStrings":

                    while (_localizing)
                    {
                        Thread.Sleep(0);
                    }

                    try
                    {
                        _localizing = true;

                        var d = new Dictionary<string, string>();
                        var post = info.GetPostDataWhenFormEncoded();

                        if (post != null)
                        {
                            foreach (string key in post.Keys)
                            {
                                try
                                {
                                    if (!d.ContainsKey(key))
                                    {
                                        var translation = GetTranslationDefaultMayNotBeEnglish(key, post[key]);
                                        d.Add(key, translation);
                                    }
                                }
                                catch (Exception error)
                                {
                                    Debug.Fail("Debug Only:" +error.Message+Environment.NewLine+"A bug reported at this location is BL-923");
                                    //Until BL-923 is fixed (hard... it's a race condition, it's better to swallow this for users
                                }
                            }
                        }

                        info.ContentType = "application/json";
                        info.WriteCompleteOutput(JsonConvert.SerializeObject(d));
                        return true;
                    }
                    finally
                    {
                        _localizing = false;
                    }
                    break;

                case "translate":
                    var parameters = info.GetQueryParameters();
                    string id = parameters["key"];
                    string englishText = parameters["englishText"];
                    string langId = parameters["langId"];
                    langId = langId.Replace("V", currentCollectionSettings.Language1Iso639Code);
                    langId = langId.Replace("N1", currentCollectionSettings.Language2Iso639Code);
                    langId = langId.Replace("N2", currentCollectionSettings.Language3Iso639Code);
                    langId = langId.Replace("UI", LocalizationManager.UILanguageId);
                    if (LocalizationManager.GetIsStringAvailableForLangId(id, langId))
                    {
                        info.ContentType = "text/plain";
                        info.WriteCompleteOutput(LocalizationManager.GetDynamicStringOrEnglish("Bloom", id, englishText, null, langId));
                        return true;
                    }
                    else
                    {
                        // Don't report missing strings if they are numbers
                        // Enhance: We might get the Javascript to do locale specific numbers someday
                        // The C# side doesn't currently have the smarts to do DigitSubstitution
                        // See Remark at https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution(v=vs.110).aspx
                        if (IsInteger(id))
                        {
                            englishText = id;
                        }
                        else
                        {
                            // it's ok if we don't have a translation, but if the string isn't even in the list of things that need translating,
                            // then we want to remind the developer to add it to the english tmx file.
                            if (!LocalizationManager.GetIsStringAvailableForLangId(id, "en"))
                            {
                                ReportL10NMissingString(id, englishText);
                            }
                            else
                            {
                                //ok, so we don't have it translated yet. Make sure it's at least listed in the things that can be translated.
                                // And return the English string, which is what we would do the next time anyway.  (BL-3374)
                                LocalizationManager.GetDynamicString("Bloom", id, englishText);
                            }
                        }
                        info.ContentType = "text/plain";
                        info.WriteCompleteOutput(englishText);
                        return true;
                    }
                    break;
            }

            return false;
        }