• Stars
    star
    1
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated over 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Modified and hardcore changed version of Django's i18n-javascript function. Can be usable as JavasScript Template engine. If you familiar with python's "format text" you will love this!

dji18njs

English

dji18njs is a small internationalization and localization script. Original script can be found at: here

I tweaked and changed a lot. i hope you all like it! i've added some sample locale files and a test.html.

Here is the list of my tweaks:

  • Bind to window.dji18njs, dji18njs namespace
  • You can set language by dji18njs.lang_id
  • dji18njs.ngettext re-written from scratch and changed
  • dji18njs.interpolate has changed and added "erroious notification"
  • version property

Usage

Include the script as usual place, then add the locale file (or files). Default language is always English (en)

<script src="dji18njs-1.1.js"></script>
<script src="locale-tr.js"></script>

Catalog file looks like this:

dji18njs.catalog.tr = {
// english => turkish
    key: value,
    "Hello world": 'Merhaba dünya',
}

You can call it:

dji18njs.gettext("Hello world"); // returns 'Merhaba dünya' 

Singular, plural solution:

django.ngettext("ball", "balls", 5); // returns 'balls'
django.ngettext("ball", "balls", 0); // returns 'ball'
django.ngettext("ball", "balls", 1); // returns 'ball'

Interpolation or i call this format string in a python way!

var strFormat = "Hello %s, you have %s %s in your pocket";
dji18njs.interpolate(strFormat, ["vigo", 5, 'coins']);
// returns 'Hello vigo, you have 5 coins in your pocket'

lets do this more complex!

var strFormat = "Hello %s, you have %s %s in your pocket";
var intCoin = 1;
var strSingularPlural = dji18njs.ngettext("coin", "coins", intCoin);
dji18njs.interpolate(strFormat, ["vigo", intCoin, strSingularPlural]);
// returns 'Hello vigo, you have 1 coin in your pocket'

If the catalog exists:

dji18njs.catalog.tr = {
    "coin": 'jeton',
    "coins": 'jeton',
    "Hello %s, you have %s %s in your pocket": 'Selam %s, cebinde %s tane %s var'
};
// the function above
// returns 'Selam vigo, cebinde 1 tane jeton var'

Here comes my favorite. You can use dji18njs as a templating helper:

var strFormat = "Hello %(username)s, your password is: %(password)s";
var dicWords = {
    "username": 'vigo',
    "password": '1!2!3!'
};
dji18njs.interpolate(strFormat, dicWords, true);
// you must pass true as 3rd argument to execute named object!
// returns 'Hello vigo, your password is: 1!2!3!'

Here is an errorious "named interpolate" example:

// string has %(user)s but there is no "user" in the object. it is "userx".
// they don't match

dji18njs.interpolate("Hello %(user)s in Turkish", { userx: "lego" }, true);

// returns 'Hello ?%(user)s? in Turkish'

For more details examples, please check: test.html file

Türkçe

Django ile uğraşırken JavaScript için gördüğüm mükemmel çözüm yöntemini biraz tırtıkladım, sildim baştan yazdım ve çok işime yarayan basit bir script çıkarttım ortaya. Esin kaynağımı buradan inceleyebilirsiniz.

Benim hazırladığım bu versiyonda;

  • window.dji18njs, dji18njs namespace olarak çalışıyor.
  • dji18njs.lang_id özelliği ile dili belirleyebilirsiniz.
  • dji18njs.ngettext metodunu tamamen baştan yazdım.
  • dji18njs.interpolate metodunda hatalı kelime için işaret koyuldu.
  • version bilgisi eklendi.

Yapmanız gereken, html içinde, JavaScript'leri nereye koyuyorsanız, sırasıyla aşağıdaki dosyaları da aynı yere koymak:

<script src="dji18njs-1.1.js"></script>
<script src="locale-tr.js"></script>

Örnek katalog:

dji18njs.catalog.tr = {
// ingilizce => türkçe
    key: value,
    "Hello world": 'Merhaba dünya',
}

Aşağıdaki gibi kullanınca;

dji18njs.gettext("Hello world"); // sonuç 'Merhaba dünya' 

İngilizce'deki tekil/çoğul içinde çözüm var. Aslında sizin de bildiğiniz gibi, Türkçe'deki tekil/çoğul olayı İngilizce'den biraz daha farklı.

django.ngettext("ball", "balls", 5); // sonuç 'balls'
django.ngettext("ball", "balls", 0); // sonuç 'ball'
django.ngettext("ball", "balls", 1); // sonuç 'ball'

Python'daki gibi "format string" tadında; 2 kullanım şekli var: dizi(Array) ya da sözlük/obje(dictionary/object):

var strFormat = "Hello %s, you have %s %s in your pocket";
dji18njs.interpolate(strFormat, ["vigo", 5, 'coins']);
// sonuç 'Hello vigo, you have 5 coins in your pocket'


var strFormat = "Hello %(username)s, your password is: %(password)s";
var dicWords = {
    "username": 'vigo',
    "password": '1!2!3!'
};
dji18njs.interpolate(strFormat, dicWords, true);
// "named object" olayını kullanmak için mutlaka 3.parametre olarak true geçin.
// sonuç 'Hello vigo, your password is: 1!2!3!'

Daha fazla örnek ve bilgi için test.html dosyasına bakabilirsiniz.

More Repositories

1

git-puf-noktalari

Günlük hayatta kullandığımız revizyon kontrol sistemi GIT ile ilgili küçük ipuçlarını anlatan yeni mini kitabım.
Ruby
425
star
2

kommit

Build your commit message without touching or editing your code!
Shell
204
star
3

ruby101-kitap

Türkçe Ruby kitabı
Ruby
197
star
4

dinozorus

1990'lı yıllara damgasını vuran efsane televizyon oyunu Dinozorus. Amiga 1200 - AGA
Assembly
134
star
5

turk-scene-tarihi

80'lerin ortasında başlayan, günümüz bilgisayar kültürünün neredeyse başlangıç noktası olan Türk SCENE/DEMOSCENE tarihçesi
85
star
6

lyk-2017

Linux Yaz Kampı 2017'de anlattığım konular.
41
star
7

django2-project-template

Django project/application starter for lazybones :)
Python
40
star
8

textmate-twitterbootstrap.tmbundle

TextMate bundle for Twitter's Bootstrap
HTML
33
star
9

statoo

`statoo` is a super simple http GET tool for checking site health
Go
32
star
10

awesome-c64

Commodore 64 related links such as C64 tools for Mac/Linux/PC, demoscene related stuff, coding tools, C64 utilities etc...
31
star
11

textmate-octopress.tmbundle

TextMate bundle for Octopress blog engine. http://octopress.org/
29
star
12

gh_issues

Manage GitHub issues from command-line (read only!)
Ruby
22
star
13

dotfiles-light

Just another but `lighter` version of BASH environment
Shell
18
star
14

django-admin-list-filter

Dead simple autocompletion for Django admin `list_filter`.
Python
18
star
15

dotfiles-universal

My old BASH dot-files... Try new one!
Shell
13
star
16

textmate2-python-fmt

Python FMT is a python linter/formatter/checker for TextMate.
Ruby
13
star
17

textmate2-gfm-preview

GitHub Flavored Markdown Preview for TextMate 2
CSS
13
star
18

django-project-template

Project starter for Django 1.11.4 ... Batteries included :)
Python
10
star
19

textmate-hosts.tmbundle

TextMate bundle for /etc/hosts file
9
star
20

dotfiles-fever

Elegant BASH environment for macOS, Ubuntu and Gentoo
Shell
8
star
21

textmate-window-manager

Window manager/arranger for oldskool TextMate (1.5)
Shell
7
star
22

apm-bash-completion

Bash completion for Atom Package Manager (apm)
Shell
6
star
23

stormssh-completion

Bash competion for `sshstorm` (https://github.com/emre/storm)
Shell
6
star
24

devpod

Official website of devPod
HTML
6
star
25

textmate2-ruff-linter

Ruff linter for TextMate2. Linter and auto fixer for python!
Perl
5
star
26

ugur.ozyilmazel.com-v5

Kişisel websitem V4.0
HTML
5
star
27

lsvirtualenvs

Small commandline tool for `virtualenvwrapper`
Go
4
star
28

ghstars

Show GitHub stars count for user!
Go
4
star
29

git-init-githubrepo

Create git repository for GitHub style
Go
4
star
30

git-tips

Git versiyon kontrol sistemi ile ilgili Türkçe dökümantasyon ve ipuçları
JavaScript
4
star
31

golang-notlarim

Golang geliştirme ile ilgili aldığım notlar
Ruby
4
star
32

dox2008

2007-2008 yıllarında İstanbul Bilgi Üniversitesi, web departmanı için hazırladığım javascript ve html/css eğitimleri
JavaScript
4
star
33

textmate2-power-tools

Some useful TextMate2 snippets, commands etc...
Ruby
3
star
34

inspect-go

Ruby-ish Object#inspect tryouts for Golang!
Go
3
star
35

putio

Command-Line client for put.io platform (unofficial)
Go
3
star
36

textmate2-tailwind-css

Tailwind CSS support for TextMate 2
Ruby
3
star
37

textmate2-64tass-bundle

64tass Bundle for TextMate 2
2
star
38

sayisal_captcha

Çok basit şekilde iki basamaklı sayılardan random toplama ya da çarpma üreten bir sistem
Ruby
2
star
39

learning-stimulus-02

I'm learning stimulus, this is example 02
JavaScript
2
star
40

vigo

I'm a hustler baby!
2
star
41

textmate2-gomodifytags

TextMate2 implementation of Fatih’s gomodifytags
Shell
2
star
42

vigo.io

Personal website
HTML
2
star
43

els

Unix `ls` command alternative via Ruby :)
Ruby
2
star
44

pg16_django

3. Programlama Günleri / Karabük - Django ile tanışma
Python
2
star
45

ruby101-kitap-kod

Ruby101 kitabında geçen kod örnekleri
2
star
46

learning-stimulus-01

I'm learning stimulus, this is example 01
JavaScript
1
star
47

textmate2-bootstrap3

Bootstrap 3's helpers for TextMate2
1
star
48

uptimerobot_cmd

Command-line client for Uptimerobot service
Ruby
1
star
49

stringutils-demo

A basic golang package for demo purpose only...
Go
1
star
50

textmate1-twitter-bs3.tmbundle

Twitter Bootstrap 3 bundle for TextMate1 (yes the oldest TextMate)
1
star
51

try_git

1
star
52

homebrew-ghstars

Brew tap for ghstars cli
Ruby
1
star
53

homebrew-lsvirtualenvs

brew tap for lsvirtualenvs
Ruby
1
star
54

homebrew-git-init-githubrepo

brew tap for `git-init-githubrepo`
Ruby
1
star
55

amiga-ascii.tmbundle

Oldskool Amiga ascii/azki/nfo/diz support for TextMate with real Amiga fonts
1
star
56

vigo.github.com

My developer home page...
HTML
1
star
57

ugur.ozyilmazel.com

Kişisel web sitem
HTML
1
star
58

my-custom-textmate1-bundle

Many snippets and my custom scratch-pad for TextMate1.
CSS
1
star
59

textmate-nanorc.tmbundle

.nanorc bundle for TextMate
Ruby
1
star
60

homebrew-statoo

brew tap repo for `statoo`
Ruby
1
star