Code Monkey home page Code Monkey logo

i18n's Introduction

I18n

[عربي]

Internationalization functionality for Alusus Language. This library can load translations from PO files.

Usage

  • Add the library to the project using Alusus Package Manager:
import "Apm";
Apm.importFile("Alusus/I18n");
  • Initialize the library from PO file:
I18n.initializeStrings(poContent);

or

I18n.initializeStrings(Srl.Fs.readFile("lang.po"));

You can also load additional strings from a separate PO file using addStrings:

I18n.addStrings(poContent);

or

I18n.addStrings(Srl.Fs.readFile("lang.po"));

You can use the clearStrings function to clear all the loaded strings.

  • Replace the hardcoded strings with I18n.string:
Console.print(I18n.string("My localized string"));

Determining Language Direction

To determine the direction of a language based on its code, i.e. determine whether it's right from right to left or left to right, we use the isRightToLeft function:

I18n.isRightToLeft(String("ar")) // returns true
I18n.isRightToLeft(String("ar_EG")) // returns true
I18n.isRightToLeft(String("en")) // returns false

TextAssets class

This class enables you to load text files in multiple languages from a folder and automatically organize them as a class containing a text property for each of these files. It then loads them for you automatically, categorized by languages according to the available folders. This class takes two parameters: one is the path to the text resources folder, and the other is the default language code. It expects the text resources folder to contain a subfolder for each language with the language code as the folder name. Inside each of these subfolders, the text files with the same names for all languages are placed. The class will define a variable for each of these files, named after the file but without the extension.

Note: This class will load all resources for all languages during compilation rather than during execution, allowing it to be used for translating user interfaces in web applications.

For example, if you have the following folder:

assets/
  en/
    file1.txt
    file2.txt
    strings.po
  ar/
    file1.txt
    file2.txt
    strings.po

After initializing the class, you'll have a class containing a Map with two keys: 'ar' and 'en', and for each key an instance of the class. In this scenario, the class will contain three variables of type String: file1, file2, and strings.

To initialize and use this class:

I18n.TextAssets["./text_assets", "en"].initialize();
I18n.TextAssets["./text_assets", "en"].setCurrentLanguage(currentLang);
def strings: String = I18n.TextAssets["./text_assets", "en"].current.strings;
I18n.initializeStrings(strings);
Console.print("%s\n", I18n.string("string example"));

You can access the current language code using the variable currentLanguage. It's preferable to rely on this variable as it ensures the usage of only available languages within the current translations. For instance, if you call the setCurrentLanguage function with a language code that isn't available in the translations, the class will choose the default language instead of selecting a language with no translation.

TextAssets.getAvailableLanguages

This function returns a list of available languages. The initialize function must be called before invoking this function. It ensures that the default language is placed at the beginning of the returned array.

func getAvailableLanguages(): Array[String];

i18n's People

Contributors

sarmadka avatar slemanalrstum231 avatar

Stargazers

Ali Galal avatar

Watchers

 avatar Faisal Al-Humaimidi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.