Created
February 4, 2020 14:15
-
-
Save simon04/44fd82f9b717e411e5fd5427582545ac to your computer and use it in GitHub Desktop.
node-polyglot for angularjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
navbar: | |
homepage: 'Navigate to the homepage' | |
user: 'Logged in as {{user}}' | |
load: 'Load {{smart_count}} item |||| Load {{smart_count}} items' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Polyglot = require('node-polyglot'); | |
import en from './en.yaml'; | |
import de from './de.yaml'; | |
const phases = {en, de}; | |
export const polyglot = new Polyglot({ | |
interpolation: { | |
prefix: '{{', | |
suffix: '}}' | |
} | |
}); | |
setLocale('de'); | |
export function setLocale(locale: string) { | |
polyglot.locale(locale); | |
polyglot.replace(phases[locale]); | |
return polyglot; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Polyglot = require('node-polyglot'); | |
function provider(polyglot: Polyglot): ng.IFilterFunction { | |
return filter; | |
/** | |
* Translates the given key | |
* @param key the i18n string | |
* @param variable variable name for interpolation | |
* @param value variable value for interpolation | |
*/ | |
function filter(key: string, variable?: number | string, value?: object) { | |
if (typeof variable === 'number') { | |
return polyglot.t(key, variable); | |
} else if (variable) { | |
return polyglot.t(key, {[variable]: value}); | |
} else { | |
return polyglot.t(key); | |
} | |
} | |
} | |
provider.$inject = ['polyglot']; | |
export default provider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment