Exemplo n.º 1
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Updates the localized info. in the cache with the info. from the specified
 /// LocalizedObjectInfo.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 internal void UpdateLocalizedInfo(LocalizingInfo locInfo)
 {
     if (_tuUpdater.Update(locInfo) && !IsDirty)
     {
         IsDirty = true;
     }
 }
Exemplo n.º 2
0
        private void UpdateTransUnitComment(TransUnit tu, LocalizingInfo locInfo)
        {
            if (locInfo.DiscoveredDynamically && (tu.GetPropValue(LocalizedStringCache.kDiscoveredDyanmically) != "true"))
            {
                tu.AddProp(LocalizedStringCache.kDiscoveredDyanmically, "true");
                _updated = true;
            }

            if ((locInfo.UpdateFields & UpdateFields.Comment) != UpdateFields.Comment)
            {
                return;
            }

            if ((tu.Notes.Count > 0) && (tu.Notes[0].Text == locInfo.Comment))
            {
                return;
            }

            tu.Notes.Clear();
            _updated = true;

            if (!string.IsNullOrEmpty(locInfo.Comment))
            {
                tu.AddNote(locInfo.Comment);
            }
        }
Exemplo n.º 3
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Updates the value for the specified translation unit with the specified new value.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private TransUnit UpdateValue(TransUnit tu, string newValue, LocalizingInfo locInfo, string tuId)
        {
            newValue = newValue ?? string.Empty;

            // Get rid of the variant we are about to set if it is present.
            // If no variants remain get rid of the whole thing.
            // Later we will create whatever we need.
            if (tu != null)
            {
                var tuv = tu.GetVariantForLang(locInfo.LangId);
                if (tuv != null)
                {
                    // don't need to update if the value hasn't changed
                    if (tuv.Value == newValue)
                    {
                        return(tu);
                    }

                    _updated = true;
                    tu.RemoveVariant(tuv);
                    if (tu.Variants.Count == 0)
                    {
                        _tmxDoc.RemoveTransUnit(tu);
                        tu = null;                         // so we will make a new one if needed.
                    }
                }
            }

            if (newValue == string.Empty)
            {
                return(tu);
            }

            // Create a new entry if needed.
            if (tu == null)
            {
                tu    = new TransUnit();
                tu.Id = tuId;
                _tmxDoc.AddTransUnit(tu);
            }

            tu.AddOrReplaceVariant(locInfo.LangId, newValue);
            _updated = true;
            return(tu);
        }
Exemplo n.º 4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets a string for the specified application id and string id, in the requested
        /// language. When a string for the
        /// specified id cannot be found, then one is added  using the specified englishText is
        /// returned when a string cannot be found for the specified id and the current UI
        /// language. Use GetIsStringAvailableForLangId if you need to know if we have the
        /// value or not.
        /// Special case: unless englishText is null, that is what will be returned for langId = 'en',
        /// irrespective of what is in TMX/Xliff.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public static string GetDynamicStringOrEnglish(string appId, string id, string englishText, string comment, string langId)
        {
            //this happens in unit test environments or apps that
            //have imported a library that is L10N'ized, but the app
            //itself isn't initializing L10N yet.
            if (LoadedManagers.Count == 0)
            {
                if (PreviouslyLoadedManagers.Contains(appId))
                {
                    if (LocalizationManager.ThrowIfManagerDisposed)
                    {
                        throw new ObjectDisposedException(
                                  $"The application id '{appId}' refers to a LocalizationManagerInternal that has been disposed");
                    }
                    return(string.IsNullOrEmpty(englishText) ? id : englishText);
                }

                if (!string.IsNullOrEmpty(englishText) && langId == LocalizationManager.kDefaultLang)
                {
                    return(englishText);
                }
                return(id);
            }
            if (!LoadedManagers.TryGetValue(appId, out var lm))
            {
                if (PreviouslyLoadedManagers.Contains(appId))
                {
                    if (LocalizationManager.ThrowIfManagerDisposed)
                    {
                        throw new ObjectDisposedException(
                                  $"The application id '{appId}' refers to a LocalizationManagerInternal that has been disposed");
                    }

                    return(string.IsNullOrEmpty(englishText) ? id : englishText);
                }
                throw new ArgumentException(
                          $"The application id '{appId}' does not have an associated localization manager. " +
                          $"Initialized LMs are {string.Join(", ", LoadedManagers.Keys)}");
            }

            // If they asked for English, we are going to use the supplied englishText, regardless of what may be in
            // some TMX/Xliff, following the rule that the current c# code always wins. In case we really need to
            // recover the TMX/Xliff version, we will retrieve that if no default is provided.
            // Otherwise, let's look up this string, maybe it has been translated and put into a TMX/Xliff
            if (langId != "en" || englishText == null)
            {
                var text = lm.GetStringFromStringCache(langId, id);
                if (text != null)
                {
                    return(text);
                }
            }

            if (!lm.CollectUpNewStringsDiscoveredDynamically)
            {
                return(englishText);
            }

            var locInfo = new LocalizingInfo(id)
            {
                LangId = LocalizationManager.kDefaultLang,
                Text   = englishText,
                DiscoveredDynamically = true,
                UpdateFields          = UpdateFields.Text
            };

            if (!string.IsNullOrEmpty(comment))
            {
                locInfo.Comment       = comment;
                locInfo.UpdateFields |= UpdateFields.Comment;
            }

            lm.StringCache.UpdateLocalizedInfo(locInfo);
            lm.SaveIfDirty(null);            // this will be common for GetDynamic string on users restricted from writing to ProgramData
            return(englishText);
        }
Exemplo n.º 5
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Updates the localized info. in the cache with the info. from the specified
        /// LocalizedObjectInfo.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        internal bool Update(LocalizingInfo locInfo)
        {
            _updated = false;

            // Can't do anything without a language id.
            if (string.IsNullOrEmpty(locInfo.LangId))
            {
                return(_updated);
            }

            var tuText = _tmxDoc.GetTransUnitForId(locInfo.Id);

            var tuToolTip      = _tmxDoc.GetTransUnitForId(locInfo.Id + kToolTipSuffix);
            var tuShortcutKeys = _tmxDoc.GetTransUnitForId(locInfo.Id + kShortcutSuffix);

            if (locInfo.Priority == LocalizationPriority.NotLocalizable)
            {
                _updated = (tuText != null || tuToolTip != null || tuShortcutKeys != null);
                _tmxDoc.RemoveTransUnit(tuText);
                _tmxDoc.RemoveTransUnit(tuToolTip);
                _tmxDoc.RemoveTransUnit(tuShortcutKeys);
                return(_updated);
            }

            // Save the shortcut keys
            if ((locInfo.UpdateFields & UpdateFields.ShortcutKeys) == UpdateFields.ShortcutKeys)
            {
                tuShortcutKeys = UpdateValue(tuShortcutKeys, locInfo.ShortcutKeys, locInfo, locInfo.Id + kShortcutSuffix);
            }

            // Save the tooltips
            if ((locInfo.UpdateFields & UpdateFields.ToolTip) == UpdateFields.ToolTip)
            {
                tuToolTip = UpdateValue(tuToolTip, locInfo.ToolTipText, locInfo, locInfo.Id + kToolTipSuffix);
            }

            // Save the text
            if ((locInfo.UpdateFields & UpdateFields.Text) == UpdateFields.Text)
            {
                var text = locInfo.Text ?? string.Empty;
                text   = text.Replace(Environment.NewLine, _literalNewline);
                text   = text.Replace(_literalNewline, "@#$");
                text   = text.Replace(kOSRealNewline, _literalNewline);
                text   = text.Replace("@#$", _literalNewline);
                tuText = UpdateValue(tuText, text, locInfo, locInfo.Id);
            }

            if (tuText != null)
            {
                UpdateTransUnitComment(tuText, locInfo);
            }

            if (tuToolTip != null)
            {
                UpdateTransUnitComment(tuToolTip, locInfo);
            }

            if (tuShortcutKeys != null)
            {
                UpdateTransUnitComment(tuShortcutKeys, locInfo);
            }

            return(_updated);
        }