Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Localize your greeting message with javascript
##################################################################
# #
# Copyright (C) 2012 Dommmel <dommmel@gmail.com> #
# This code is released under WTFPL, version 2.0. #
# #
##################################################################
# Usage:
# <span id="sayhi">привет</span>
# <script>
# replace_text_with_greeting("#sayhi");
# </script>
# country codes should have the ISO 3166 format.
# See http://en.wikipedia.org/wiki/ISO_3166-2
greeting_for =
"default" : "Hi"
"DE" :
"default" : "Hallo"
"Baden-Wurttemberg" : "Grüß Gottle"
"Bayern" : "Servus"
"Hamburg" : "Moin"
"CH" : "Grüezi"
"AT" : "Habe die Ehre"
"FR" : "Salut"
"US" : "Hello"
replace_text_with_greeting = (selector) ->
$.getJSON "http://freegeoip.net/json/?callback=?", (resp) ->
$(selector).text(
if greeting_for[resp.country_code]?
greeting_for[resp.country_code][resp.region_name] or greeting_for[resp.country_code]["default"] or greeting_for[resp.country_code]
else
greeting_for["default"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment