Пример #1
0
 public static void RegisterToken(TranslationToken token)
 {
     if (tokens == null)
     {
         tokens = new List <TranslationToken>();
     }
     tokens.Add(token);
 }
Пример #2
0
    private IEnumerator CreateTokens()
    {
        ContextMenuOnClick cm = this.gameObject.GetComponent <ContextMenuOnClick>();

        if (cm == null)
        {
            Debug.LogError("ContextMenuTranslationTokenizer must be attached to element with ContextMenuOnClick component.");
        }
        while (cm.isInitialized() == false)
        {
            yield return(new WaitForSeconds(0.1f));
        }
        List <RectTransform> buttonsRTs = cm.GetContextMenuButtonsRTs();
        Transform            parent     = buttonsRTs[0].parent;
        bool shouldDeactivateParent     = false;

        if (!parent.gameObject.activeSelf)
        {
            parent.gameObject.SetActive(true);
            shouldDeactivateParent = true;
        }
        foreach (RectTransform buttonRT in buttonsRTs)
        {
            bool shouldDeactivateButton = false;
            if (!buttonRT.gameObject.activeSelf)
            {
                buttonRT.gameObject.SetActive(true);
                shouldDeactivateButton = true;
            }
            Text             txt   = buttonRT.gameObject.GetComponentInChildren <Text>();
            string           text  = txt.text;
            TranslationToken token = txt.gameObject.AddComponent <TranslationToken>();
            token.targetText  = txt;
            token.token       = text;
            token.tagFileName = tagFileName;
            txt.text          = TranslationManager.GetMessage(tagFileName, text);
            if (shouldDeactivateButton == true)
            {
                buttonRT.gameObject.SetActive(false);
            }
        }
        if (shouldDeactivateParent == true)
        {
            parent.gameObject.SetActive(false);
        }
    }
Пример #3
0
 public static void RegisterToken(TranslationToken token)
 {
     if (tokens == null)
         tokens  = new List<TranslationToken>();
     tokens.Add(token);
 }
Пример #4
0
        /// <summary>
        /// Returns the translation for a UI string.
        /// </summary>
        /// <param name="identifier">The UI string identifier set with <see cref="AddTranslation"/>.</param>
        /// <param name="tokens">The tokens inserted into the string.</param>
        protected string GetTranslation(string identifier, TranslationToken[] tokens)
        {
            // Translation tokens use .Net's String.Format format: {numeric}.
            // So the first token replaces "{0}".

            if (identifier == null)
                throw new ArgumentException("Identifier must not be null.", nameof(identifier));

            // Remove the "C_" from the identifier:
            identifier = identifier.Remove(0, 2);

            if (!_stringDic.Keys.Contains(identifier))
                throw new ArgumentException("The identifier is not present in the dictionary.", nameof(identifier));

            var translatedString = LocalizationManager.GetLocalString(_typeName + ":" + identifier);

            // Apply the fallback string if the language found does not have the identifier:
            if (translatedString == null)
                translatedString = _stringDic[identifier];

            // Only attempt to replace tokens if possible:
            if (tokens != null && tokens.Length > 0)
            {
                var parameters = tokens.OrderBy(x => x.Number).Select(x => x.Data).ToArray();

                translatedString = string.Format(translatedString, parameters);
            }

            return translatedString;
        }