Exemplo n.º 1
0
        /// <summary>
        /// Initialises the localisation system in a folder in Resources/ where the localisation table assets are found.
        /// </summary>
        /// <param name="path">Filepath to the folder of localisation tables</param>
        /// <param name="cultureName">Optional culture name to initialise with. Falls back to default culture if not passed in or invalid</param>
        public static bool Initialise(string path, string cultureName = null)
        {
            CurrentLocale = NameToCultureInfo(cultureName);

            validTablePaths.Clear();
            allTablePaths.Clear();

            var localisationTables = Resources.LoadAll <LocalisationTable>(path);

            if (localisationTables.Length == 0)
            {
                throw new ArgumentException(string.Format("No localisation tables found at path: {0}", path));
            }

            foreach (var locTable in localisationTables)
            {
                var fullPath = Path.Combine(path, locTable.name);

                allTablePaths.Add(locTable.name, fullPath);

                if (locTable.SupportedLocales.Count == 0)
                {
                    Debug.LogWarning(string.Format(
                                         "Did not initialise localisation table '{0}' as it has no supported locales so would never be used.",
                                         locTable.name));
                    continue;
                }

                foreach (var localeName in locTable.SupportedLocales)
                {
                    if (validTablePaths.ContainsKey(localeName))
                    {
                        Debug.LogWarning(string.Format("Localisation table contains locale '{0}' which has already been added!",
                                                       localeName));
                    }
                    else
                    {
                        validTablePaths.Add(localeName, fullPath);

                        if (localeName == CurrentLocale.Name && Application.isPlaying)
                        {
                            currentLocalisationTable = locTable;
                        }
                        else if (!locTable.SupportedLocales.Contains(CurrentLocale.Name))
                        {
                            Resources.UnloadAsset(locTable);
                        }
                    }
                }
            }

            if (currentLocalisationTable == null && Application.isPlaying)
            {
                Debug.LogWarning(string.Format("Error: unsupported locale: {0}, switching to {1}", CurrentLocale.Name, DefaultCulture));
                SwitchCulture(DefaultCulture);
            }

            return(Initialised);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Switches the application to the specified culture
        /// <param name="culture">The culture to switch to</param>
        /// <returns>bool - if the culture was switched to successfully</returns>
        /// </summary>
        public static bool SwitchCulture(string culture)
        {
            var loadedTable = LoadLocalisationTable(culture);

            if (loadedTable == null)
            {
                Debug.LogWarning(string.Format("Error loading localisation table for locale: {0}", culture));
                return(false);
            }

            CurrentLocale            = new CultureInfo(culture);
            currentLocalisationTable = loadedTable;

            if (OnLocaleChanged != null)
            {
                OnLocaleChanged.Invoke();
            }

            return(true);
        }