getCultureInfo

Posted by: dean.kinnear on 13 February 2025, 12:21 pm EST

    • Post Options:
    • Link

    Posted 13 February 2025, 12:21 pm EST

    I am trying to get culture info for another locale.

    I’m in en-us locale, and I want to get UK culture info:

    var locale = GC.Spread.Common.CultureManager.getCultureInfo(‘en-GB’);

    But this always returns null.

    What am I doing wrong?

  • Posted 14 February 2025, 3:19 am EST

    Hi,

    To obtain cultural information about cultures other than “en-US,” “ja-JP,” and “zh-CN,” you must include the SpreadJS globalization script in your project and add other cultures to the Culture Manager fetched from the script.

    Please refer to the attached sample, which uses the globalization script to add other cultures to the Culture Manager and fetches the UK culture information (see below).

    Please refer to the following resources to learn more about the globalization of cultures in SpreadJS:

    Please feel free to reach out if you encounter any further issues or require additional guidance.

    Attachment: (UK Culture) https://jscodemine.mescius.io/sample/ful1ATPcykyfMWTif6qV7Q/

    Best Regards,

  • Posted 14 February 2025, 10:56 am EST

    Thanks for the information!

    I need to globalize dates only.

    What I have done is detect if current local is not en-US,

    Find the curent locale culture info, then just make current datetime format the same as the locale.

    The underlying value changes datetime format, but the display remains.

    How do I get the display to update as well as in the demo provided? Thanks!

    if (currentLocale != 'en-US') {
    	var defaultCulture = GC.Spread.Common.CultureManager.culture();
    	var defaultCultureInfo = GC.Spread.Common.CultureManager.getCultureInfo(defaultCulture);
    
    	var currentCultureInfo = cultureInfos?.find(q => q.name() == currentLocale);
    	if (currentCultureInfo != null) {
    		var dateTimeFormat = currentCultureInfo.DateTimeFormat;
    		defaultCultureInfo.DateTimeFormat = dateTimeFormat
    		GC.Spread.Common.CultureManager.addCultureInfo(defaultCulture, defaultCultureInfo);
    		spread.refresh();
    	}
    }
    

    chrome_LTyX4rpxM1.zip

  • Posted 17 February 2025, 2:27 am EST - Updated 17 February 2025, 2:32 am EST

    Hi,

    Please note that this custom or explicitly defined format in the cell will remain unchanged except for language regardless of any alterations in cultural settings.

    Additionally, the behavior you’re experiencing is by design. If a cell has a specific format defined, any changes in cultural settings won’t impact that format except for language. Cultural settings affect the editing mode format and those that have auto format applied by spreadjs.

    For instance, in cells B1 and C1, I’ve entered date values that default to the short date format due to SpreadJS parsing and applying auto-formatting. Consequently, both B1 and C1 will have the short-date format by default. Then, I manually applied the mm/dd/yyyy format to cell B1, explicitly defining its format. Meanwhile, cell C1 does not have an explicitly defined format. Therefore, when changing cultural settings, only the format of cell C1 will be affected, as no specific format was set, whereas, for cell B1, only the editing mode format will be impacted. You can confirm this by double-clicking on cell B1. Please refer to the attached GIF “Verify.gif” and sample for clarification.

    Verify Gif:

    Sample: https://jscodemine.mescius.io/share/pegigx-kDEqhtUkqlda6aQ/?IsEmbed=false&Theme=Unset&PreviewDirection=0&IsEditorShow=true&IsExplorerShow=true&IsPreviewShow=true&IsConsoleShow=true&IsRunBTNShow=false&IsResetBTNShow=false&IsOpenInCodemineBTNShow=false&PanelWidth=20&PanelWidth=50&PanelWidth=30&defaultOpen={"OpenedFileName"%3A["%2Findex.html"]%2C"ActiveFile"%3A"%2Findex.html"}

    Regarding “The underlying value changes datetime format, but the display remains.”, it seems that formatting has been explicitly applied to the cell, which is why it does not change when the culture is modified in your end.

    Refer to the sample and the attached GIF “Steps.gif”, where the date is automatically formatted according to the culture when the culture is changed.

    if (currentLocale != 'en-US') {
      var defaultCulture = GC.Spread.Common.CultureManager.culture();
      var defaultCultureInfo = GC.Spread.Common.CultureManager.getCultureInfo(defaultCulture);
      var currentCultureInfo = cultureInfos?.find(q => q.name() == currentLocale);
      if (currentCultureInfo != null) {
        var dateTimeFormat = currentCultureInfo.DateTimeFormat;
        defaultCultureInfo.DateTimeFormat = dateTimeFormat;
        const newDefaultCultureName = "NewCulture";
        GC.Spread.Common.CultureManager.addCultureInfo(newDefaultCultureName, defaultCultureInfo);
        GC.Spread.Common.CultureManager.culture(newDefaultCultureName);
      }
    }

    Steps gif:

    Sample: https://jscodemine.mescius.io/share/6UW18PeOr0_t3mjpsj_NCw/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"]%2C"ActiveFile"%3A"%2Findex.html"}

    Please feel free to reach out if you encounter any further issues or require additional guidance.

    Best Regards,

  • Posted 18 February 2025, 3:31 pm EST

    If I enter a date in en-US, then hit set en-GB format, it formats correctly. But if I afterwards I enter a date in en-GB format, it is not recognized as a date. If I enter the date in en-US format, it is translated into en-GB date format.

    How can I get spread js to recognize en-GB format after I set it?

    chrome_gzusRxZh3X.zip

  • Posted 19 February 2025, 7:08 am EST

    Hi,

    We can replicate the behavior you mentioned on our end. However, this behavior can be overridden by deleting the property $Pt from the default culture which is added to the culture as soon as a value is added in the SpreadJS cell.

    Please refer to the code snippet below that works according to the above workaround and helps to write the date in the locale-specific format -

    function setCultureDateTimeFormat(currentLocale) {
        const spread = GC.Spread.Sheets.findControl(document.getElementById("ss"));
        spread.suspendPaint();
        if (currentLocale != 'en-US') {
    	var defaultCulture = GC.Spread.Common.CultureManager.culture();
    	var defaultCultureInfo = GC.Spread.Common.CultureManager.getCultureInfo(defaultCulture);
        	var currentCultureInfo = cultureInfos?.find(q => q.name() == currentLocale);
    	if (currentCultureInfo != null) {
                var dateTimeFormat = currentCultureInfo.DateTimeFormat;
        	    defaultCultureInfo.DateTimeFormat = dateTimeFormat;  
                if(defaultCultureInfo["$Pt"] != null) {
                    delete defaultCultureInfo["$Pt"];
                }
                const newDefaultCultureName = `${defaultCulture}_${window.defaultCultureCount++}`;
                GC.Spread.Common.CultureManager.addCultureInfo(newDefaultCultureName, defaultCultureInfo);
                GC.Spread.Common.CultureManager.culture(newDefaultCultureName);
        	}
        }
        spread.resumePaint();
    }

    You can further refer to the attached sample that uses the above code snippet and achieves the required behavior (see below).

    Please feel free to reach out if you encounter any further issues or require additional guidance.

    Attachment: https://jscodemine.mescius.io/share/aeTPRYBf90eer144Ibr4og/?IsEmbed=false&Theme=Unset&PreviewDirection=0&IsEditorShow=true&IsExplorerShow=true&IsPreviewShow=true&IsConsoleShow=true&IsRunBTNShow=false&IsResetBTNShow=false&IsOpenInCodemineBTNShow=false&PanelWidth=20&PanelWidth=50&PanelWidth=30&defaultOpen={"OpenedFileName"%3A["%2Findex.html"]%2C"ActiveFile"%3A"%2Findex.html"}

    Best Regards,

  • Posted 20 February 2025, 3:54 pm EST

    Awesome, thanks a bunch!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels