A JavaScript library to translate HTML content into multiple languages.
For TypeScript compilation please install Cloud Mate from npm
npm i -g cloudmateTo generate testing JavaScript file and keep watching for changes run the below:
cloudmate -wWhen you're done, please update the version under package.json and run the following for generating distribution files:
cloudmate distCheck out https://angrymonkeycloud.com/cloudlocalization for more information.
jQuery 2.x or 3.x: jQuery CDN, download jQuery.
Cloud Localization JavaScript files: Uncompressed, Compressed.
Cloud Localization JavaScript Compressed
<script
src="https://cdn.amcapi.com/localization/1.0.2/cloudlocalization.min.js"
crossorigin="anonymous"
></script>Initialize Cloud Localization as follows:
import cloudLocalization, { CloudLocalization, LanguageDirection, TranslatorProvider, UrlLanguageLocation } from "./cloudlocalization";
cloudLocalization({
defaultLanguage: "en", // optional | The main language used.
defaultTextLanguage: "en", // optional | The language in which the default text is written.
urlLanguageLocation: UrlLanguageLocation.none, // optional | To append language in the url (ex: www.website.com/en/).
translatorProvider: TranslatorProvider.none, // optional | Translate text using a translator provider.
translatorProviderKey: "", // optional | The translator provider key.
logTranslationsFromProvider: false, // optional | Set to true to log the translation output in the console.
useDefaultLanguageAlways: false, // optional | Always use the default language.
restartOnLanguageChange: true, // optional | Restart the application on language change.
languages: [
{
code: "en",
displayName: "English"
},
{
code: "es",
displayName: "Spanish"
},
{
code: "fr",
displayName: "Français"
},
{
code: "fr-ca",
displayName: "Français (Canada)"
},
{
code: "ar",
displayName: "العربية",
direction: LanguageDirection.rtl
}
]
});
// Get selected language code.
console.log(CloudLocalization.currentLanguage.code);
document.getElementById('expandButton')?.addEventListener('click', async () => {
let div: HTMLElement | null = document.querySelector('#newDiv');
if (div === null) {
div = document.createElement('div');
div.id = 'newDiv';
div.innerHTML = "Logo";
const expandButton = document.getElementById('expandButton');
if (expandButton?.parentNode) {
expandButton.parentNode.insertBefore(div, expandButton.nextSibling);
}
await CloudLocalization.translateElement(div);
}
});Add predefined languages as json files under the 'translation' directory. Each language code should have its own file named: {languageCode}.json.
o: Original text.
t: Translated text.
[
{
"o": "Logo",
"t": "شعار"
},
{
"o": "Welcome",
"t": "اهلا وسهلا"
},
{
"o": "Home",
"t": "الصفحة الرئيسية"
}
]You can add an empty select into your website having the class "cloudlocalization-selection", which will be filled in with the predefined languages automatically.
<select class="cloudlocalization-selection"></select>Specify HTML attribute translate="no" to prevent the translation of inside text, check out more on MDN web docs
Cloud Localization supports RTL (Right-to-Left) languages. The library automatically adjusts the CSS properties for RTL languages.
To use Azure Translator, set the translatorProvider to TranslatorProvider.Azure and provide the translatorProviderKey.
cloudLocalization({
translatorProvider: TranslatorProvider.Azure,
translatorProviderKey: "YOUR_AZURE_TRANSLATOR_KEY",
// other settings...
});The library provides a method to scroll to the top of the page.
CloudLocalization.scrollToTop(200); // Scroll to top with a duration of 200msThe library provides a method to update the current language based on various factors like local storage, URL, and browser settings.
CloudLocalization.updateCurrentLanguage();The library provides methods to get translations for specific texts.
const translations = await CloudLocalization.getTranslation("Hello");