Esempio n. 1
0
        public void HandleI18nRequest(ApiRequest request)
        {
            var lastSegment = request.LocalPath().Split(new char[] { '/' }).Last();

            switch (lastSegment)
            {
            case "loadStrings":
                var d    = new Dictionary <string, string>();
                var post = request.GetPostDataWhenFormEncoded();

                if (post != null)
                {
                    foreach (string key in post.Keys)
                    {
                        try
                        {
                            if (d.ContainsKey(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
                        }
                    }
                }
                request.ReplyWithJson(JsonConvert.SerializeObject(d));
                break;

            case "translate":
                var    parameters  = request.Parameters;
                string id          = parameters["key"];
                string englishText = parameters["englishText"];
                string langId      = parameters["langId"];
                langId = langId.Replace("V", request.CurrentCollectionSettings.Language1Iso639Code);
                langId = langId.Replace("N1", request.CurrentCollectionSettings.Language2Iso639Code);
                langId = langId.Replace("N2", request.CurrentCollectionSettings.Language3Iso639Code);
                langId = langId.Replace("UI", LocalizationManager.UILanguageId);
                string localizedString;
                if (GetSomeTranslation(id, langId, out localizedString))
                {
                    // Ensure that we actually have a value for localized string.  (This should already be true, but I'm paranoid.)
                    if (localizedString == null)
                    {
                        localizedString = englishText;
                    }
                    request.ReplyWithJson(new { text = localizedString, success = true });
                }
                else
                {
                    var idFound = true;
                    // 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);
                                idFound = false;
                            }
                            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);
                            }
                        }
                    }
                    request.ReplyWithJson(new { text = englishText, success = idFound });
                }
                break;

            default:
                request.Failed();
                break;
            }
        }