There are various options available to address this issue.
First and foremost, it is important to acknowledge some potential challenges. By storing translations in local storage, you may encounter difficulties when updating translations in the future. Furthermore, adding new translations to an updated website could pose challenges, requiring data hashing in local storage to verify changes with the server.
One effective solution is to inject translations directly into the webpage as static data, eliminating the need for ajax requests. Although this may require server side rendering, it is a reliable approach.
If you are determined to utilize local storage, consider the following strategies for managing multiple pages with the same translation keys:
- Implement namespaces for all translation keys, such as page1:mName and page2:mName.
- Utilize per page local storage, storing translations for each page in separate local storage keys.
To save a string to local storage, use the following code:
localStorage.setItem(key, value)
To retrieve a key from local storage, use this code:
localStorage.getItem(key)