UNPKG

@nativescript-community/l

Version:

Native internationalization plugin for NativeScript using native capabilities of each platform

43 lines 1.46 kB
import { vsprintf } from 'sprintf-js'; import { convertAtSignToStringSign } from './placeholder'; import { pluralCategory } from './plural'; let bundle; const getBundle = (function () { return function () { if (!bundle) { bundle = NSBundle.mainBundle; } return bundle; }; })(); export function localizeNative(key, ...args) { try { const localizedString = getBundle().localizedStringForKeyValueTable(key, key, null); return vsprintf(convertAtSignToStringSign(localizedString), args); } catch (error) { return key; } } export function localizeNativePlural(key, count, ...args) { try { const formKeys = [`${key}_${pluralCategory(count)}`, `${key}_other`, key]; const formKey = formKeys.find((candidate) => getBundle().localizedStringForKeyValueTable(candidate, candidate, null) !== candidate) || key; return localizeNative(formKey, count, ...args); } catch (error) { return key; } } export function overrideNativeLocale(locale) { const path = NSBundle.mainBundle.pathForResourceOfType(locale.substring(0, 2), 'lproj'); if (!path) { return false; } bundle = NSBundle.bundleWithPath(path); NSUserDefaults.standardUserDefaults.setObjectForKey([locale], 'AppleLanguages'); NSUserDefaults.standardUserDefaults.synchronize(); return true; } //# sourceMappingURL=localizenative.ios.js.map