Exemplo n.º 1
0
        /// <summary>
        /// Set any translation from 'Translations' property
        /// </summary>
        /// <param name="translation"></param>
        public static void SetCulture(TranslationInfo translation)
        {
            if (!isInit)
            {
                throw new NotInitializedException(typeof(LocalizationManager));
            }

            if (!Translations.Contains(translation))
            {
                throw new CultureNotFoundException($"Invalid translation: '{translation.TranslationName}', with culture: '{translation.CultureInfo.Name}'");
            }

            if (CurrentTranslation == translation)
            {
                Debug.LogWarning("You are trying to switch to the current culture.");
                return;
            }

            CurrentTranslation = translation;
            IsDefaultCulture   = false;

            PluralForm = PluralFormGenerator.CreateForm(CurrentTranslation.CultureInfo);

            var translationDictionary = translationDatabase.GetTranslationDictionary(CurrentTranslation);

            LocalizationCache.Init(translationDictionary);

            OnCultureChange?.Invoke();
        }
Exemplo n.º 2
0
        public Translation Import(TranslationInfo translationInfo)
        {
            string translationDirectory = Path.Combine(LocalizationEditorEnvironmentManager.TempFolder, translationInfo.TranslationName);

            string[] files = Directory.GetFiles(translationDirectory, "*.po");

            if (files.Length > 1)
            {
                throw new Exception($"More then one PO file in directory {translationDirectory}.");
            }

            this._pluralsCount = PluralFormGenerator.CreateForm(translationInfo.CultureInfo).PluralsCount;

            var result = new Translation();

            using (var fileStream = File.OpenRead(files[0]))
                using (var streamReader = new StreamReader(fileStream))
                {
                    string line;
                    while (!string.IsNullOrEmpty(streamReader.ReadLine()))
                    {
                    }

                    var unit = new List <string>();

                    while ((line = streamReader.ReadLine()) != null)
                    {
                        if (line.StartsWith(this._comment))
                        {
                            unit.Clear();
                        }

                        if (line.StartsWith(this._msgstr) || line.StartsWith(this._comment))
                        {
                            unit.Add(line.Replace("\"", string.Empty));
                        }

                        if (string.IsNullOrEmpty(line))
                        {
                            this.ProcessUnit(unit, ref result);
                        }
                    }

                    //TODO: из за этого иногда дублируются строки в сете
                    if (unit.Count > 0)
                    {
                        this.ProcessUnit(unit, ref result);
                    }
                }

            return(result);
        }
Exemplo n.º 3
0
        public static void SetDefaultCulture()
        {
            if (!isInit)
            {
                throw new NotInitializedException(typeof(LocalizationManager));
            }

            CurrentTranslation = null;
            IsDefaultCulture   = true;

            PluralForm = PluralFormGenerator.CreateForm(defaultCulture);

            OnCultureChange?.Invoke();
        }
Exemplo n.º 4
0
        public void Export(TranslationInfo translationInfo, DefaultLocalizationSet defaultLocalizationSet)
        {
            string translationDirectory = Path.Combine(LocalizationEditorEnvironmentManager.TempFolder, translationInfo.TranslationName);

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(this._msgid.F(string.Empty));
            sb.AppendLine(this._msgstr);
            sb.AppendLine("\"Language: " + translationInfo.CultureInfo.TwoLetterISOLanguageName + "\\n\"");
            sb.AppendLine();

            int pluralsCount = PluralFormGenerator.CreateForm(translationInfo.CultureInfo).PluralsCount;

            foreach (StoredString s in defaultLocalizationSet.Strings)
            {
                sb.AppendLine(this._comment.F(s.Id));

                if (s.Strings.Length == 1)
                {
                    sb.AppendLine(this._msgid.F(s.Strings[0]));

                    sb.AppendLine(this._msgstr);
                }
                else
                {
                    sb.AppendLine(this._msgid.F(s.Strings[0]));
                    sb.AppendLine(this._msgid_plural.F(s.Strings[1]));

                    for (int i = 0; i < pluralsCount; i++)
                    {
                        sb.AppendLine(this._msgstr_plural.F(i));
                    }
                }

                sb.AppendLine();
            }

            File.WriteAllText(Path.Combine(translationDirectory, translationInfo.TranslationName + ".po"), sb.ToString());

            Debug.Log("Gettext translation export - Complete.");
        }