Difference between revisions of "LocalizedString class"
Nguyenduyan (talk | contribs) (Created page with "{{CGscriptClass_Template |Name=<nowiki>LocalizedString</nowiki> |Description=<nowiki>Represents translation of a string.</nowiki> |Constructors= {{CGscriptConstructors_Templat...") |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
|Name=<nowiki>LocalizedString</nowiki> | |Name=<nowiki>LocalizedString</nowiki> | ||
|Description=<nowiki>Represents translation of a string.</nowiki> | |Description=<nowiki>Represents translation of a string.</nowiki> | ||
− | |Constructors= | + | |InheritsFrom=object|Constructors= |
{{CGscriptConstructors_Template|Description=<nowiki>Create a new translatable text</nowiki>}} | {{CGscriptConstructors_Template|Description=<nowiki>Create a new translatable text</nowiki>}} | ||
+ | {{CGscriptConstructors_Template|Parameters= | ||
+ | {{CGscriptParameters_Template|Type=Dictionary|Name=<nowiki>dictionary</nowiki>|Description=<nowiki>Dictionary with existing values</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=string|Name=<nowiki>defaultIsocode</nowiki>|Description=<nowiki>If given use this isocodes value in the dictionary as the default</nowiki>}} | ||
+ | |Description=<nowiki>Create a new translatable text based on existing dictionary</nowiki>}} | ||
|Methods= | |Methods= | ||
{{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>GetSpecificTranslation</nowiki>|Parameters= | {{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>GetSpecificTranslation</nowiki>|Parameters= | ||
Line 18: | Line 21: | ||
{{CGscriptParameters_Template|Type=string|Name=<nowiki>isocode</nowiki>|Description=<nowiki>Isocode for language. Use empty for default text</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=string|Name=<nowiki>s</nowiki>|Description=<nowiki>New translation</nowiki>}} | {{CGscriptParameters_Template|Type=string|Name=<nowiki>isocode</nowiki>|Description=<nowiki>Isocode for language. Use empty for default text</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=string|Name=<nowiki>s</nowiki>|Description=<nowiki>New translation</nowiki>}} | ||
|Description=<nowiki>Set a specific translation</nowiki>}} | |Description=<nowiki>Set a specific translation</nowiki>}} | ||
− | {{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Description=<nowiki>The string representation of the object.</nowiki>}} | + | {{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>SetTranslation</nowiki>|Parameters= |
+ | {{CGscriptParameters_Template|Type=Dictionary|Name=<nowiki>dictionary</nowiki>|Description=<nowiki>Dictionary of iso code and translation</nowiki>}} | ||
+ | |Description=<nowiki>Set all translations. Removes the rest.</nowiki>}} | ||
+ | {{CGscriptMethods_Template|ReturnType=Dictionary|Name=<nowiki>ToDictionary</nowiki>|Description=<nowiki>Convert localized object to a dictionary with key is iso code</nowiki>}} | ||
+ | {{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Inherited=object|Description=<nowiki>The string representation of the object.</nowiki>}} | ||
|Properties= | |Properties= | ||
{{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}} | {{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}} | ||
− | {{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Description=<nowiki>Get information about this class.</nowiki>}} | + | {{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Inherited=object|Description=<nowiki>Get information about this class.</nowiki>}} |
}} | }} | ||
+ | |||
+ | === <span style="color:#DF8621">'''Examples'''</span> === | ||
+ | <span style="color:#DF8621"> Create a new translatable text </span> | ||
+ | <source lang="javascript"> | ||
+ | LocalizedString localizedString = new LocalizedString (); | ||
+ | //SetTranslation | ||
+ | localizedString.SetTranslation("en-GB", "En text"); | ||
+ | localizedString.SetTranslation("da-DK", "Da text"); | ||
+ | //GetTranslation | ||
+ | print(localizedString.GetSpecificTranslation("en-GB"));//En text | ||
+ | print(localizedString.GetSpecificTranslation("da-DK"));//Da text | ||
+ | print(localizedString.GetSpecificTranslation("en-US"));//Empty | ||
+ | print(localizedString.GetTranslation("en-US"));//En text | ||
+ | print(localizedString.GetTranslationForLoggedInUser());//"Da text" if languge of login user is Danish | ||
+ | //RemoveTranslation | ||
+ | localizedString.RemoveTranslation("en-GB"); | ||
+ | print(localizedString.GetSpecificTranslation("en-GB"));//Empty | ||
+ | </source> | ||
+ | <span style="color:#DF8621"> Create a new translatable text based on existing dictionary </span> | ||
+ | <source lang="javascript"> | ||
+ | Dictionary translation = { | ||
+ | "en-GB":"En text", | ||
+ | "da-DK":"Da text" | ||
+ | }; | ||
+ | LocalizedString localizedString = new LocalizedString (translation, "da-DK"); | ||
+ | //GetTranslation | ||
+ | print(localizedString.GetSpecificTranslation(""));//Da text | ||
+ | //SetTranslation | ||
+ | localizedString.SetTranslation("","Danish text"); | ||
+ | print(localizedString.GetSpecificTranslation(""));//Danish text | ||
+ | </source> | ||
+ | |||
+ | <span style="color:#DF8621"> Set Title and Description for Axis </span> | ||
+ | <source lang="javascript"> | ||
+ | DCS_use(15636575); | ||
+ | Axis axis = AxisSet_getAxis("Gender"); | ||
+ | //set Titles | ||
+ | axis.LocalizedTitles.SetTranslation("en-GB", "Title En text"); | ||
+ | axis.LocalizedTitles.SetTranslation("da-DK", "title Da text"); | ||
+ | //set Texts | ||
+ | axis.LocalizedTexts.SetTranslation("en-GB", "Description En text"); | ||
+ | axis.LocalizedTexts.SetTranslation("da-DK", "Description Da text"); | ||
+ | AxisSet_save(); | ||
+ | print(Catglobe.Json.Encode(axis));//{"id":"Gender","title":{"en-GB":"Title En text","da-DK":"title Da text"},"desc":{"":"123","en-GB":"Description En text","da-DK":"Description Da text"},"mathbase":"Gender","mathbasemod":"","pctbase":"Gender != empty","color":"#00000000","options":[{"title":{"":"Male"},"expression":"","pctbase":"","color":"#00000000","tags":[],"accessGroups":[]},{"title":{"":"Female"},"expression":"","pctbase":"","color":"#00000000","tags":[],"accessGroups":[]}],"tags":[],"accessGroups":[]} | ||
+ | </source> | ||
+ | <span style="color:#DF8621">Set Question text </span> | ||
+ | <source lang="javascript"> | ||
+ | QuestionnaireTemplate qt = new QuestionnaireTemplate (new Questionnaire(17148177).TemplateId); | ||
+ | qt.GetQuestion("Q1").Text = new LocalizedString ({"": "Q1 text UK", "da-DK": "Q1 text DK"}, "en-GB"); | ||
+ | qt.Save(true); | ||
+ | print(qt.GetQuestion("Q1").Text.ToDictionary()); | ||
+ | </source> |
Latest revision as of 08:46, 11 May 2022
LocalizedString
Represents translation of a string.
Parent class
Inherits from object
Constructors
- () - Create a new translatable text
- (Dictionary dictionary "Dictionary with existing values", string defaultIsocode "If given use this isocodes value in the dictionary as the default") - Create a new translatable text based on existing dictionary
Methods
- string GetSpecificTranslation(string isocode "Isocode for language. Use empty for default text") - Get the translation for given isocode or null
- string GetTranslation(string isocode "Isocode for language. Use empty for default text") - Get the best matching translation for given isocode
- string GetTranslationForLoggedInUser() - Get the best matching translation for the current user
- bool RemoveTranslation(string isocode "Isocode for language. Use empty for default text") - Remove a specific translation
- Empty SetTranslation(string isocode "Isocode for language. Use empty for default text", string s "New translation") - Set a specific translation
- Empty SetTranslation(Dictionary dictionary "Dictionary of iso code and translation") - Set all translations. Removes the rest.
- Dictionary ToDictionary() - Convert localized object to a dictionary with key is iso code
- (From object) string ToString() - The string representation of the object.
Properties
- string ObjectTypeName { get; } - The name of the type of object.
- (From object) TypeInformation TypeInformation { get; } - Get information about this class.
Examples
Create a new translatable text
LocalizedString localizedString = new LocalizedString ();
//SetTranslation
localizedString.SetTranslation("en-GB", "En text");
localizedString.SetTranslation("da-DK", "Da text");
//GetTranslation
print(localizedString.GetSpecificTranslation("en-GB"));//En text
print(localizedString.GetSpecificTranslation("da-DK"));//Da text
print(localizedString.GetSpecificTranslation("en-US"));//Empty
print(localizedString.GetTranslation("en-US"));//En text
print(localizedString.GetTranslationForLoggedInUser());//"Da text" if languge of login user is Danish
//RemoveTranslation
localizedString.RemoveTranslation("en-GB");
print(localizedString.GetSpecificTranslation("en-GB"));//Empty
Create a new translatable text based on existing dictionary
Dictionary translation = {
"en-GB":"En text",
"da-DK":"Da text"
};
LocalizedString localizedString = new LocalizedString (translation, "da-DK");
//GetTranslation
print(localizedString.GetSpecificTranslation(""));//Da text
//SetTranslation
localizedString.SetTranslation("","Danish text");
print(localizedString.GetSpecificTranslation(""));//Danish text
Set Title and Description for Axis
DCS_use(15636575);
Axis axis = AxisSet_getAxis("Gender");
//set Titles
axis.LocalizedTitles.SetTranslation("en-GB", "Title En text");
axis.LocalizedTitles.SetTranslation("da-DK", "title Da text");
//set Texts
axis.LocalizedTexts.SetTranslation("en-GB", "Description En text");
axis.LocalizedTexts.SetTranslation("da-DK", "Description Da text");
AxisSet_save();
print(Catglobe.Json.Encode(axis));//{"id":"Gender","title":{"en-GB":"Title En text","da-DK":"title Da text"},"desc":{"":"123","en-GB":"Description En text","da-DK":"Description Da text"},"mathbase":"Gender","mathbasemod":"","pctbase":"Gender != empty","color":"#00000000","options":[{"title":{"":"Male"},"expression":"","pctbase":"","color":"#00000000","tags":[],"accessGroups":[]},{"title":{"":"Female"},"expression":"","pctbase":"","color":"#00000000","tags":[],"accessGroups":[]}],"tags":[],"accessGroups":[]}
Set Question text
QuestionnaireTemplate qt = new QuestionnaireTemplate (new Questionnaire(17148177).TemplateId);
qt.GetQuestion("Q1").Text = new LocalizedString ({"": "Q1 text UK", "da-DK": "Q1 text DK"}, "en-GB");
qt.Save(true);
print(qt.GetQuestion("Q1").Text.ToDictionary());