The main idea for external .js file is quite simple – use global variable (of course you can use namespaces as well, but in this post I will use global variable for simplicity) which will be available in .js file and which will keep locale of current SPWeb:
To use this variable in .js file the following function was added:
So those pages which don’t have globalLocale defined will have empty value and then use some default settings.
Keeping in mind DRY principle we didn’t want to copy and past definition of globalLocale variable in each page where it is required. In order to avoid duplication several approaches can be used:
- include definition of globalLocale variable using server-side include. I.e. you need to move definition into separate .ascx file (extension is not important actually) and then include it in your pages:
This appoach works only for application pages – i.e. pages which are stored on filesystem in 12/template/layouts folder. It doesn’t work for publishing pages and master pages because server-side includes are not allowed in pages of this type.
- use regular user control .ascx file, which contains definition of globalLocale variable, and register it as regular user control wherever you need. This approach works both for application and publishing pages.
We used 2nd approach – but if you are not able to use user controls because of some reasons, 1st way is applicable as well.