Manage field translations programmatically is also possible and therefore useful as you might import your products from another system and therefore also want to import the translations via the import.
Therefore, load the item, where you want to update or add your translation. Then you have to check, if the translation for your language already exists. If the translation exists, you only have to update the translation, if it has changed. If the translation doesn’t exists, you have to add the translation as a new Localized Label.
Keep in mind, that if you want to create a new product, the connected user has to set the language to the base language (usually English). Otherwise, you will get the following error message:
System.ServiceModel.FaultException`1: ‘Rows with localizable attributes can only be created when the user interface (UI) language for the current user is set to the organization’s base language.’
With the following SourceCode, you can create a new product and add translations to the product.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
static void Main(string[] args) { using (var orgService = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString)) { // creating a new Test product var product = new Entity("product"); product.Attributes.Add("description", "This is a test product"); product.Attributes.Add("name", "English Test product"); product.Attributes.Add("productnumber", "123456789"); product.Attributes.Add("defaultuomid", new EntityReference("uom", new Guid("F13F6EFF-5820-43EA-95A2-2F230DB30FE6"))); product.Attributes.Add("pricelevelid", new EntityReference("pricelevel", new Guid("d437e569-e7c2-e411-80df-fc15b42886e8"))); product.Attributes.Add("defaultuomscheduleid", new EntityReference("uomschedule", new Guid("69fdad7a-735a-436d-802b-bc4254abe76c"))); var id = orgService.Create(product); var locLabName = new List<LocalizedLabel> { new LocalizedLabel("Deutsches Test Produkt", 1031), new LocalizedLabel("Italian Test Product", 1040), new LocalizedLabel("Japanese Test Product", 1041) }; AddLocalizedLabelsToItem(orgService, new EntityReference("product", id), "name", locLabName); } } /// <summary> /// Add a list of localized labes to item /// </summary> /// <returns> /// True if localized labels are set, false if reference is null or localized labels are empty /// </returns> /// <remarks> /// Only translations for installed language packs can be added. If you add any language which /// is not installed it wont be set and you WONT get any error! /// </remarks> public static bool AddLocalizedLabelsToItem(IOrganizationService orgService, EntityReference reference, string attributeName, List<LocalizedLabel> localizedLabels) { if (reference != null && localizedLabels != null && localizedLabels.Any()) { var setLocLabelsRequest = new SetLocLabelsRequest { AttributeName = attributeName, EntityMoniker = reference, Labels = localizedLabels.ToArray() }; var setResponse = (SetLocLabelsResponse)orgService.Execute(setLocLabelsRequest); return true; } return false; } |
Changing the language will also use the translation for the name.
This translation will also be displayed, if the product is used as a lookup in another form.