private void SaveInternal(TranslationCollection translations, Dictionary<int, string> DisplayFolderLanguages, Dictionary<int, string> DescriptionLanguages, SSAS.TabularTranslationObjectAnnotation annotation)
        {
            List<int> listAllLanguages = new List<int>(Languages.Keys);
            foreach (int iLang in DisplayFolderLanguages.Keys)
            {
                if (!listAllLanguages.Contains(iLang))
                    listAllLanguages.Add(iLang);
            }
            foreach (int iLang in DescriptionLanguages.Keys)
            {
                if (!listAllLanguages.Contains(iLang))
                    listAllLanguages.Add(iLang);
            }

            List<SSAS.TabularTranslationAnnotation> listAnnotations = new List<TabularTranslationAnnotation>();
            translations.Clear();
            foreach (int iLang in listAllLanguages)
            {
                Translation t = new Translation(iLang);
                if (translations is AttributeTranslationCollection)
                    t = new AttributeTranslation(iLang);
                if (DisplayFolderLanguages.ContainsKey(iLang))
                    t.DisplayFolder = DisplayFolderLanguages[iLang];
                if (DescriptionLanguages.ContainsKey(iLang))
                    t.Description = DescriptionLanguages[iLang];

                if (Languages.ContainsKey(iLang) && !string.IsNullOrEmpty(Languages[iLang]))
                    t.Caption = Languages[iLang];
                else if (!string.IsNullOrEmpty(t.DisplayFolder) || !string.IsNullOrEmpty(t.Description))
                    t.Caption = this.DefaultLanguage; //this works around a problem where if they translate the display folder or the description but not the caption, then the caption ends up blank
                
                if (!string.IsNullOrEmpty(t.Caption) || !string.IsNullOrEmpty(t.Description) || !string.IsNullOrEmpty(t.DisplayFolder))
                {
                    translations.Add(t);
                    SSAS.TabularTranslationAnnotation tranAnnotation = new TabularTranslationAnnotation();
                    tranAnnotation.Caption = t.Caption;
                    tranAnnotation.Description = t.Description;
                    tranAnnotation.DisplayFolder = t.DisplayFolder;
                    tranAnnotation.Language = t.Language;
                    listAnnotations.Add(tranAnnotation);
                }
            }

            if (annotation != null)
                annotation.TabularTranslations = listAnnotations.ToArray();
        }
        public TabularTranslatedItem(string table, IModelComponent obj, TabularTranslatedItemProperty property, TabularTranslatedItem caption, SSAS.TabularTranslationsAnnotation annotations)
        {
            _Table = table;
            _object = obj;
            _Property = property;

            TranslationCollection translations;
            string sCaption;
            string sDescription;
            string sDisplayFolder = null;
            if (obj is DimensionAttribute)
            {
                _ObjectType = TabularTranslatedItemType.Column;
                DimensionAttribute typedobj = (DimensionAttribute)obj;
                translations = typedobj.Translations;
                sDisplayFolder = typedobj.AttributeHierarchyDisplayFolder;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Hierarchy)
            {
                _ObjectType = TabularTranslatedItemType.Hierarchy;
                Hierarchy typedobj = (Hierarchy)obj;
                translations = typedobj.Translations;
                sDisplayFolder = typedobj.DisplayFolder;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Level)
            {
                _ObjectType = TabularTranslatedItemType.Level;
                Level typedobj = (Level)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is CalculationProperty)
            {
                _ObjectType = TabularTranslatedItemType.Measure;
                CalculationProperty typedobj = (CalculationProperty)obj;
                translations = typedobj.Translations;
                sDisplayFolder = typedobj.DisplayFolder;
                sCaption = typedobj.CalculationReference;
                sDescription = typedobj.Description;

                if (sCaption.StartsWith("[") && sCaption.EndsWith("]"))
                {
                    sCaption = sCaption.Substring(1, sCaption.Length - 2);
                }
            }
            else if (obj is Database)
            {
                _ObjectType = TabularTranslatedItemType.Database;
                Database typedobj = (Database)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Cube)
            {
                _ObjectType = TabularTranslatedItemType.Cube;
                Cube typedobj = (Cube)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Perspective)
            {
                _ObjectType = TabularTranslatedItemType.Perspective;
                Perspective typedobj = (Perspective)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Dimension)
            {
                _ObjectType = TabularTranslatedItemType.Table;
                Dimension typedobj = (Dimension)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Microsoft.AnalysisServices.Action)
            {
                _ObjectType = TabularTranslatedItemType.Action;
                Microsoft.AnalysisServices.Action typedobj = (Microsoft.AnalysisServices.Action)obj;
                translations = typedobj.Translations;
                sCaption = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else
            {
                throw new Exception("Unexpected object type: " + obj.GetType().Name);
            }

            _ObjectName = sCaption;

            if (property == TabularTranslatedItemProperty.Caption)
            {
                _DefaultLanguage = sCaption;

                SSAS.TabularTranslationObjectAnnotation annotation = annotations.Find(obj);
                if (annotation != null && translations.Count == 0)
                {
                    foreach (SSAS.TabularTranslationAnnotation tranAnnotation in annotation.TabularTranslations)
                    {
                        Translation t = new Translation(tranAnnotation.Language);
                        if (obj is DimensionAttribute)
                            t = new AttributeTranslation(tranAnnotation.Language);
                        t.Caption = tranAnnotation.Caption;
                        t.Description = tranAnnotation.Description;
                        t.DisplayFolder = tranAnnotation.DisplayFolder;
                        translations.Add(t);
                    }
                    _restoredTranslations = true;
                }
            
            }
            else if (property == TabularTranslatedItemProperty.Description)
            {
                _DefaultLanguage = sDescription;
            }
            else if (property == TabularTranslatedItemProperty.DisplayFolder)
            {
                _DefaultLanguage = sDisplayFolder;
            }

            Languages = new Core.DirtyMonitoredDictionary<int, string>();
            foreach (Translation t in translations)
            {
                if (property == TabularTranslatedItemProperty.Caption)
                {
                    Languages.Add(t.Language, t.Caption);
                }
                else if (property == TabularTranslatedItemProperty.Description)
                {
                    Languages.Add(t.Language, t.Description);
                }
                else if (property == TabularTranslatedItemProperty.DisplayFolder)
                {
                    Languages.Add(t.Language, t.DisplayFolder);
                }
            }

            if (caption != null)
            {
                caption.DependentProperties.Add(this);
            }
        }