Guides and Articles

Use this documentation when building your Tekla Structures apps

Code example: Localization and translation

Updated: 16 Jan 2018

This Tekla Open API code snippet shows how to create a separate localization instance.

Enable localization of several Forms (using the Localization field of Form1), without inheriting from ApplicationFormBase and connect to Tekla Structures through a model object.

Note:
Unfortunately, the whole source code of this example is not available for downloading. Please copy-paste the following code snippet to your project.

using TeklaModel = Tekla.Structures.Model.Model;
using TeklaLocalization = Tekla.Structures.Dialog.Localization; 
 
namespace LocalizationExample
{     
    public partial class Form1 : Form     
    {         
        private static TeklaModel _model;         
        private static TeklaLocalization _localization;                  
        public Form1()         
        {             
            InitializeComponent();             
            Localization.Localize(this);         
        } 
        // Create a new Model object.
        public TeklaModel Model         
        {             
            get             
            {                 
                if (_model == null)                 
                {                     
                    _model = new TeklaModel();                 
                }                 
                return _model;             
            }         
        } 
        // Create a separate localization instance
        public TeklaLocalization Localization         
        {             
            get             
            {                 
                if (_localization == null)                 
                {                     
                    string language = string.Empty;                     
                    string languageFile = string.Empty; 
 
                    Model.GetAdvancedOption("XS_LANGUAGE", ref language);                     		
                    Model.GetAdvancedOption("XS_DIR", ref languageFile); 
                    languageFile = Path.Combine(languageFile, @"messages\DotNetDialogStrings.xml");                                          
                    _localization = new TeklaLocalization();     
                    _localization.LoadFile(languageFile);
                    _localization.Language = GetShortLanguage(language);
                }
                return _localization;             
            }         
        } 
 	
        private static string GetShortLanguage(string Language)         
        {             
            switch (Language)             
            {                 
                case "ENGLISH":
                    return "enu";                 
                case "DUTCH":
                    return "nld";
                case "FRENCH":     
                    return "fra";          
                case "GERMAN":                   
                    return "deu";
                case "ITALIAN":           
                    return "ita";       
                case "SPANISH":                   
                    return "esp";     
                case "JAPANESE":             
                    return "jpn";           
                case "CHINESE SIMPLIFIED":              
                    return "chs";           
                case "CHINESE TRADITIONAL":          
                    return "cht";           
                case "CZECH":              
                    return "csy";         
                case "PORTUGUESE BRAZILIAN":  
                    return "ptb";                
                case "HUNGARIAN":     
                    return "hun";    
                case "POLISH":     
                    return "plk";     
                case "RUSSIAN":       
                    return "rus";                  
                default:      
                    return "enu";             
            }         
        }     
    }
}

Load several localization files by calling method LoadFile for each of them. The translations are searched in the loading order and the first match is returned.