Beispiel #1
0
        public static void ExportEmptyValues(LocalisationTable locTable, LocalisationKeySchema currentSchema)
        {
            if (locTable == null || currentSchema == null)
            {
                return;
            }

            var path = GuiUtils.SaveCsvFileDialog($"{locTable.name}-0.csv");

            if (!string.IsNullOrEmpty(path))
            {
                Exporter.ExportTableToCsv(path, locTable, currentSchema, true);
            }
        }
Beispiel #2
0
        /*
         * FORMAT:
         * Category,Key,Content
         * Category,Key,"Complex, Content"
         */
        private void LoadFromFile(
            LocalisationTable locTable,
            LocalisationKeySchema currentSchema,
            bool emptyValuesOnly = false,
            bool saveAssets      = true)
        {
            if (locTable == null)
            {
                throw new ArgumentNullException(nameof(locTable));
            }
            if (currentSchema == null)
            {
                throw new ArgumentNullException(nameof(currentSchema));
            }

            var path = GuiUtils.OpenCsvFileDialog();

            if (!string.IsNullOrEmpty(path))
            {
                Importer.ImportFromCsv(path, targetTable, currentSchema, emptyValuesOnly, saveAssets);
            }
        }
Beispiel #3
0
        public static int FindEmptyValues(LocalisationTable locTable, LocalisationKeySchema currentSchema,
                                          bool fixMissing = false)
        {
            var emptyValues = 0;

            foreach (var category in currentSchema.categories)
            {
                if (category == null || category.keys == null)
                {
                    continue;
                }

                foreach (var key in category.keys)
                {
                    var locKeyCRC = CrcUtils.GetCrc(category.name, key);
                    var locValue  = string.Empty;

                    try
                    {
                        locValue = locTable.GetString(locKeyCRC);
                    }
                    catch
                    {
                        if (fixMissing)
                        {
                            locTable.SetData(locKeyCRC, string.Empty);
                        }
                    }

                    if (string.IsNullOrEmpty(locValue))
                    {
                        emptyValues++;
                    }
                }
            }

            return(emptyValues);
        }
        /// <summary>
        /// Generates the LocalisationConfig class from the supplied key schema - this allows application-side code to access the available keys
        /// and request localised content with them. Also exports some cross functionality such as the Localisation folder path which the application
        /// will need.
        /// </summary>
        public static void GenerateLocalisationConfig(TextAsset configTemplate, LocalisationKeySchema schema)
        {
            if (configTemplate == null)
            {
                throw new Exception("No template supplied - can't generate localisation config file");
            }

            var filePath      = $"{Application.dataPath}/{LocalisationSettings.Current.CodeGenerationFilePath}";
            var directoryPath = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            if (File.Exists(filePath))
            {
                var renamedPath = filePath + CONFIG_REPLACE_SUFFIX;
                if (File.Exists(renamedPath))
                {
                    File.Delete(renamedPath);
                }

                File.Delete(filePath);
            }

            Debug.Log("Writing to " + filePath);
            var totalKeys = 0;

            var stringBuilder = new StringBuilder();

            foreach (var category in schema.categories)
            {
                stringBuilder.AppendLine($"\t\tpublic enum {category.name}");
                stringBuilder.AppendLine("\t\t{");
                for (var i = 0; i < category.keys.Length; i++)
                {
                    var key = category.keys[i];

                    stringBuilder.Append("\t\t\t" + key + " = " + CrcUtils.GetCrc(category.name, key));
                    if (i < category.keys.Length - 1)
                    {
                        stringBuilder.AppendLine(",");
                    }
                    else
                    {
                        stringBuilder.AppendLine();
                    }
                }
                stringBuilder.AppendLine("\t\t}");
                totalKeys += category.keys.Length;
            }

            var outputText = configTemplate.text;

            outputText = outputText.Replace("{CLASS}", CONFIG_LOC_CLASS_NAME);
            outputText = outputText.Replace("{KEYS}", stringBuilder.ToString());
            File.WriteAllText(filePath, outputText);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

            Debug.Log($"Successfully generated {totalKeys} localisation keys.");
        }
Beispiel #5
0
        private void DrawLocalisationContents(LocalisationKeySchema currentSchema)
        {
            GUILayout.Label("Localisation texts", EditorStyles.boldLabel);

            showCRC = EditorGUILayout.Toggle("Show CRC values", showCRC);
            EditorGUILayout.LabelField("CRC version: ", crcEncodingVersion.intValue.ToString(), EditorStyles.helpBox);

            if (currentSchema.categories.Length > contentToggles.Length)
            {
                contentToggles = new bool[currentSchema.categories.Length];
            }

            keyMap.Clear();
            for (var i = 0; i < localisationKeys.arraySize; i++)
            {
                // mapping of locKey to its index in the keysArray (i.e. Array.IndexOf lookup-table)
                keyMap.Add(localisationKeys.GetArrayElementAtIndex(i).intValue, i);
            }

            var updateKeyEncoding = (crcEncodingVersion.intValue >= 0 &&
                                     crcEncodingVersion.intValue != CrcUtils.KEY_CRC_ENCODING_VERSION);

            for (var i = 0; i < currentSchema.categories.Length; i++)
            {
                var category = currentSchema.categories[i];

                contentToggles[i] = EditorGUILayout.Foldout(contentToggles[i], category.name);
                if (contentToggles[i] || updateKeyEncoding)
                {
                    EditorGUI.indentLevel++;
                    for (var j = 0; j < category.keys.Length; j++)
                    {
                        var locKeyCRC =
                            CrcUtils.GetCrcWithEncodingVersion(category.name, category.keys[j], crcEncodingVersion.intValue);

                        if (contentToggles[i])
                        {
                            if (!showCRC)
                            {
                                EditorGUILayout.BeginHorizontal();
                            }
                            EditorGUILayout.LabelField(
                                string.Format("{0} {1}", category.keys[j], showCRC ? "(" + locKeyCRC + ")" : string.Empty),
                                EditorStyles.boldLabel, GUILayout.MaxWidth(showCRC ? 200f : 140f));
                        }

                        var currentValue = string.Empty;
                        SerializedProperty currentValueProperty = null;

                        if (keyMap.ContainsKey(locKeyCRC))
                        {
                            var currentArrayIndex = keyMap[locKeyCRC];

                            if (updateKeyEncoding)
                            {
                                keyMap.Remove(locKeyCRC);
                                locKeyCRC = CrcUtils.GetCrc(category.name, category.keys[j]);
                                localisationKeys.GetArrayElementAtIndex(currentArrayIndex).intValue = locKeyCRC;
                                keyMap.Add(locKeyCRC, currentArrayIndex);
                            }

                            currentValueProperty = localisationValues.GetArrayElementAtIndex(currentArrayIndex);
                            currentValue         = currentValueProperty.stringValue;
                        }

                        if (contentToggles[i])
                        {
                            var newValue = EditorGUILayout.DelayedTextField(currentValue);
                            if (!showCRC)
                            {
                                EditorGUILayout.EndHorizontal();
                            }

                            if (newValue != currentValue)
                            {
                                if (currentValueProperty != null)
                                {
                                    currentValueProperty.stringValue = newValue;
                                }
                                else
                                {
                                    localisationKeys.arraySize++;
                                    localisationValues.arraySize++;
                                    localisationKeys.GetArrayElementAtIndex(localisationKeys.arraySize - 1).intValue        = locKeyCRC;
                                    localisationValues.GetArrayElementAtIndex(localisationValues.arraySize - 1).stringValue = newValue;
                                }
                            }
                        }
                    }

                    EditorGUI.indentLevel--;
                }
            }

            if (updateKeyEncoding)
            {
                crcEncodingVersion.intValue = CrcUtils.KEY_CRC_ENCODING_VERSION;
                Debug.Log(string.Format("Localisation table '{0}' updated key CRC encoding to latest version: {1}",
                                        targetTable.name, crcEncodingVersion.intValue));
            }

            serializedObject.ApplyModifiedProperties();
        }