Download src/js/intl/index.js from merve/git-101: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/spaces/merve/git-101/resolve/main/src/js/intl/index.js
- Command line
-
hf download hf://spaces/merve/git-101/src/js/intl/index.js
-
curl -L -o index.js https://huggingface.co/spaces/merve/git-101/resolve/main/src/js/intl/index.js
3.3 kB
| var LocaleStore = require('../stores/LocaleStore'); | |
| var _ = require('underscore'); | |
| var strings = require('../intl/strings').strings; | |
| var getDefaultLocale = LocaleStore.getDefaultLocale; | |
| var fallbackMap = { | |
| 'zh_TW': 'zh_CN', | |
| 'es_AR': 'es_ES', | |
| 'es_MX': 'es_ES' | |
| }; | |
| // lets change underscores template settings so it interpolates | |
| // things like "{branchName} does not exist". | |
| var templateSettings = Object.assign({}, _.templateSettings); | |
| templateSettings.interpolate = /\{(.+?)\}/g; | |
| var template = exports.template = function(str, params) { | |
| return _.template(str, templateSettings)(params); | |
| }; | |
| var str = exports.str = function(key, params) { | |
| params = params || {}; | |
| // this function takes a key like "error-branch-delete" | |
| // and parameters like {branchName: 'bugFix', num: 3}. | |
| // | |
| // it sticks those into a translation string like: | |
| // 'en': 'You can not delete the branch {branchName} because' + | |
| // 'you are currently on that branch! This is error number + {num}' | |
| // | |
| // to produce: | |
| // | |
| // 'You can not delete the branch bugFix because you are currently on that branch! | |
| // This is error number 3' | |
| var locale = LocaleStore.getLocale(); | |
| if (!strings[key]) { | |
| console.warn('NO INTL support for key ' + key); | |
| return 'NO INTL support for key ' + key + '. this is probably a dev error'; | |
| } | |
| if (!strings[key][locale]) { | |
| // try falling back to another locale if in the map | |
| locale = fallbackMap[locale] || getDefaultLocale(); | |
| } | |
| if (!strings[key][locale]) { | |
| if (key !== 'error-untranslated') { | |
| return str('error-untranslated'); | |
| } | |
| return 'No translation for the key "' + key + '"'; | |
| } | |
| return template( | |
| strings[key][locale], | |
| params | |
| ); | |
| }; | |
| var getIntlKey = exports.getIntlKey = function(obj, key, overrideLocale) { | |
| if (!obj || !obj[key]) { | |
| throw new Error('that key ' + key + ' doesn\'t exist in this blob ' + obj); | |
| } | |
| if (!obj[key][getDefaultLocale()]) { | |
| console.warn( | |
| 'WARNING!! This blob does not have intl support:', | |
| obj, | |
| 'for this key', | |
| key | |
| ); | |
| } | |
| var locale = overrideLocale || LocaleStore.getLocale(); | |
| return obj[key][locale]; | |
| }; | |
| exports.todo = function(str) { | |
| return str; | |
| }; | |
| exports.getDialog = function(obj) { | |
| return getIntlKey(obj, 'dialog') || obj.dialog[getDefaultLocale()]; | |
| }; | |
| exports.getHint = function(level) { | |
| if (!getIntlKey(level, 'hint')) { | |
| return getIntlKey(level, 'hint', getDefaultLocale()) + ' -- ' + str('error-untranslated'); | |
| } | |
| return getIntlKey(level, 'hint'); | |
| }; | |
| exports.getName = function(level) { | |
| if (!getIntlKey(level, 'name')) { | |
| return getIntlKey(level, 'name', getDefaultLocale()) + ' -- ' + str('error-untranslated'); | |
| } | |
| return getIntlKey(level, 'name'); | |
| }; | |
| exports.getStartDialog = function(level) { | |
| var startDialog = getIntlKey(level, 'startDialog'); | |
| if (startDialog) { return startDialog; } | |
| // this level translation isn't supported yet, so lets add | |
| // an alert to the front and give the english version. | |
| var errorAlert = { | |
| type: 'ModalAlert', | |
| options: { | |
| markdown: str('error-untranslated') | |
| } | |
| }; | |
| var startCopy = Object.assign( | |
| {}, | |
| level.startDialog[getDefaultLocale()] || level.startDialog | |
| ); | |
| startCopy.childViews.unshift(errorAlert); | |
| return startCopy; | |
| }; | |