UNPKG

@nativescript-community/l

Version:

Native internationalization plugin for NativeScript using native capabilities of each platform

48 lines 1.77 kB
import { vsprintf } from 'sprintf-js'; import { Device, Utils } from '@nativescript/core'; import { pluralCategory } from './plural'; let resources; function getResources() { if (!resources) { resources = Utils.ad.getApplicationContext().getResources(); } return resources; } ; export function localizeNative(key, ...args) { let localizedString; try { const identifier = Utils.ad.resources.getStringId(key); localizedString = identifier === 0 ? key : getResources().getString(identifier); return vsprintf(localizedString, args); } catch (error) { return key; } } export function localizeNativePlural(key, count, ...args) { try { const context = Utils.ad.getApplicationContext(); const identifier = getResources().getIdentifier(key, 'plurals', context.getPackageName()); if (identifier !== 0) { return vsprintf(getResources().getQuantityString(identifier, count), [count, ...args]); } const formKeys = [`${key}_${pluralCategory(count)}`, `${key}_other`, key]; const formKey = formKeys.find((candidate) => Utils.ad.resources.getStringId(candidate) !== 0) || key; return localizeNative(formKey, count, ...args); } catch (error) { return key; } } const sdkVersion = parseInt(Device.sdkVersion, 10); export function overrideNativeLocale(lang) { const locale = new java.util.Locale(lang); java.util.Locale.setDefault(locale); const resources = getResources(); const configuration = resources.getConfiguration(); configuration.locale = locale; resources.updateConfiguration(configuration, resources.getDisplayMetrics()); return true; } //# sourceMappingURL=localizenative.android.js.map